selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。
pip3 install selenium -i https://pypi.douban.com/simple/
1.PhantomJS+Selenium
PhantomJS是一个基于webkit的无头浏览器JavaScript API。
https://phantomjs.org/download.html
https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-macosx.zip
直接使用PhantomJS可以看这里:
https://phantomjs.org/quick-start.html
https://www.jianshu.com/p/8210a17bcdb8
1 2 3 4 5 6 7 8 9 10 11 12 |
from selenium import webdriver # driver = webdriver.PhantomJS() # 配置到了环境变量 driver = webdriver.PhantomJS(executable_path="/apps/phantomjs") # 指定PhantomJS执行程序的路径 driver.get('http://www.baidu.com/') # 访问 page = driver.page_source # 网页源码 tableData = driver.find_elements_by_tag_name('tableData').get_attribute('innerHTML') # 获取元素的html源码 tableI = driver.find_elements_by_tag_name('tableData').get_attribute('id') # 获取元素的id值 tableI = driver.find_elements_by_tag_name('tableData').text # 获取元素的文本内容 driver.quit() # 退出浏览器 |
2.Chrome+Selenium
要使用Chrome浏览器的无头模式驱动,须根据当前浏览器的版本下载,即本地须已有一个正常的Chrome浏览。
http://npm.taobao.org/mirrors/chromedriver ,当前Chrome版本可以从 chrome://settings/help 查看。
将之安装在一个环境变量目录里(或新配置) 如 $JAVA_HOME/bin 下。如果不放到环境变量目录里,则须要在代码中指定这个驱动文件的路径。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
from selenium import webdriver option = webdriver.ChromeOptions() # 1.配置参数 # option.set_headless() # 设为无头模式 option.add_argument("--headless") # 无头模式 option.add_argument("--no-sandbox") option.add_argument("--disable-gpu") option.add_argument("disable-infobars") # browser = webdriver.Chrome(executable_path="chromedriver路径", options=option) # 2.创建浏览器 browser = webdriver.Chrome(options=option) # 2.创建浏览器 browser.get("https://www.baidu.com/") # 3.请求连接 html = browser.page_source # 拿到页面 browser.close() # 关闭连接 browser.quit() |
3.Firefox+Selenium
首先本地安装正常的Firefox浏览器。然后在 https://github.com/mozilla/geckodriver/releases 下载一个无头驱动geckodriver,将之安装在一个环境变量目录里(或新配置)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
from selenium import webdriver option = webdriver.FirefoxOptions() # 1.配置参数 option.set_headless() # 无头模式 option.add_argument("--no-sandbox") option.add_argument("--disable-gpu") option.add_argument("disable-infobars") option.add_argument("--proxy-server=" + proxy) # 代理 协议://host:port 如 "http://www.baidu.com" # browser = webdriver.Firefox(executable_path="geckodriver路径", options=option) browser = webdriver.Firefox(options=option) # 2.创建浏览器 browser.get("https://www.baidu.com/") html = browser.page_source browser.close() browser.quit() |
4.Pyppeteer
selenium的保护机制不允许跨域cookies保存。以及登录的时候必须先打开网页,然后加载cookies,再刷新,这种方式很不友好。并且有些网页会检测到是否是使用了selenium。
Puppeteer是Chrome开发团队在2017年发布的一个Node.js包,用来模拟Chrome浏览器,可以无头方式运行,https://github.com/puppeteer/puppeteer。
Pyppeteer是Puppeteer的Python移植,API跟JavaScript版本基本一致。
pip3 install pyppeteer -i https://pypi.douban.com/simple/
Pyppeteer通常和异步库asyncio合作使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import asyncio from pyppeteer import launch async def main(): browser = await launch() # 启动浏览器 page = await browser.newPage() # 打开新页面tab await page.goto('https://www.baidu.com') # 访问网址 await page.screenshot({'path': 'baidu_screenshot.png'}) # 截图。/VSCode的当前项目目录/图片.png await browser.close() # 关闭浏览器 asyncio.run(main()) # 如果出现如下错误,是因为首次使用pyppeteer,会下载chromium插件: # raise MaxRetryError(_pool, url, error or ResponseError(cause)) # urllib3.exceptions.MaxRetryError: # HTTPSConnectionPool(host='storage.googleapis.com', port=443): # Max retries exceeded with url: /chromium-browser-snapshots/Mac/588429/chrome-mac.zip ( # Caused by SSLError( # SSLCertVerificationError( # 1, # '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: \ # unable to get local issuer certificate (_ssl.c:1056)'))) # 1. # 从 pyppeteer 库里找到chromium_downloader.py文件, # /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyppeteer/chromium_downloader.py # 2. # 替换里面 # DEFAULT_DOWNLOAD_HOST = 'https://storage.googleapis.com' 为 # DEFAULT_DOWNLOAD_HOST = 'http://storage.googleapis.com' |
其它动态抓取页面的技术:
dryscrape、PythonWebkit、PyWebKitGit、Pygt、pyjamas、Playwright,核心是开源的浏览器引擎WebKit。DrissionPage。
-end
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/2709.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设