1、seleniumhtml
是Python的一个第三方库,对外提供的接口能够操做浏览器,而后让浏览器完成自动化的操做。 web
环境搭建chrome
安装selenum:pip install seleniumwindows
获取某一款浏览器的驱动程序(以谷歌浏览器为例)api
谷歌浏览器驱动下载地址:http://chromedriver.storage.googleapis.com/index.html
浏览器
下载的驱动程序必须和浏览器的版本统一,你们能够根据http://blog.csdn.net/huilan_same/article/details/51896672中提供的版本映射表进行对应
less
效果展现:ide
from selenium import webdriver from time import sleep # 建立浏览器对象,后面是你的浏览器驱动位置,记得前面加r'','r'是防止字符转义的 bai = webdriver.Chrome(executable_path=r'D:\pycharm\exercise\13.爬虫\day4\谷歌浏览器驱动\chromedriver.exe') # 同过get发送请求 bai.get(url='https://www.baidu.com') # 根据find系列的函数定位到指定的标签 my_input = bai.find_element_by_id("kw") sleep(3) # 向标签中指定录入的内容 my_input.send_keys("美女") sleep(3) # 根据find系列的函数定位到指定的标签 my_button = bai.find_element_by_id("su").click() sleep(3) # 获取当前浏览器显示的页面的页面源码 page_text = bai.page_source #关闭浏览器 bai.quit()
代码解释:函数
#导包 from selenium import webdriver #建立浏览器对象,经过该对象能够操做浏览器 browser = webdriver.Chrome('驱动路径') #使用浏览器发起指定请求 browser.get(url) #使用下面的方法,查找指定的元素进行操做便可 find_element_by_id 根据id找节点 find_elements_by_name 根据name找 find_elements_by_xpath 根据xpath查找 find_elements_by_tag_name 根据标签名找 find_elements_by_class_name 根据class名字查找
#像指定的标签元素中录入指定的内容
send_keys("指定输入的内容")
page_source 获取当前浏览器显示的页面的页面源码
quit() 关闭浏览器
示例:自动化登录qq空间,并获取部分好友动态信息 网站
1 from selenium import webdriver 2 from time import sleep 3 from lxml import etree 4 5 # 爬取qq空间内容 6 bro= webdriver.Chrome(executable_path=r'D:\pycharm\exercise\13.爬虫\day4\谷歌浏览器驱动\chromedriver.exe') 7 8 url = 'https://qzone.qq.com/' 9 10 bro.get(url=url) 11 sleep(1) 12 13 # 定位到指定的iframe, switch_to.frame() 定位到当前指定元素 14 bro.switch_to.frame('login_frame') 15 bro.find_element_by_id('switcher_plogin').click() 16 sleep(1) 17 18 username = bro.find_element_by_id("u") 19 username.send_keys('169675573') 20 password = bro.find_element_by_id("p") 21 password.send_keys('xxxxxx') 22 sleep(1) 23 bro.find_element_by_id('login_button').click() 24 sleep(2) 25 26 # 拿到登录以后的页面 27 page_text = bro.page_source 28 sleep(5) 29 30 31 tree = etree.HTML(page_text) 32 div_list = tree.xpath('//div[@class="f-info qz_info_cut"] | div[@class="f-info"]') 33 for div in div_list: 34 text = div.xpath('./text(text)') 35 text = "".join() 36 print(text) 37 print("下载完成!!!") 38 bro.quit()
2、 phantomJs
效果展现:
# 无界面浏览器的使用 from selenium import webdriver from time import sleep #建立浏览器对象,经过该对象能够操做浏览器,后面是你的浏览器驱动位置,记得前面加r'','r'是防止字符转义的 bai = webdriver.PhantomJS(executable_path=r'D:\pycharm\exercise\13.爬虫\day4\phantomjs-2.1.1-windows\bin\phantomjs.exe') # 同过get发送请求 bai.get(url='https://www.baidu.com') bai.save_screenshot('./1.jpg') # 截图 在无界面执行的时候截取当前执行动做 # 根据find系列的函数定位到指定的标签 my_input = bai.find_element_by_id("kw") sleep(3) # send_keys 向标签中指定录入的内容 my_input.send_keys("美女") sleep(3) # 根据find系列的函数定位到指定的标签 my_button = bai.find_element_by_id("su").click() sleep(3) # 获取当前浏览器显示的页面的页面源码 page_text = bai.page_source bai.save_screenshot('./2.jpg') # save_screenshot函数实现截图功能 print(page_text) # 关闭浏览器 bai.quit()
selenium 和 phantomJs 实现的功能是同样的,都须要指定驱动程序,
不一样的:
selenium:是Python的一个第三方库,对外提供的接口能够操做浏览器,而后让浏览器完成自动化的操做。
phantomJs:是一款无界面的浏览器,其自动化操做流程和上述操做谷歌浏览器是一致的
3、谷歌无头浏览器
因为PhantomJs最近已经中止了更新和维护,因此推荐你们可使用谷歌的无头浏览器,是一款无界面的谷歌浏览器
在使用谷歌无头浏览器的时候,须要导入一个包,并建立参数对象,并在建立浏览器对象的时候,将建立的参数对象,添加到浏览器驱动位置的后面,便可使用谷歌无头浏览器
导入包:
from selenium.webdriver.chrome.options import Options
建立一个参数对象,用来控制chrome以无界面模式打开:
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
将建立的参数对象添加到浏览器对象的驱动位置后面:
webdriver.Chrome('驱动路径', chrome_options=chrome_options)
效果展现:
# 谷歌无头浏览器 from selenium import webdriver from time import sleep # 导入指定包 from selenium.webdriver.chrome.options import Options # 建立一个参数对象,用来控制chrome以无界面模式打开 chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') bai = webdriver.Chrome(executable_path= r'D:\pycharm\exercise\13.爬虫\day4\谷歌浏览器驱动\chromedriver.exe',chrome_options=chrome_options) # 同过get发送请求 bai.get(url='https://www.baidu.com') # 根据find系列的函数定位到指定的标签 my_input = bai.find_element_by_id("kw") sleep(3) # 向标签中指定录入的内容 my_input.send_keys("美女") sleep(3) # 根据find系列的函数定位到指定的标签 my_button = bai.find_element_by_id("su").click() sleep(3) # 获取当前浏览器显示的页面的页面源码 page_text = bai.page_source print(page_text) # 关闭浏览器 bai.quit()
selenium+phantomjs 就是爬虫终极解决方案:
有些网站上的内容信息是经过动态加载js造成的,因此使用普通爬虫程序没法回去动态加载的js内容。例如豆瓣电影中的电影信息是经过下拉操做动态加载更多的电影信息。
示例:需求是尽量多的爬取豆瓣网中的电影信息
from selenium import webdriver from time import sleep bro= webdriver.Chrome(executable_path=r'D:\pycharm\exercise\13.爬虫\day4\谷歌浏览器驱动\chromedriver.exe') url = 'https://movie.douban.com/typerank?type_name=%E7%88%B1%E6%83%85&type=13&interval_id=100:90&action=' # 请求发送 bro.get(url=url) # 获取当前网页页面显示的高度 js = "window.scrollTo(0,document.body.scrollHeight)" # 执行js代码,execute_script该函数能够执行一组字符串形式的js代码 bro.execute_script(js) sleep(2) bro.execute_script(js) sleep(2) bro.execute_script(js) sleep(2) bro.execute_script(js) sleep(2) page_text = bro.page_source # page_source 该属性能够获取当前浏览器的当前页的源码(html) with open("./douban.html","w",encoding="utf-8") as fp: fp.write(page_text) bro.quit()