过程:
点击底部的第3页,第5页,网页刷新了,但是顶部的url地址没有变。那么就是 动态加载, 就是 XHR. 直接请求api.
实验代码如下:
python3
import requests
headers = {
"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"
}
def get_data():
url = "https://datacenter-web.eastmoney.com/api/data/v1/get?callback=jQuery1123003202116224702545_1721237385870&sortColumns=SCGGRQ&sortTypes=-1&pageSize=50&pageNumber=18&columns=ALL&source=WEB&token=894050c76af8597a853f5b408b759f5d&reportName=RPTA_WEB_BGCZMX"
resp = requests.get(url, headers=headers)
print(resp.status_code)
ret = resp.text
print(ret)
file_path = 'resp2.txt'
with open(file_path, 'w', encoding='utf-8') as f:
f.write(ret)
get_data()
输出: