龙空技术网

Python爬虫的第二种姿势,Selenium框架案例讲解

Python可乐 410

前言:

现在看官们对“selenium爬虫python”大约比较关怀,同学们都需要知道一些“selenium爬虫python”的相关知识。那么小编在网摘上搜集了一些对于“selenium爬虫python””的相关内容,希望我们能喜欢,小伙伴们一起来学习一下吧!

文章目录selenium模块selenium基本概念基本使用代码基于浏览器自动化的操作代码代码selenium处理iframe:代码selenium模拟登陆QQ空间代码无头浏览器和规避检测代码selenium模块selenium基本概念selenium优势

便捷地获取网站中动态加载的数据

便捷实现模拟登陆

私下小编01即可获取大量Python学习资料,电子书籍,视频教程源码案例!

selenium使用流程:

1.环境安装:

pip install selenium

2.下载一个浏览器的驱动程序(谷歌浏览器)

3.实例化一个浏览器对象基本使用

代码

from selenium import webdriverfrom lxml import etreefrom time import sleepif __name__ == '__main__':    bro = webdriver.Chrome(r"E:\google\Chrome\Application\chromedriver.exe")    bro.get(url=';)    page_text = bro.page_source    tree = etree.HTML(page_text)    li_list = tree.xpath('//*[@id="gzlist"]/li')    for li in li_list:        name = li.xpath('./dl/@title')[0]        print(name)    sleep(5)    bro.quit()

基于浏览器自动化的操作代码

#编写基于浏览器自动化的操作代码

- 发起请求: get(url)

- 标签定位: find系列的方法

- 标签交互: send_ keys( 'xxx' )

- 执行js程序: excute_script('jsCod')

- 前进,后退: back(),forward( )

- 关闭浏览器: quit()1

代码

 selenium import webdriverfrom time import sleepbro = webdriver.Chrome(executable_path=r"E:\google\Chrome\Application\chromedriver.exe")bro.get(url=';)#标签定位search_input = bro.find_element_by_id('q')sleep(2)#执行一组js代码,使得滚轮向下滑动bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')sleep(2)#标签交互search_input.send_keys('女装')button = bro.find_element_by_class_name('btn-search')button.click()bro.get(';)sleep(2)bro.back()sleep(2)bro.forward()sleep(5)bro.quit()

selenium处理iframe:

- 如果定位的标签存在于iframe标签之中,则必须使用switch_to.frame(id)

- 动作链(拖动) : from selenium. webdriver import ActionChains

- 实例化一个动作链对象: action = ActionChains (bro)

- click_and_hold(div) :长按且点击操作

- move_by_offset(x,y)

- perform( )让动作链立即执行

- action.release( )释放动作链对象

代码

 selenium import webdriverfrom time import sleepfrom selenium.webdriver import ActionChainsbro = webdriver.Chrome(executable_path=r"E:\google\Chrome\Application\chromedriver.exe")bro.get(';)bro.switch_to.frame('iframeResult')div = bro.find_element_by_id('draggable')#动作链action = ActionChains(bro)action.click_and_hold(div)for i in range(5):    action.move_by_offset(17,0).perform()    sleep(0.3)#释放动作链action.release()bro.quit()

selenium模拟登陆QQ空间

代码

 selenium import webdriverfrom time import sleepbro = webdriver.Chrome(executable_path=r"E:\google\Chrome\Application\chromedriver.exe")bro.get(';)bro.switch_to.frame("login_frame")switcher = bro.find_element_by_id('switcher_plogin')switcher.click()user_tag = bro.find_element_by_id('u')password_tag = bro.find_element_by_id('p')user_tag.send_keys('1234455')password_tag.send_keys('qwer123')sleep(1)but = bro.find_element_by_id('login_button')but.click()

无头浏览器和规避检测

代码

from  selenium import webdriverfrom time import sleep#实现无可视化界面from selenium.webdriver.chrome.options import Options#实现规避检测from selenium.webdriver import ChromeOptions#实现无可视化界面chrome_options = Options()chrome_options.add_argument('--headless')chrome_options.add_argument('--disable-gpu')#实现规避检测option = ChromeOptions()option.add_experimental_option('excludeSwitches',['enable-automation'])bro = webdriver.Chrome(executable_path=r"E:\google\Chrome\Application\chromedriver.exe",chrome_options=chrome_options,options=option)bro.get(';)print(bro.page_source)sleep(2)bro.quit()

标签: #selenium爬虫python #js向下滑动 #jquery触发滚轮 #pythonselenium爬虫 #php 爬虫框架