爬虫中主要用解决JavaScript渲染问题
作者:互联网
Selenium
自动化测试工具,支持多种浏览器;
爬虫中主要用来解决JavaScript渲染问题。
用法详解
基本使用
声明浏览器对象
from selenium import webdriver
browser = webdriver.Chrome()
browser = webdriver.Firefox()
browser = webdriver.Edge()
browser = webdriver.PhantomJS()
browser = webdriver.Safari()
访问页面
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
print(browser.page_source)
browser.close()
查找元素
单个元素
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
input_first = browser.find_element_by_id('q')
input_second = browser.find_element_by_css_selector('#q')
input_third = browser.find_element_by_xpath('//*[@id="q"]')
print(input_first, input_second, input_third)#这些方法效果是同样的
browser.close()
一样的方法
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
input_first = browser.find_element(By.ID,'q')
print(input_first)
browser.close()
多个元素
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
lis = browser.find_elements_by_css_selector('.service-bd Ii')
print(lis)
browser.close()
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
lis = browser.find_elements(By.CSS_SELECTOR,'.service-bd li')
print(lis)
browser.close()
元素交互操作
对获取的元素调用交互方法
交互动作
将动作附加到动作链中串行执行
执行JavaScript
获取元素信息
Frame
等待
https://www.ximalaya.com/yinyue/25395842/
https://www.ximalaya.com/yinyue/25395858/
https://www.ximalaya.com/yinyue/25395873/
https://www.ximalaya.com/yinyue/25395889/
https://www.ximalaya.com/yinyue/25395897/
https://www.ximalaya.com/yinyue/25395905/
https://www.ximalaya.com/yinyue/25395915/
https://www.ximalaya.com/yinyue/25395917/
https://www.ximalaya.com/yinyue/25395753/
https://www.ximalaya.com/yinyue/25395736/
https://www.ximalaya.com/yinyue/25395762/
https://www.ximalaya.com/yinyue/25395769/
https://www.ximalaya.com/yinyue/25395772/
https://www.ximalaya.com/yinyue/25395777/
https://www.ximalaya.com/yinyue/25395786/
https://www.ximalaya.com/yinyue/25395791/
https://www.ximalaya.com/yinyue/25395804/
https://www.ximalaya.com/yinyue/25395828/
https://www.ximalaya.com/yinyue/25395653/
https://www.ximalaya.com/yinyue/25395647/
https://www.ximalaya.com/yinyue/25395659/
https://www.ximalaya.com/yinyue/25395666/
https://www.ximalaya.com/yinyue/25395674/
标签:yinyue,www,渲染,ximalaya,JavaScript,爬虫,https,com,browser 来源: https://www.cnblogs.com/strugksjncxa/p/11256255.html