python萌新 requests 返回 status_code:200,但text下returnCode:301,怎么解决重定向?

requests.get(https://www.kuaidi100.com/autonumber/autoComNum?resultv2=1&text=4602992279077).text

获取到需要的返回值应该为:

{"comCode":"","num":"4602992279077","auto":[{"comCode":"yunda","lengthPre":13,"noCount":287195,"noPre":"460299"}]}

实际返回内容:

{"returnCode":"301","message":"非法请求","result":fals

这个是用requests.get()内的全部参数
图片说明

1个回答

加上 header 即可

import requests

url = "https://www.kuaidi100.com/autonumber/autoComNum?resultv2=1&text=4602992279077"
header = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0'
}

response = requests.get(url, headers = header)
print(response.text)

图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问