11.获取动态接口

import requests
from bs4 import BeautifulSoup

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'
}

# 登录页url
login_page = "http://bbs.chinaunix.net/member.php?mod=logging&action=login&logsubmit=yes"
# 访问登录页
s = requests.Session()
page_html = s.get(login_page,headers=headers)
soup = BeautifulSoup(page_html.text,"lxml")
# formhash、action是动态生成的
action = soup.select("form.cl")[0].get("action")
formhash = soup.select("[name='formhash']")[0].get("value")

# 登录接口需要动态获取
login_url = "http://bbs.chinaunix.net/" + action

# 登录
# 请求体
data = {
    'formhash':formhash,
    'referer':'http://bbs.chinaunix.net/./',
    'username':'MrFan666',
    'password':'f12345678',
    'loginsubmit':'true',
    'return_type':''
}

r = s.post(url=login_url,headers=headers,data=data)
print(r.text)





你可能感兴趣的:(11.获取动态接口)