【问题标题】:How to retrieve redirect url given in window.location如何检索 window.location 中给出的重定向 url
【发布时间】:2015-04-28 18:43:20
【问题描述】:

我正在尝试使用 Python 制作爬虫。我正在使用 beautifulsoup 和 requests 库,并且需要给定网站的一组 URL。但是,在某个部分,存在重定向,当我打印 response.text 即页面内容时,我得到以下几行

<script>
<!--
window.location = "redirect_URL/index.php"
-->
</script>

如何检索此 url 以便抓取此 url

【问题讨论】:

    标签: python beautifulsoup web-crawler python-requests url-redirection


    【解决方案1】:

    正则表达式怎么样

    您只需检查response.text 是否发生重定向(python):

    regex= /window\.location\s*=\s*\"([^"]+)\"/
    var occurance = regex.exec(responce.text)
    if (occurance[1]) 
        print occurance[1];     
    

    the demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      • 2018-05-17
      • 2019-12-11
      • 2018-03-15
      • 2023-04-01
      • 2014-01-15
      • 1970-01-01
      相关资源
      最近更新 更多