Reputation: 935
I am trying to access a website that requires cookies. Using urllib2 and cookielib I am able to get a response from the site. The HTML printout informs me that I am not getting access with the line:
<h2>Cookies Disabled</h2>
<p> class="share-prompt"><strong>Cookies must be enabled.</strong></p>
I cannot understand where I am going wrong. Code below:
import urllib2, cookielib
cookieJar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.ProxyHandler({'http':"http://216.208.156.69:3128"}),urllib2.HTTPCookieProcessor(cookieJar))
request = urllib2.Request("[website]")
response = opener.open(request)
print response.read()
Can anyone see where I have gone wrong?
Cheers,
Upvotes: 1
Views: 3077
Reputation: 42465
The code looks good. For example the output from this
import urllib, urllib2, cookielib
cookieJar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar))
params = urllib.urlencode({'cookie_name': 'cookie_value'})
request = urllib2.Request('http://httpbin.org/cookies/set?' + params)
opener.open(request)
request = urllib2.Request('http://httpbin.org/cookies')
response = opener.open(request)
print response.read()
is
{
"cookies": {
"cookie_name": "cookie_value"
}
}
Without showing us the url you use not much can be done.
Upvotes: 2