2021-07-07
这里使用了小工具cookiejar来源于cookielib包。这个小工具能够自动的存储已经获取的cookie值(即response头部的set-cookie值),并在下一次访问时添加上这些cookie值。当然,我们也能够自己增删改查cookiejar中的内容,使用起来很方便。否则我们需要自己动手写处理cookie的代码,非常浪费时间。
def login_third_step(self):
sent_url = 'https://www.facebook.com'
request = urllib2.Request(url=sent_url,headers=self.facebook_header)
content=self.opener.open(request)
print '-------------------------------------------'
print content.read()
print '-------------------------------------------'</span>
def login_third_step(self):
sent_url = 'https://www.facebook.com'
request = urllib2.Request(url=sent_url,headers=self.facebook_header)
content=self.opener.open(request)
# print content.read()
tmp_html = content.read()
#查找datr
reg = r'"_js_datr","([A-Za-z0-9]*)"'
m = re.compile(reg)
search = re.search(m,tmp_html)
datr = ''
if search:
datr = search.group(1)
print '-------------------------------------------'
print 'datr: ',datr
self.cj.set_cookie(cookielib.Cookie(
version=0,
name='datr',
value=datr,
port=None,
port_specified=False,
domain=".facebook.com",
domain_specified=True,
domain_initial_dot=False,
path="/",
path_specified=True,
secure=False,
expires=None,
discard=False,
comment=None,
comment_url=None,
rest=None
))
def login_fourth_step(self):
sent_url = 'https://www.facebook.com'
request = urllib2.Request(url=sent_url,headers=self.facebook_header)
content=self.opener.open(request)
print '-------------------------------------------'
print ' getting html '
# print content.read()
self.html = content.read()
print '-------------------------------------------'