7、通过requests的session请求登录示例,并通过BeautifulSoup解析

# Author:toloy
import requests
import json
from bs4 import BeautifulSoup

# 创建session对象
sess = requests.session()
# 登录的url
url = "http://www.dfenqi.cn/User/Login"
# 请求头
headers = {
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN",
    "Cache-Control": "no-cache",
    "Connection": "Keep-Alive",
    "Content-Type": "application/json; charset=utf-8",
    "Host": "www.dfenqi.cn",
    "Origin": "http://www.dfenqi.cn",
    "Referer": "http://www.dfenqi.cn/",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299",
    "X-Requested-With": "XMLHttpRequest"
}
# 请求数据
data = {
    "username":"13917475176",
    "password":"9688856b810f347416248a40236af572",
    "remember":"false",
    "AccountType":0,
    "ReturnUrl":None
}
# 返回响应文件
response = sess.post(url,data = json.dumps(data),headers = headers)
html = response.text
# 转换成json对象
result_json = json.loads(html)
# 如果登录成功,则请求首页
if result_json["Success"] == True:
    response = sess.get("http://www.dfenqi.cn/Product/Index",headers = headers)
    homePageHtml = response.text
    # 构建bs处理对象
    bs = BeautifulSoup(homePageHtml,"lxml")
    # bs可通过类似css选择器一样,按层级来选择
    menuList = bs.select(".zuonav .lanjiantou h3 a")
    for menu in menuList:
        print(menu.string)
else:
    print("登录失败!")
原文地址:https://www.cnblogs.com/toloy/p/8621961.html