Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cookie问题和发送POST/GET以获取Python中的te web内容_Python_Post_Cookies_Get - Fatal编程技术网

Cookie问题和发送POST/GET以获取Python中的te web内容

Cookie问题和发送POST/GET以获取Python中的te web内容,python,post,cookies,get,Python,Post,Cookies,Get,可能重复: 我想从一个以某种不寻常的方式处理cookie的服务下载整个网页源代码。我写了一个脚本,它实际上可以工作,看起来很好,但是在某些时候它返回了这样的错误: urllib2.HTTPError:HTTP错误302:HTTP服务器返回一个重定向错误,该错误将导致无限循环。 最后一条30倍的错误消息是: 找到 我的脚本在循环中工作,并将链接更改为我感兴趣下载的子页面 我得到了一个cookie,发送了一个数据包,然后我就可以访问porper链接,然后下载html 脚本如下所示: import

可能重复:

我想从一个以某种不寻常的方式处理cookie的服务下载整个网页源代码。我写了一个脚本,它实际上可以工作,看起来很好,但是在某些时候它返回了这样的错误:

urllib2.HTTPError:HTTP错误302:HTTP服务器返回一个重定向错误,该错误将导致无限循环。
最后一条30倍的错误消息是:
找到

我的脚本在循环中工作,并将链接更改为我感兴趣下载的子页面

我得到了一个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()
import http.cookiejar, urllib.request
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener( urllib.request.HTTPCookieProcessor(cj) )
r = opener.open(url)  # now cookies are stored in cj
r1 = urllib.request(url, data)  #TypeError: POST data should be bytes or an iterable of bytes. It cannot be str.
r2 = opener.open(url2)
print( r2.read() )
我一直在读某个ab cookiejar/cookielib,因为使用这个库我应该消除上面提到的这个错误,但是我不知道如何重新生成我的代码以供使用:
http.cookiejar,urllib.request

我试过这样的东西:

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()
import http.cookiejar, urllib.request
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener( urllib.request.HTTPCookieProcessor(cj) )
r = opener.open(url)  # now cookies are stored in cj
r1 = urllib.request(url, data)  #TypeError: POST data should be bytes or an iterable of bytes. It cannot be str.
r2 = opener.open(url2)
print( r2.read() )
但这不是我的第一个剧本


抱歉,我的英语不是本地人。

@Piotr Dobrogost谢谢你的链接,它解决了这个问题

通过使用
data=b“string”
而不是
data=“string”

我仍然有一些问题,由于移植到python3,但问题将被关闭