Possible Duplicate:
How to use Python to login to a webpage and retrieve cookies for later usage?
我想从一个处理cookies的服务下载整个网页的源代码。我写了一个脚本,实际上很好,但在某些时候它返回了这样的错误:
urllib2.HTTPError: HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
Found
我的脚本在循环中工作,并将链接更改到我感兴趣下载的子页面的链接。在
我得到了一个cookie,发送了一个数据包,然后我就可以访问porper链接,然后下载html。在
脚本如下:
import urllib2
data = 'some_string'
url = "http://example/index.php"
url2 = "http://example/source"
req1 = urllib2.Request(url)
response = urllib2.urlopen(req1)
cookie = response.info().getheader('Set-Cookie')
## Use the cookie is subsequent requests
req2 = urllib2.Request(url, data)
req2.add_header('cookie', cookie)
response = urllib2.urlopen(req2)
## reuse again
req3 = urllib2.Request(url2)
req3.add_header('cookie', cookie)
response = urllib2.urlopen(req3)
html = response.read()
我一直在读一篇关于cookiejar/cookielib的文章,因为使用这个库我应该消除上面提到的这个错误,但是我不知道如何重新生成代码以供使用:http.cookiejar, urllib.request
我试过这样的事:
^{pr2}$但这不是我的第一个剧本。在
抱歉,我的英语不是本地人。在
@Piotr Dobrogost谢谢你的链接,它解决了问题。在
使用
data=b"string"
而不是data="string"
解决了TypeError由于移植到python3,我还有一些问题,但这个问题将被解决。在
相关问题 更多 >
编程相关推荐