requests模块的cookie处理
from CodeClass import YDMHttp
import requests
from lxml import etree
def getCodeText(imgPath,codeType):
username = 'bobo328410948'
password = 'bobo328410948'
appid = 6003
appkey = '1f4b564483ae5c907a1d34f8e2f2776c'
filename = imgPath
codetype = codeType
timeout = 20
result = None
if (username == 'username'):
print('请设置好相关参数再测试')
else:
yundama = YDMHttp(username, password, appid, appkey)
uid = yundama.login();
print('uid: %s' % uid)
balance = yundama.balance();
print('balance: %s' % balance)
cid, result = yundama.decode(filename, codetype, timeout);
print('cid: %s, result: %s' % (cid, result))
return result
session = requests.Session()
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
}
url = 'http://www.renren.com/SysHome.do'
page_text = requests.get(url=url,headers=headers).text
tree = etree.HTML(page_text)
code_img_src = tree.xpath('//*[@id="verifyPic_login"]/@src')[0]
code_img_data = requests.get(url=code_img_src,headers=headers).content
with open('./code.jpg','wb') as fp:
fp.write(code_img_data)
result = getCodeText('code.jpg',1000)
login_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2019431046983'
data = {
'email': 'www.zhangbowudi@qq.com',
'icode': result,
'origURL': 'http://www.renren.com/home',
'domain': 'renren.com',
'key_id': '1',
'captcha_type': 'web_login',
'password': '06768edabba49f5f6b762240b311ae5bfa4bcce70627231dd1f08b9c7c6f4375',
'rkey': '3d1f9abdaae1f018a49d38069fe743c8',
'f':'',
}
response = session.post(url=login_url,headers=headers,data=data)
print(response.status_code)
detail_url = 'http://www.renren.com/289676607/profile'
detail_page_text = session.get(url=detail_url,headers=headers).text
with open('bobo.html','w',encoding='utf-8') as fp:
fp.write(detail_page_text)