请亲自试试:)!
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>
我想下载网页的实际来源。
答案 0 :(得分:3)
失败的原因是因为http://www.windowsphone.com尝试设置一个cookie,在https://login.live.com上检查该cookie会创建另一个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'
在你的shell中运行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中获取该页面的内容。
答案 1 :(得分:2)
Flesk真的有这个答案(+1)。
调试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状态代码,从而提示“浏览器”打开位置标头中传递的URL。
Netcat是一个很好的调试和跟踪各种网络通信的工具,当我想深入研究HTTP协议时,它对我帮助很大。