Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set cookies using Python urlopen?

I am trying to fetch an html site using Python urlopen.
I am getting this error:

HTTPError: HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop

The code:

from urllib2 import Request
request = Request(url)
response = urlopen(request)

I understand that the server redirects to another URL and that it is looking for a cookie.
How do I set the cookie it is looking for so I can read the html?

like image 755
yossi Avatar asked Jan 31 '26 22:01

yossi


1 Answers

Here's an example from Python documentation, adjusted to your code:

import cookielib, urllib2
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
request = urllib2.Request(url)
response = opener.open(request)
like image 69
DzinX Avatar answered Feb 02 '26 11:02

DzinX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!