美文网首页
解决chromedriver + mitmproxy返回www.

解决chromedriver + mitmproxy返回www.

作者: warmi_ | 来源:发表于2018-11-12 14:55 被阅读0次

问题:爬取某网站需要用到 chromedriver + mitmproxy , 但是有个致命的问题就是加入headless和proxy参数后,代理一直会出错,其实就是证书的问题。

解决:根据以上问题,进行了很多搜索,测试。 但是有很多版本的答案其实都是不能用的,问题还是没有得到解决。最终在chromium上找到了一个标准答案。下面的代码转自:https://bugs.chromium.org/p/chromium/issues/detail?id=721739#c60

作者:黑蚂蚁
来源:CSDN
原文:https://blog.csdn.net/weixin_39847926/article/details/82190341
版权声明:本文为博主原创文章,转载请附上博文链接!

from selenium import webdriver
from pyvirtualdisplay import Display
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
 
#_____________________基本设定___________________________
CHROME_DRIVER_PATH = r'/usr/bin/chromedriver' 
PROXY = "http://127.0.0.1:8080"
#_____________________启动参数___________________________
options = webdriver.ChromeOptions()
options.add_argument('--headless')  
options.add_argument('--disable-gpu')  
options.add_argument("window-size=1024,768")  
options.add_argument("--no-sandbox")
 
#_____________________代理参数___________________________
desired_capabilities = options.to_capabilities()
desired_capabilities['acceptSslCerts'] = True
desired_capabilities['acceptInsecureCerts'] = True
desired_capabilities['proxy'] = {
    "httpProxy": PROXY,
    "ftpProxy": PROXY,
    "sslProxy": PROXY,
    "noProxy": None,
    "proxyType": "MANUAL",
    "class": "org.openqa.selenium.Proxy",
    "autodetect": False,
}
#_____________________启动浏览器___________________________
driver = webdriver.Chrome(
    chrome_options=options, 
    executable_path=CHROME_DRIVER_PATH,
    desired_capabilities = desired_capabilities,
                         )
 
for i in range(1):
    driver.get('https://www.iplocation.net')
    contant = driver.page_source
    driver.save_screenshot('hello.png')
    print(contant)
    driver.close()
    driver.quit()
 
mitmdump -p 8080

抄自https://blog.csdn.net/weixin_39847926/article/details/82190341,查侵删
成功
我的是返回<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>

相关文章

网友评论

      本文标题:解决chromedriver + mitmproxy返回www.

      本文链接:https://www.haomeiwen.com/subject/yxhcfqtx.html