import requests
from lxml import etree
# 使用代理
proxy = {
'http': '219.149.59.250:9797'
}
# 创建一个会话
s = requests.Session()
# 定制头信息
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36',
}
url = 'http://bbs.chinaunix.net/member.php?mod=logging&action=login&logsubmit=yes'
# 发送get请求
r = s.get(url=url, headers=headers, proxies=proxy)
# 查找formhash值的xpath
formhash_xpath = '//div[@class="form"]//input[@name="formhash"]/@value'
# 创建xpath tree对象
html_tree = etree.HTML(r.text)
# 查找得到formhash值
formhash = html_tree.xpath(formhash_xpath)
# print(formhash)
# 发送post请求进行登录
post_url = 'http://bbs.chinaunix.net/member.php?mod=logging&action=login&loginsubmit=yes&loginhash=LFpXy'
# 抓包分析得到的参数
data = {
'formhash': formhash,
'referer': 'http://bbs.chinaunix.net/',
'username': '17312345678', # 账号
'password': '123456', # 密码
'loginsubmit': 'true',
'return_type': ''
}
headers_post = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded',
'Host': 'bbs.chinaunix.net',
'Origin': 'http://bbs.chinaunix.net',
'Referer': 'http://bbs.chinaunix.net/member.php?mod=logging&action=login&logsubmit=yes',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36',
}
r = s.post(url=post_url, data=data, headers=headers_post, proxies=proxy)
r.encoding = 'gbk'
with open('chinaunix.html', 'w') as fp:
fp.write(r.text)
注意:代码是否执行成功具有时效性