【问题标题】:get html with python request and redirection使用 python 请求和重定向获取 html
【发布时间】:2020-02-25 09:47:06
【问题描述】:

我要爬取页面 url = 'https://e-justice.europa.eu/searchBris.do' 来提交我自己的信息。我使用 requests.get(url) 来获取页面的 html 内容。

requests.get(url)

但我得到重定向页面作为请求的输出,如下所示:

\n\n\n\n\n\n\n<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n<html lang="en">\n    <head>\n    <title>Find a company</title>\n    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">\n\n    <script> \n        top.location.reload();\n    </script>\n\n    <noscript><meta http-equiv="refresh" content="0;url=https://e-justice.europa.eu/searchBris.do"/></noscript>\n    </head>\n    <body>\n        <h1>Redirecting...</h1>\n    </body>\n</html>

我也测试了allow_redirect选项以及session.get()和session.post()方案如下,但是重定向的输出依然存在,并且url的html的访问被拒绝了。

requests.get(url, allow_redirects=True)
session.get(url, allow_redirects=True)
requests.post(url, allow_redirects=True)
session.post(url, allow_redirects=True)

有没有办法获取原始url的内容?

【问题讨论】:

    标签: python-3.x beautifulsoup request web-crawler


    【解决方案1】:

    尽管它声称,该页面没有使用传统的重定向,您可以检查:

    url = 'https://e-justice.europa.eu/searchBris.do'
    r = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})  # spoof UA just in case
    r.is_redirect
    > False
    

    &lt;noscript&gt; 标记中发生了什么。该站点是使用客户端 Javascript 呈现的,因此您不能使用 HTML 抓取工具(没有浏览器)来执行此操作。

    您可以尝试使用带有 Selenium 的无头浏览器。

    【讨论】:

      【解决方案2】:

      我尝试用phantomjs抓取本站的html,成功了。

      driver = webdriver.PhantomJS()
      driver.get(url)
      html = str(driver.page_source)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-04
        • 1970-01-01
        • 2016-04-03
        • 2016-11-28
        • 2019-01-18
        • 1970-01-01
        相关资源
        最近更新 更多