Selenium抓取
下载Chrome WebDriver
https://chromedriver.chromium.org/
下载Selenium Server,用的是版本4,3不再维护了
https://www.selenium.dev/downloads/
把2个文件放一个目录后,再运行:
java -jar .\selenium-server-4.0.0-beta-1.jar standalone
访问:http://IP:4444/ 有控制台
安装php-webdriver
php composer.phar require php-webdriver/webdriver
php-webdriver相关参考
https://github.com/php-webdriver/php-webdriver
有的网站判断了是否通过navigator.webdriver来判断是否是通过模拟抓取的,这种情况下需要用到中间人的方式去把返回的javascript替换掉,让他判断不成功。
目前用到的是mitmdump,可以写一个python文件,把返回的数据进行替换:
# coding:utf-8
# modify_response.py
import re
from mitmproxy import ctx
def response(flow):
"""修改应答数据"""
if 'web-login/canvas' in flow.request.url:
# 屏蔽selenium检测
for webdriver_key in ['navigator.webdriver']:
ctx.log.info('Remove"{}"from{}.'.format(webdriver_key,flow.request.url))
flow.response.text=flow.response.text.replace(webdriver_key,'navigator.webdriverabc')
运行:mitmdump.ext -s 脚本.py,Python的版本要是3
如果用这样的话,chrome会出现非法的https,需要在webdriver里加入”ignore-certificate-errors”
还有一个问题是当这样用了之后,就没有办法给浏览器指定代理了,如果要指定的话,只能在mitmdump里指定二级代理,思路见:https://www.jianshu.com/p/dfd1e2753d71
没试过,脚本感觉有问题要修改才行。
def request(flow: http.HTTPFlow) -> None:
if flow.request.method == "CONNECT":
# If the decision is done by domain, one could also modify the server address here.
# We do it after CONNECT here to have the request data available as well.
return
client_ip = flow.client_conn.address[0]
if 'ip.cn' in flow.request.url:
ctx.log.info(flow.request.url)
proxy =("localhost", 8888)
else:
proxy = ("localhost", 3800)
# 这里配置二级代理的ip地址和端口
if flow.live:
flow.live.change_upstream_proxy_server(proxy)
mitmdump –mode upstream:http://default-upstream-proxy.local:8080/ -s ./change_upstream_proxy.py
https://docs.mitmproxy.org/stable/addons-examples/
还有一个方式是用google的puppeteer,是node写的,没有试过。
更多关于webdriver检测的了解见:https://onefine.blog.csdn.net/article/details/88200217