爬取腾讯新闻https://new.qq.com/ch/world/
数据时,发现新闻请求为
url=https://r.inews.qq.com/web_feed/getPCList
请求json为
{"qimei36":"0_c97817e1c7064","forward":"1","base_req":{"from":"pc"},"flush_num":null,"channel_id":"news_news_world","device_id":"0_c97817e1c7064","is_local_chlid":""}
爬取数据的时候,响应码为200,但是text为空,message为“empty req” 求解?用postman模拟请求时是有数据的
url=https://r.inews.qq.com/web_feed/getPCList
headers = {
"Content-Type": "application/json",
"Content-Length": "166",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"
}
data = {"qimei36":"0_c97817e1c7064","forward":"1","base_req":{"from":"pc"},"flush_num":"","channel_id":"news_news_world","device_id":"0_c97817e1c7064","is_local_chlid":""}
response = requests.post(url=url, headers=headers, data=data)
return response
import requests
import pandas as pd
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
}
json_data = {
'qimei36': '0_d6367ad6381c3',
'forward': '2',
'base_req': {
'from': 'pc',
},
'flush_num': None,
'channel_id': 'news_news_world',
'device_id': '0_d6367ad6381c3',
'is_local_chlid': '',
}
response = requests.post('https://r.inews.qq.com/web_feed/getPCList', headers=headers, json=json_data)
data=pd.DataFrame(response.json()['data'])
抓包对比下 referer user-agent cookie 以及地址
一些网站会限制同一IP地址的请求速率。尝试减慢你的爬虫,以降低对服务器的负载,同时减小被封锁的风险。
1、可能是python3中的data里面的数据过期导致的
2、再次检查postman运行下,是否可以获取有效数据
如果还不行的话,也有可能是ip问题导致
更换wifi为手机热点,看下是否可以正常获取数据
如有问题随时沟通
如有帮助欢迎采纳
引用 皆我百晓生 小程序回复内容作答:
在Python中,使用requests
库发送POST请求时,可以通过传递JSON格式的数据作为json
参数来发送请求。修改你的代码如下:
import requests
url = "https://r.inews.qq.com/web_feed/getPCList"
headers = {
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"
}
data = {
"qimei36": "0_c97817e1c7064",
"forward": "1",
"base_req": {"from": "pc"},
"flush_num": "",
"channel_id": "news_news_world",
"device_id": "0_c97817e1c7064",
"is_local_chlid": ""
}
response = requests.post(url=url, headers=headers, json=data)
print(response.status_code)
print(response.text)
这样可以确保请求体以JSON格式发送,并且会自动设置Content-Type
为application/json
。
出现这种情况可能是因为你使用的是 Python 自带的 requests 库,而该库的默认 content-type 为 application/x-www-form-urlencoded,而实际上你的请求应该使用的是 application/json。
你可以尝试在请求头中添加 Content-Type: application/json,或者使用 requests 库的 json 参数来指定请求体为 json 格式,示例代码如下:
import requests
url = 'http://example.com'
data = {'key': 'value'}
response = requests.post(url, json=data, headers={'Content-Type': 'application/json'})
print(response.status_code)
print(response.text)
如果问题仍然存在,可以尝试使用 Fiddler 或 Wireshark 等工具来抓取请求和响应的详细信息,进一步定位问题。出现这种情况可能是因为你使用的是 Python 自带的 requests 库,而该库的默认 content-type 为 application/x-www-form-urlencoded,而实际上你的请求应该使用的是 application/json。
你可以尝试在请求头中添加 Content-Type: application/json,或者使用 requests 库的 json 参数来指定请求体为 json 格式,示例代码如下:
import requests
url = 'http://example.com'
data = {'key': 'value'}
response = requests.post(url, json=data, headers={'Content-Type': 'application/json'})
print(response.status_code)
print(response.text)
如果问题仍然存在,可以尝试使用 Fiddler 或 Wireshark 等工具来抓取请求和响应的详细信息,进一步定位问题。
援引讯飞星火:
这个问题可能是由于请求头中的 "User-Agent" 导致的。尝试将 "User-Agent" 更改为其他值,例如使用 "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0",然后再次尝试发送请求。
修改后的代码如下:
import requests
url = "https://r.inews.qq.com/web_feed/getPCList"
headers = {
"Content-Type": "application/json",
"Content-Length": "166",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0"
}
data = {"qimei36": "0_c97817e1c7064", "forward": "1", "base_req": {"from": "pc"}, "flush_num": "", "channel_id": "news_news_world", "device_id": "0_c97817e1c7064", "is_local_chlid": ""}
response = requests.post(url=url, headers=headers, data=data)
print(response)
如果仍然无法解决问题,可以尝试使用代理或者更换 IP 地址。
如果Python的requests库返回了HTTP状态码200,但text属Xing为空,则这意味着服务器成功响应请求,但返回的内容为空。这可能是因为: