【问题标题】:Python follow redirects and then download the page?Python遵循重定向然后下载页面?
【发布时间】:2012-02-08 07:08:07
【问题描述】:

我有以下 python 脚本,它运行良好。

import urllib2

url = 'http://abc.com' # write the url here

usock = urllib2.urlopen(url)
data = usock.read()
usock.close()

print data

但是,我给它的一些 URL 可能会重定向 2 次或更多次。在加载数据之前,如何让 python 等待重定向完成。 例如,当使用上面的代码时

http://www.google.com/search?hl=en&q=KEYWORD&btnI=1

这相当于在谷歌搜索中点击我的幸运按钮,我得到:

>>> url = 'http://www.google.com/search?hl=en&q=KEYWORD&btnI=1'
>>> usick = urllib2.urlopen(url)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 400, in open
    response = meth(req, response)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden
>>> 

我已经尝试过(网址、数据、超时),但是我不确定该放什么。

编辑: 我实际上发现如果我不重定向并且只使用第一个链接的标题,我可以获取下一个重定向的位置并将其用作我的最终链接

【问题讨论】:

  • 你熟悉HTTPRedirectHandler
  • 我不是。刚google了一下。我可以看到如何让它不跟随。但是,我不能强迫它跟随
  • 我知道这已经有一段时间了,但是你能深入挖掘一下内存库并告诉我你是如何解决这个问题的吗?谢谢!

标签: python html web-scraping


【解决方案1】:

使用具有更好的 API 来控制重定向处理的 Requests 库可能会更好:

https://requests.readthedocs.io/en/master/user/quickstart/#redirection-and-history

请求:

https://pypi.org/project/requests/(人类的 urllib 替代品)

【讨论】:

  • @user1048138:您介意告诉我们您找到解决问题的方法吗?
  • 这个功能让我大吃一惊。此外,对于其他请求(例如 HEAD),请务必注意,您必须将 allow_redirects 设置为 True 才能正常工作。
  • 虽然指针正确,但这并不能立即解决所讨论的问题。
  • 第一个链接坏了
【解决方案2】:

使用requests 作为其他答案状态,这是一个示例。重定向将位于r.url。在下面的示例中,http 被重定向到 https

对于头部:

In [1]:     import requests
   ...:     r = requests.head('http://github.com', allow_redirects=True)
   ...:     r.url

Out[1]: 'https://github.com/'

对于 GET:

In [1]:     import requests
   ...:     r = requests.get('http://github.com')
   ...:     r.url

Out[1]: 'https://github.com/'

注意对于 HEAD,您必须指定 allow_redirects,如果不这样做,您可以在标题中获取它,但不建议这样做。

In [1]: import requests

In [2]: r = requests.head('http://github.com')

In [3]: r.headers.get('location')
Out[3]: 'https://github.com/'

下载页面,您需要 GET,然后您可以使用 r.content 访问该页面

【讨论】:

  • 为什么不建议通过标题获取它?
  • 我知道这不是很久以前的事,但感觉就是这样,我想我做了一个验证,发现它不那么可靠,它也可能在文档中这么说。如果您进行验证,请告诉我您发现了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 2014-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多