python - 使用代理时 headless (headless) Chrome 返回空 HTML

标签 python selenium google-chrome headless

我希望使用 headless (headless)浏览器来抓取一些网站,并且需要使用代理服务器。

我有点迷失,正在寻求帮助。

当我禁用代理时,它每次都能完美运行。

当我禁用 headless (headless)模式时,我会得到一个空的浏览器窗口,但如果我在具有“https://www.whatsmyip.org ”的 URL 栏上按 Enter 键,则会加载页面(使用显示不同 IP 的代理服务器)。

我对其他网站也有同样的错误,不仅仅是whatsmyip.org 出现此结果。

我正在运行 Centos7、Python 3.6 和 Selenium 3.14.0。

我还在运行 Anaconda 的 Windows 机器上进行了尝试,并得到了相同的结果。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.common.proxy import Proxy, ProxyType

my_proxy = "x.x.x.x:xxxx" #I have a real proxy address here
proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': my_proxy,
    'ftpProxy': my_proxy,
    'sslProxy': my_proxy,
    'noProxy': ''
})

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--allow-insecure-localhost')
chrome_options.add_argument('--allow-running-insecure-content')
chrome_options.add_argument("--ignore-ssl-errors");
chrome_options.add_argument("--ignore-certificate-errors");
chrome_options.add_argument("--ssl-protocol=any");        
chrome_options.add_argument('--window-size=800x600')
chrome_options.add_argument('--disable-application-cache')

capabilities = dict(DesiredCapabilities.CHROME)
proxy.add_to_capabilities(capabilities)
capabilities['acceptSslCerts'] = True
capabilities['acceptInsecureCerts'] = True

browser = webdriver.Chrome(executable_path=r'/home/glen/chromedriver', chrome_options=chrome_options, desired_capabilities=capabilities)

browser.get('https://www.whatsmyip.org/')

print(browser.page_source)     

browser.close()

当我运行代码时,我得到以下返回:

<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>

不是网站。

最佳答案

这里有两个问题:

  1. 您需要等待浏览器加载网站。
  2. browser.page_source 不会返回您想要的内容。

第一个问题是通过等待元素出现在 DOM 中来解决的。通常,您会想要刮掉一些东西,这样您就知道如何识别该元素。添加代码以等待该元素存在。

第二个问题是 page_source 不返回当前 DOM,而是返回浏览器加载的初始 HTML。如果此后 JavaScript 修改了页面,您将不会以这种方式看到它。

解决方案是找到 html 元素并请求 outerHtml 属性:

from selenium.webdriver.common.by import By
htmlElement = driver.find_element(By.TAG_NAME, "html")
dom = htmlElement.get_attribute("outerHTML")
print(dom)

有关详细信息,请参阅以下示例:https://www.seleniumhq.org/docs/03_webdriver.jsp#introducing-the-selenium-webdriver-api-by-example

关于python - 使用代理时 headless (headless) Chrome 返回空 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55378054/

相关文章:

javascript - 如何访问 Firefox 扩展的 manifest.json 文件?

Android Webview 自动填充地址、密码、个人信息等信息

python - 为什么删除临时文件时出现 WindowsError?

python - 字符串格式化 - Python

Selenium 试验机

python - svg 的 xpath,无法根据路径属性定位

android - 文件输入 - 在 android 上接受来自相机或图库的图像

Python:使用自动缩放功能自动添加 x-y 边距 (pyplot)

python - 使用flask-wtf queryselectfield进行更具体的SQL查询

c# - BoDi.ObjectContainerException 接口(interface)无法解析 : OpenQA. Selenium.IWebDriver