开发者

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.exam开发者_如何学JAVAple.com/home.php -o home.html

Thank you.

Goon 


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)


have a look at mechanize.

the home page shows an example using cookies.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜