Urllib2 移动用户代理
如果我使用 Urllib2 使用以下命令打开 url:
import urllib
import urllib2
url = 'http://www.bbc.co.uk'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {}
headers = { 'User-Agent' : user_agent }
data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()
一切正常
但我想要移动版本,所以我将用户代理设置为:
user_agent = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6533.18.5'
这是我的 iphone 在进入测试页面并读取其标题时返回的结果
但是如果我在用户代理设置为这个 urllib 的情况下运行上面的代码,这让我感到惊讶,并且似乎遵循了无限期的 302 重定向循环,当我在 iPhone 上访问该网站时,这种情况不会发生。
urllib2 返回一大堆调试信息,表明它正在跟踪大量 302,最后:
urllib2.HTTPError: HTTP Error 301: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
Moved Permanently
任何想法将不胜感激。
If I use Urllib2 to open a url using this:
import urllib
import urllib2
url = 'http://www.bbc.co.uk'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {}
headers = { 'User-Agent' : user_agent }
data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()
It all works fine
But I want the mobile version so I set the user-agent to:
user_agent = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6533.18.5'
Which is what my iphone comes back with when go to a test page and read its headers
However if I run the above code with the user agent set to this urllib freaks out and seems to follow an indefinite 302 redirection loop which doesn't occur when I visit the site on my iphone.
urllib2 comes back with a whole heap of debug info showing that it is following lots of 302's and then finally:
urllib2.HTTPError: HTTP Error 301: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
Moved Permanently
Any ideas would be gratefully received.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是重定向您的请求的响应。
尝试这个库来帮助您处理重定向网址:
http://pypi.python.org/ pypi/requests/0.7.3
或者
http://wwwsearch.sourceforge.net/mechanize/
Your problem is redirect responses of your request.
Try this lib to help you to handle redirect urls:
http://pypi.python.org/pypi/requests/0.7.3
or
http://wwwsearch.sourceforge.net/mechanize/