爬虫--selenuim和phantonJs处理网页动态加载数据的爬取

1、谷歌浏览器的使用

下载谷歌浏览器

安装谷歌访问助手

终于用上谷歌浏览器了。。。。。激动

问题:处理页面动态加载数据的爬取

-1.selenium
-2.phantomJs

1.selenium

二.selenium

什么是selenium?
是Python的一个第三方库,对外提供的接口可以操作浏览器,然后让浏览器完成自动化的操作。  

环境搭建

安装selenum:pip install selenium

获取某一款浏览器的驱动程序(以谷歌浏览器为例)

谷歌浏览器驱动下载地址:http://chromedriver.storage.googleapis.com/index.html

下载的驱动程序必须和浏览器的版本统一,大家可以根据http://blog.csdn.net/huilan_same/article/details/51896672中提供的版本映射表进行对应

下载好后选择相应版本解压后粘贴到项目文件夹下

 把谷歌浏览器设置成默认的浏览器

import selenium
print(selenium.__file__)

ModuleNotFoundError: No module named 'selenium'

在jupyter下载模块:

------------------------------------------------------------------------------------------

运行成功

 -----------------------------------------------------------------------------

#使用下面的方法,查找指定的元素进行操作即可
    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名字查找
# # #编码流程:
from selenium import webdriver
# 操作速度快 引入sleep观看效果
from time import sleep
#创建一个浏览器对象    executable_path是驱动的路径
bro = webdriver.Chrome(executable_path='./chromedriver')
#get方法可以指定一个url,让浏览器进行请求
bro.get('https://www.baidu.com')

# 让百度进行指定词条的搜索
text = bro.find_element_by_id('kw') # 定位到了text文本框
text.send_keys('人名币') # send_keys表示向文本框中录入指定内容 
sleep(1)
button = bro.find_element_by_id('su')
button.click()# 点击操作
sleep(3)
bro.quit()#关闭浏览器

# import selenium
# print(selenium.__file__)

2、phantomJs

  • PhantomJS是一款无界面的浏览器,其自动化操作流程和上述操作谷歌浏览器是一致的。由于是无界面的,为了能够展示自动化操作流程,PhantomJS为用户提供了一个截屏的功能,使用save_screenshot函数实现。

 下载驱动程序,放在当前目录下

from selenium import webdriver

bro = webdriver.PhantomJS(executable_path='C:/Users/Administrator/爬虫/动态加载下的爬虫/phantomjs-2.1.1-windows/bin/phantomjs')

# 打开浏览器
bro.get('https://www.baidu.com')

# 截屏的操作 
bro.save_screenshot('./1.png')

# 让百度进行指定词条的搜索
text = bro.find_element_by_id('kw') # 定位到了text文本框
text.send_keys('人名币') # send_keys表示向文本框中录入指定内容 
# 截屏操作
bro.save_screenshot('./2.png')
bro.quit()

使用selenium+phantomJs处理页面动态加载数据的爬取

  • 需求:获取豆瓣电影中动态加载出更多电影详情数据
from selenium import webdriver
from time import sleep
bro = webdriver.PhantomJS(executable_path='C:/Users/Administrator/爬虫/动态加载下的爬虫/phantomjs-2.1.1-windows/bin/phantomjs')
url = 'https://movie.douban.com/typerank?type_name=%E5%96%9C%E5%89%A7&type=24&interval_id=100:90&action='
bro.get(url)
sleep(1)
bro.save_screenshot('./3.png')
# 向下拖动滚轮
# 编写js代码,让页面中的滚轮滑动到底部
js = 'window.scrollTo(0,document.body.scrollHeight)'

# 如何让浏览器对象执行js代码
bro.execute_script(js)
sleep(1)
bro.save_screenshot('./4.png')
# 获取加载数据后的页面: page_source获取浏览器当前数据
page_text = bro.page_source

# 解析数据

print(page_text)

 谷歌无头浏览器

  • 由于PhantomJs最近已经停止了更新和维护,所以推荐大家可以使用谷歌的无头浏览器,是一款无界面的谷歌浏览器。
  • 代码展示: 
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
 
# 创建一个参数对象,用来控制chrome以无界面模式打开
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
# 驱动路径
path = r'C:UsersBLiDesktop1801day05ziliaochromedriver.exe'
 
# 创建浏览器对象
browser = webdriver.Chrome(executable_path=path, chrome_options=chrome_options)
 
# 上网
url = 'http://www.baidu.com/'
browser.get(url)
time.sleep(3)
 
browser.save_screenshot('baidu.png')
 
browser.quit()
原文地址:https://www.cnblogs.com/foremostxl/p/10072122.html