user789904
user789904

Reputation: 21

How can I use a cookie to enter and download a web page in Python

How do I download a webpage using a cookie in Python. I am successfully using curl to do the job, but I would like to use a Python equivalent. I think pycurl should do it but I cant find an example that allows the user to input just a cookie. The curl bash line I am using is:-

curl -b "*cookie*" http://www.example.com/home.php -o home.html

Thank you.
Goon 

Upvotes: 2

Views: 616

Answers (2)

Corey Goldberg
Corey Goldberg

Reputation: 60604

have a look at mechanize.

the home page shows an example using cookies.

Upvotes: 0

Zach Kelling
Zach Kelling

Reputation: 53819

Use cookielib and Cookie modules in the standard library to create/store cookies, to actually make requests I recommend requests over urllib2 in the standard library.

import Cookie, cookielib, requests
# create cookiejar
cj = cookielib.CookieJar()
# create cookie
ck = Cookie.SimpleCookie()
ck.name = 'value'
ck.expires = 0
ck.path = '/'
ck.domain = 'www.example.com'
# add cookie to cookiejar
cj.set_cookie(ck)
# use cookiejar to make request
response = requests.get('http://www.example.com/home.php', cookies=cj)

Upvotes: 2

Related Questions