request爬虫库的小坑
今天在做爬虫的时候,想要请求一个json数据,
地址为:https://weibo.com/ajax/profile/info?uid=xxxxxxx
ID就自己去找一个哈。。。。本来这个应该是一个json数据,但是我的pycharm却返回了一个html,我百思不得其解,用apifox发现又是正常的,于是乎复制代码进行对比
我的代码:
import requests
headers = {
"Cookie" :"xxxxxxxxxxxxxxxx",
"user-agent":"YYYYYYY"
}
reponse = requests.get("https://weibo.com/ajax/profile/info?uid=xxxx",headers)
print(reponse.text)
apifox的正确代码:
import requests
url = "https://weibo.com/ajax/profile/info?uid=xxxx"
payload={}
files={}
headers = {
'Cookie': 'xxxxxxxxxxxxxxxx',
'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
'Accept': '*/*',
'Host': 'weibo.com',
'Connection': 'keep-alive'
}
response = requests.request("GET", url, headers=headers, data=payload, files=files)
print(response.text)
聪明的你,能看出来哪里不一样吗???
A.headers内容不对导致的
B.参数不对
C.请求的方法不对
D.lz的程序太臭了运行不出json
-----------------------
-----------------------
-----------------------
-----------------------
答案是B!!
headers之前实验过,是没问题的
requests.get()和requests.request('GET,xxxx)是一样的实现,实现如下
def get(url, params=None, **kwargs):
r"""Sends a GET request.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary, list of tuples or bytes to send
in the query string for the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
return request("get", url, params=params, **kwargs)
正确的写法应该是:
response = requests.get("https://weibo.com/ajax/profile/info?uid=6028381202",headers=headers)
不指定header,会被解释为params,艹
原文地址:https://blog.csdn.net/m0_72678953/article/details/143755692
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!