[英]Python - urllib2 & cookielib
I am trying to open the following website and retrieve the initial cookie and use it for the second url-open BUT if you run the following code it outputs 2 different cookies. 我正在尝试打开以下网站并检索初始cookie并将其用于第二个url-open但如果您运行以下代码则输出2个不同的cookie。 How do I use the initial cookie for the second url-open?
如何在第二个url-open中使用初始cookie?
import cookielib, urllib2
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
home = opener.open('https://www.idcourts.us/repository/start.do')
print cj
search = opener.open('https://www.idcourts.us/repository/partySearch.do')
print cj
Output shows 2 different cookies every time as you can see: 每次输出显示2个不同的cookie,如您所见:
<cookielib.CookieJar[<Cookie JSESSIONID=0DEEE8331DE7D0DFDC22E860E065085F for www.idcourts.us/repository>]>
<cookielib.CookieJar[<Cookie JSESSIONID=E01C2BE8323632A32DA467F8A9B22A51 for www.idcourts.us/repository>]>
This is not a problem with urllib. 这不是urllib的问题。 That site does some funky stuff.
该网站做了一些时髦的东西。 You need to request a couple of stylesheets for it to validate your session id:
您需要请求几个样式表来验证您的会话ID:
import cookielib, urllib2
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
# default User-Agent ('Python-urllib/2.6') will *not* work
opener.addheaders = [
('User-Agent', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11'),
]
stylesheets = [
'https://www.idcourts.us/repository/css/id_style.css',
'https://www.idcourts.us/repository/css/id_print.css',
]
home = opener.open('https://www.idcourts.us/repository/start.do')
print cj
sessid = cj._cookies['www.idcourts.us']['/repository']['JSESSIONID'].value
# Note the +=
opener.addheaders += [
('Referer', 'https://www.idcourts.us/repository/start.do'),
]
for st in stylesheets:
# da trick
opener.open(st+';jsessionid='+sessid)
search = opener.open('https://www.idcourts.us/repository/partySearch.do')
print cj
# perhaps need to keep updating the referer...
Not an actual answer (but far too long for a comment); 不是一个真正的答案(但是评论的时间太长了); possibly useful to anyone else trying to answer this.
可能对其他试图回答此问题的人有用。
Despite my best attempts, I can't figure this out. 尽管我做了最好的尝试,但我无法弄清楚这一点。
Looking in Firebug, the cookie seems to remain the same (works properly) for Firefox. 看看Firebug,Firefox似乎保持不变(工作正常)。
I added urllib2.HTTPSHandler(debuglevel=1)
to debug what headers Python is sending, and it does appear to resend the cookie. 我添加了
urllib2.HTTPSHandler(debuglevel=1)
来调试Python发送的头文件,它似乎重新发送cookie。
I also added all the Firefox request headers to see if that would help (it didn't): 我还添加了所有Firefox请求标头,看看是否有帮助(它没有):
opener.addheaders = [
('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13'),
..
]
My test code: 我的测试代码:
import cookielib, urllib2
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), urllib2.HTTPSHandler(debuglevel=1))
opener.addheaders = [
('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13'),
('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
('Accept-Language', 'en-gb,en;q=0.5'),
('Accept-Encoding', 'gzip,deflate'),
('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'),
('Keep-Alive', '115'),
('Connection', 'keep-alive'),
('Cache-Control', 'max-age=0'),
('Referer', 'https://www.idcourts.us/repository/partySearch.do'),
]
home = opener.open('https://www.idcourts.us/repository/start.do')
print cj
search = opener.open('https://www.idcourts.us/repository/partySearch.do')
print cj
I feel like I'm missing something obvious. 我觉得我错过了一些明显的东西。
我认为,这是服务器的问题,它为每个请求设置一个新的cookie。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.