天天看点

python requests 通过post方式获取网页数据

用Firefox浏览器,按住F12

**

post内容的获取

**

python requests 通过post方式获取网页数据

header的获取

python requests 通过post方式获取网页数据
import requests
	from bs4 import BeautifulSoup

    url = "https://www.hkexnews.hk/sdw/search/searchsdw.aspx"
    
	headers = {
    "Host": "www.hkexnews.hk",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
    "Accept-Encoding": "gzip, deflate, br",
    "Content-Type": "application/x-www-form-urlencoded",
    "Content-Length": "325",
    "Connection": "keep-alive",
    "Referer": "https://www.hkexnews.hk/sdw/search/searchsdw.aspx",
    "Cookie": "WT_FPC=id=101.88.230.86-94988256.30695114:lv=1569290281081:ss=1569290281081; TS0161f2e5=017038eb4918d05ce1c9dcca72b0ff72ca80292fce21e1dfe4d1c19d88103fc3843d6f7dc1",
    "Upgrade-Insecure-Requests": "1"
}
    postdata = {
        "__EVENTARGUMENT": "",
        "__EVENTTARGET": "btnSearch",
        "__VIEWSTATE": "/wEPDwULLTIwNTMyMzMwMThkZLiCLeQCG/lBVJcNezUV/J0rsyMr",
        "__VIEWSTATEGENERATOR": "A7B2BBE2",
        "alertMsg": "",
        "sortBy": "shareholding",
        "sortDirection": "desc",
        "today": stoday,
        "txtParticipantID": "",
        "txtParticipantName": "",
        "txtSelPartID": "",
        "txtShareholdingDate": yesterday.strftime('%Y/%m/%d'),
        "txtStockCode": ticker,
        "txtStockName": ""
    }

    s = requests.Session()
    s.headers.update(headers)
    resp = s.post(url, data=postdata)
    soup = BeautifulSoup(resp.text, 'lxml') # 分析数据