开发者

Difficulty with python urllib2 form-sending: website says I don't have cookies enabled though I should

开发者 https://www.devze.com 2023-03-13 05:32 出处:网络
So, I\'m trying to submit a form with urllib/urllib2 which should set some a cookie to log me in. However, the website seems convinced that \"cookies are not enabled\" though I\'ve told urllib to proc

So, I'm trying to submit a form with urllib/urllib2 which should set some a cookie to log me in. However, the website seems convinced that "cookies are not enabled" though I've told urllib to process cookies.

This is what my code looks like:

opener = urllib2.build_opener( urllib2.HTTPCookieProcessor() )
urllib2.install_opener( opener )
params = urllib.urlencode( { 'username': 'user', 'password': 'pass' } )

f = opener.open( 'http://example.com/login/',  params )
data = f.read()
print data # Returns a webpage which shows "You need to enable cookies!"
f.close()

What am I m开发者_StackOverflow中文版issing? I know I've used a similar recipe before for logging in.

Edit: On examining the headers that get sent when I fill the form out myself, I think I may be having the same problem asked (but not answered) here (the form responds with a 302 redirect). Hum.


import urllib2,urllib,cookielib

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener( opener )
params = urllib.urlencode( { 'username': 'user', 'password': 'pass' } )

f = opener.open( 'http://example.com/login/',  params )
data = f.read()
print data 
f.close()

try this.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号