在Python中执行请求以获取http api调用

时间:2017-12-15 00:17:09

标签: python python-requests

我是python,http apis和json的新手。我试图通过此api调用中的http请求获取json:https://api.coinmarketcap.com/v1/ticker/bitcoin/

如果我手动点击该链接,我可以看到json:

[
    {
        "id": "bitcoin", 
        "name": "Bitcoin", 
        "symbol": "BTC", 
        "rank": "1", 
        "price_usd": "16578.8", 
        "price_btc": "1.0", 
        "24h_volume_usd": "13755500000.0", 
        "market_cap_usd": "277557494906", 
        "available_supply": "16741712.0", 
        "total_supply": "16741712.0", 
        "max_supply": "21000000.0", 
        "percent_change_1h": "0.34", 
        "percent_change_24h": "0.5", 
        "percent_change_7d": "-3.06", 
        "last_updated": "1513295354"
    }
]

但我的代码似乎无法满足请求。它只是挂起,什么也不做,没有错误或任何东西,只是挂起。我的python代码:

import requests
r = requests.get('https://api.coinmarketcap.com/v1/ticker/bitcoin/')
r.json()

更新:下面的代码适用于我,所以证明请求在我的python中至少对某些网站有效,但它不是我想要的网站:

import requests
r = requests.get('http://ip.jsontest.com/')
print(r.json()["ip"])

这是一个不同的网站,只有一个用于测试json。任何想法为什么我不能成功查询coinmarketcap?似乎在评论中为用户工作

UPDATE2:

我已更改请求的标头以匹配浏览器的标头。评论中的某些人建议我这样做是因为我可能会因为'用户代理'而受阻。我首先测试了'http://httpbin.org/headers',以确保我正确地获得了标题。然后我对coinmarketcap的请求进行了更改,但它仍然悬而未决。以下是代码:

import requests

chrome_headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en;q=0.9",
"Connection": "close",
"Cookie": "_gauges_unique_hour=1; _gauges_unique_day=1; _gauges_unique_month=1; _gauges_unique_year=1; _gauges_unique=1",
"Host": "httpbin.org",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"
}

python_headers = {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.18.4'}

python_headers_with_host_and_user_agent_mod = {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Host': 'api.coinmarketcap.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36'}

chrome_headers_with_host_mod = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en;q=0.9",
"Connection": "close",
"Cookie": "_gauges_unique_hour=1; _gauges_unique_day=1; _gauges_unique_month=1; _gauges_unique_year=1; _gauges_unique=1",
"Host": "api.coinmarketcap.com",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"
}

r = requests.get('http://httpbin.org/headers')
print(r.json())
r2 = requests.get('http://httpbin.org/headers', headers=chrome_headers)
print(r2.json())
r3 = requests.get('https://api.coinmarketcap.com/v1/ticker/bitcoin/', 
headers=python_headers_with_host_and_user_agent_mod)
print(r3.json())
r4 = requests.get('https://api.coinmarketcap.com/v1/ticker/bitcoin/', 
headers=chrome_headers_with_host_mod)
print(r4.json())

Update3:最后我无法让这个适用于coinmarketcap。它似乎适用于其他所有人,但不是我,也不知道为什么。我找到了另一个名为crpytocompare的网站,它也有一个API,而且似乎更像是我正在寻找的东西,所以我正在使用它。

0 个答案:

没有答案