在CentOS 6.4上使用Python 2.6.6
import urllib
#url = 'http://www.google.com.hk' #ok
#url = 'http://clients1.google.com.hk' #ok
#url = 'http://clients1.google.com.hk/complete/search' #ok (blank)
url = 'http://clients1.google.com.hk/complete/search?output=toolbar&hl=zh-CN&q=abc' #fails
print url
page = urllib.urlopen(url).read()
print page
使用前三个URL,代码可以正常工作。但是对于第四个URL,Python会返回302错误:
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://clients1.google.com.hk/complete/search?output=toolbar&hl=zh-CN&q=abc">here</A>.
</BODY></HTML>
我的代码中的URL与它告诉我使用的URL相同:
My URL: http://clients1.google.com.hk/complete/search?output=toolbar&hl=zh-CN&q=abc
Its URL: http://clients1.google.com.hk/complete/search?output=toolbar&hl=zh-CN&q=abc
Google说URL已移动,但URL都是相同的。有什么想法吗?
更新:所有的URL在浏览器中都可以正常工作。但在Python命令行中,第4个URL会返回302状态码。