【问题标题】:Scraping Metacritic with urllib to follow redirect使用 urllib 抓取 Metacritic 以遵循重定向
【发布时间】:2014-01-05 19:27:37
【问题描述】:

我正在编写一个 Python 脚本来从 Metacritic 中抓取信息。它适用于大多数电影,但 Metacritic 重定向的电影存在问题。

例如,在电影列表中,Metacritic 提供了网址“/movie/red-riding-in-the-year-of-our-lord-1983”,但当您点击该网址时,它会将您带到“/movie” /红骑三部曲”。我需要 urllib 来获取它最终到达的最终 URL 的 HTML。

【问题讨论】:

    标签: python web-scraping screen-scraping urllib


    【解决方案1】:

    我最终使用了请求模块。 (http://docs.python-requests.org/en/latest/) 这是请求的代码和保存最终 url 的行。

    response = requests.get(url)
    newUrl = response.url
    

    【讨论】:

    • 我会尽快。谢谢
    【解决方案2】:

    尝试使用,

    import urllib.request
    urllib.request.FancyURLopener().open_http("your url")
    

    【讨论】:

    • 我收到 AttributeError: 'module' object has no attribute 'request' on that line
    • @connor 查看我对您缺少的 import 语句的编辑。
    • "ImportError: No module named request" 我在 python 2.7 上,所以它可能没有那个模块
    猜你喜欢
    • 1970-01-01
    • 2015-03-01
    • 2015-06-07
    • 2018-03-31
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    相关资源
    最近更新 更多