为什么我无法用Python下载这个网页?
请你自己试试看 :) !
curl http://www.windowsphone.com/en-US/apps?list=free
结果是:
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&checkda=1&ct=1320735308&rver=6.1.6195.0&wp=MBI&wreply=http:%2F%2Fwww.windowsphone.com%2Fen-US%2Fapps%3Flist%3Dfree&lc=1033&id=268289">here</a>.</h2>
</body></html>
或者
def download(source_url):
try:
socket.setdefaulttimeout(10)
agents = ['Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)','Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1)','Microsoft Internet Explorer/4.0b1 (Windows 95)','Opera/8.00 (Windows NT 5.1; U; en)']
ree = urllib2.Request(source_url)
ree.add_header('User-Agent',random.choice(agents))
resp = urllib2.urlopen(ree)
htmlSource = resp.read()
return htmlSource
except Exception, e:
print e
return ""
download('http://www.windowsphone.com/en-US/apps?list=free')
结果是:
<html><head><meta http-equiv="REFRESH" content="0; URL=http://www.windowsphone.com/en-US/apps?list=free"><script type="text/javascript">function OnBack(){}</script></head></html>
我想下载网页的实际源代码。
2 个回答
2
Flesk在这个问题上真的给出了很好的答案(赞一个)。
另一个简单的方法来调试HTTP连接是Netcat,它基本上是一个强大的telnet工具。
假设你想调试一下你的HTTP请求发生了什么:
$ nc www.windowsphone.com 80
GET /en-US/apps?list=free HTTP/1.0
Host: www.windowsphone.com
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
这段代码会把请求头发送到服务器(你需要按两次回车键来发送)。
之后,服务器会给出回应:
HTTP/1.1 302 Found
Location: https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&checkda=1&ct=1320745265&rver=6.1.6195.0&wp=MBI&wreply=http:%2F%2Fwww.windowsphone.com%2Fen-US%2Fapps%3Flist%3Dfree&lc=1033&id=268289
Server: Microsoft-IIS/7.5
Set-Cookie: WPMSLSS=SLSS=1; domain=www.windowsphone.com; path=/; HttpOnly
X-Powered-By: ASP.NET
X-Server: SN2CONXWWBA06
Date: Tue, 08 Nov 2011 09:41:05 GMT
Connection: close
Content-Length: 337
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&checkda=1&ct=1320745265&rver=6.1.6195.0&wp=MBI&wreply=http:%2F%2Fwww.windowsphone.com%2Fen-US%2Fapps%3Flist%3Dfree&lc=1033&id=268289">here</a>.</h2>
</body></html>
服务器返回了302,这个是HTTP状态码,表示需要重定向,因此会提示“浏览器”打开在Location头中提供的URL。
Netcat是一个很棒的工具,可以用来调试和追踪各种网络通信,当我想更深入了解HTTP协议时,它帮了我很多。
3
失败的原因是因为 http://www.windowsphone.com 尝试设置一个cookie,而这个cookie会在 https://login.live.com 被检查。如果检查成功,就会创建另一个cookie,并重定向回windowsphone.com。
你可以看看 http://docs.python.org/library/cookielib.html 这个链接。
如果你想使用curl,可以让它创建一个cookie文件,方法如下:
curl -so /dev/null 'http://www.windowsphone.com/en-US/apps?list=free' -c 'myCookieJar'
在你的命令行中运行 more myCookieJar
,你会看到类似这样的内容:
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.
.www.windowsphone.com TRUE / FALSE 0 WPMSLSS SLSS=1
login.live.com FALSE / FALSE 0 MSPRequ lt=1320738008&co=1&id=268289
运行时(注意在'myCookieJar'前面有个 -b 选项):
curl -so 'windowsphone.html' 'http://www.windowsphone.com/en-US/apps?list=free' -b 'myCookieJar'
这样你就能把网页的内容保存到文件windowsphone.html里,和你在浏览器里看到的一样。