Ale*_*yev 5 python cookies urllib2
例如,我有饼干
my_cookies = {'name': 'Albert', 'uid': '654897897564'}
Run Code Online (Sandbox Code Playgroud)
我想打开http://website.com页面
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
opener.addheaders.append(('User-agent', 'Mozilla/5.0 (compatible)'))
opener.open('http://website.com').read()
Run Code Online (Sandbox Code Playgroud)
我如何使用预定义的Cookie执行此操作?
您只需要几个步骤:
import urllib2
import cookielib
cp = urllib2.HTTPCookieProcessor()
cj = cp.cookiejar
# see cookielib.Cookie documentation for options description
cj.set_cookie(cookielib.Cookie(0, 'a_cookie', 'a_value',
'80', False, 'domain', True, False, '/path',
True, False, None, False, None, None, None))
opener = urllib2.build_opener(urllib2.HTTPHandler(),
cp)
opener.addheaders.append(('User-agent', 'Mozilla/5.0 (compatible)'))
opener.open('http://website.com').read()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2381 次 |
| 最近记录: |