Python github代码搜索api返回html而不是json
我想通过python从github API获取一些数据:Python github代码搜索api返回html而不是json,python,api,github,python-requests,github-api,Python,Api,Github,Python Requests,Github Api,我想通过python从github API获取一些数据: import requests headers = {'User-Agent': 'Awesome-Octocat-App', 'Accept': 'application/vnd.github.preview+json'} link = "https://github.com/search?q=chembl+created:>=2000" r = requests.get(link, headers=headers) 看起来一切
import requests
headers = {'User-Agent': 'Awesome-Octocat-App', 'Accept': 'application/vnd.github.preview+json'}
link = "https://github.com/search?q=chembl+created:>=2000"
r = requests.get(link, headers=headers)
看起来一切都很顺利:
r.ok
>>> True
因此,我希望有json作为响应:
r.json()
但这引发了一个例外:
JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)
不幸的是,我拥有的是html:
r.content
<!DOCTYPE html>
<html>
<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# githubog: http://ogp.me/ns/fb/githubog#">
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
...
r.content
...
这个html包含我正在寻找的所有存储库,但我需要的是json而不是html。为什么? 您正在使用URL:
https://github.com/search?q=chembl+创建:>=2000
您应该使用以下URL:
https://api.github.com/search/repositories?q=chembl+已创建:>=2000
以下是文件:
您正在使用URL:
https://github.com/search?q=chembl+创建:>=2000
您应该使用以下URL:
https://api.github.com/search/repositories?q=chembl+已创建:>=2000
以下是文件:
您需要使用获取JSON内容<代码>http://github.com/search是常规的HTML前端。你可能想:
这给了我:
>>> r = requests.get(link, headers=headers, params=query)
>>> r.ok
True
>>> r.json().keys()
[u'total_count', u'items']
>>> r.json()['total_count']
17
您需要使用来获取JSON内容<代码>http://github.com/search是常规的HTML前端。你可能想:
这给了我:
>>> r = requests.get(link, headers=headers, params=query)
>>> r.ok
True
>>> r.json().keys()
[u'total_count', u'items']
>>> r.json()['total_count']
17