如何从Selenium Webdriver Firefox中的driver.page_source获取HTTP请求的原始JSON响应

时间:2019-01-03 14:38:45

标签: python json selenium selenium-webdriver httpresponse

如果我浏览到https://httpbin.org/headers,我希望得到以下JSON响应:

{
  "headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
    "Accept-Encoding": "gzip, deflate, br", 
    "Accept-Language": "en-US,en;q=0.5", 
    "Connection": "close", 
    "Host": "httpbin.org", 
    "Upgrade-Insecure-Requests": "1", 
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0"
  }
}

但是,如果我使用硒

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)

url = 'https://httpbin.org/headers'
driver.get(url)
print(driver.page_source)
driver.close()

我明白了

<html platform="linux" class="theme-light" dir="ltr"><head><meta http-equiv="Content-Security-Policy" content="default-src 'none' ; script-src resource:; "><link rel="stylesheet" type="text/css" href="resource://devtools-client-jsonview/css/main.css"><script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="viewer-config" src="resource://devtools-client-jsonview/viewer-config.js"></script><script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="json-viewer" src="resource://devtools-client-jsonview/json-viewer.js"></script></head><body><div id="content"><div id="json">{
  "headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
    "Accept-Encoding": "gzip, deflate, br", 
    "Accept-Language": "en-US,en;q=0.5", 
    "Connection": "close", 
    "Host": "httpbin.org", 
    "Upgrade-Insecure-Requests": "1", 
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0"
  }
}
</div></div><script src="resource://devtools-client-jsonview/lib/require.js" data-main="resource://devtools-client-jsonview/viewer-config.js"></script></body></html>

HTML标记来自哪里?如何从driver.page_source获取HTTP请求的原始JSON响应?

3 个答案:

答案 0 :(得分:2)

除了原始的JSON响应外,driver.page_source还包含HTML,以“漂亮地打印”浏览器中的响应。如果使用Firefox DOM和Style Inspector在浏览器中查看JSON响应的源,您将得到相同的结果。

要获取原始JSON响应,您可以照常浏览HTML元素:

print(driver.find_element_by_xpath("//div[@id='json']").text)

答案 1 :(得分:0)

在您的网址中使用“ view-source:”参数

简单模式:

示例:

url = 'view-source:https://httpbin.org/headers'
driver.get(url)
content = driver.page_source
print(content)

输出:

'<html><head><meta name="viewport" content="width=device-width"><title>https://httpbin.org/headers</title><link rel="stylesheet" type="text/css" href="resource://content-accessible/viewsource.css"></head><body id="viewsource" class="highlight" style="-moz-tab-size: 4"><pre>{\n  "headers": {\n    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", \n    "Accept-Encoding": "gzip, deflate, br", \n    "Accept-Language": "en-US,en;q=0.5", \n    "Host": "httpbin.org", \n    "Upgrade-Insecure-Requests": "1", \n    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0"\n  }\n}\n</pre></body></html>'

最佳模式:(用于JSON)

示例:

url = 'view-source:https://httpbin.org/headers'
driver.get(url)
content = driver.page_source
content = driver.find_element_by_tag_name('pre').text
parsed_json = json.loads(content)
print(parsed_json)

输出:

{'headers': {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'Accept-Encoding': 'gzip, deflate, br',
  'Accept-Language': 'en-US,en;q=0.5',
  'Host': 'httpbin.org',
  'Upgrade-Insecure-Requests': '1',
  'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0'}}

答案 2 :(得分:0)

这篇文章帮助我解决了 firefox 的问题:https://blog.francium.tech/firefox-selenium-disable-json-formatting-cfaf466fd20f

我已将此首选项添加到我的驱动程序工厂:

from selenium.webdriver.firefox.options import Options as FirefoxOptions

    @staticmethod
    def get_firefox_options(headless):
        options = FirefoxOptions()
        options.set_preference('devtools.jsonview.enabled', False)

        if headless:
            options.headless = True

        return options