Python 如何提取 Ajax 真正响应的内容:
以 https://m.weibo.cn/u/2830678474 这个网页为例,选择其中一个 Ajax 请求,找到请求的URL和传递的参数
import requests
from urllib.parse import urlencode # 定义请求头
headers = {
"MWeibo-Pwa": "",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
} # 定义传递的参数
params = {
"type": "uid",
"value": "",
"containerid": "",
} # 构造请求的URL并发起请求
url = "https://m.weibo.cn/api/container/getIndex?" + urlencode(params)
data = requests.get(url, headers=headers).text
print(data)
结果返回真正的响应内容: