七、經過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("登陸失敗!")
相關文章
相關標籤/搜索