我正试图通过api访问xml文档。当我尝试用python连接时,我得到一个403状态码。但是,当我将链接粘贴到chrome浏览器中时,数据会按原样显示。我知道可能需要在python中向请求添加一些头,但我不确定如何添加。

schedule = requests.get('https://api.sportradar.us/golf-t2/schedule/pga/2015/tournaments/schedule.xml?api_key=mssbj55v2wbrbr6jcet2xcdd')
print(schedules.status_code)

我可以在chrome中得到我的标题,但不确定需要添加到我的请求中的标题
接受:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
用户代理:mozilla/5.0(windows nt 10.0;win64;x64)applewebkit/537.36(khtml,类似于gecko)chrome/55.0.2883.87 safari/537.36
如何调整请求以返回200的状态?

最佳答案

将所有其他浏览器标题添加到请求中。最简单的方法是:打开chrome上的链接,打开dev工具,network选项卡,然后右键单击并“复制为curl”。粘贴到控制台并检查是否足够:

$ curl 'https://api.sportradar.us/golf-t2/schedule/pga/2015/tournaments/schedule.xml?api_key=mssbj55v2wbrbr6jcet2xcdd' -H 'Accept-Encoding: gzip, deflate, sdch, br' -H 'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4,es;q=0.2' -H 'Upgrade-Insecure-Requests: 1' -H 'X-Compress: null' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Cache-Control: max-age=0' -H 'If-None-Match: "c97bea3f0b2917ae53554f338c416859"' -H 'Connection: keep-alive' -H 'If-Modified-Since: Wed, 07 Oct 2015 02:41:03 GMT' --compressed ;

输出:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:s="http://feed.elasticstats.com/schema/golf/schedule-v1.0.xsd"
            exclude-result-prefixes="s" version="1.0">

<xsl:output method="html" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
....

然后将标题添加到应用程序:
http://docs.python-requests.org/en/master/user/quickstart/#custom-headers
>>> url = 'https://api.github.com/some/endpoint'
>>> headers = {'user-agent': 'my-app/0.0.1'}

>>> r = requests.get(url, headers=headers)

关于python - 通过Web与Python连接到API,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41522947/

10-15 16:08
查看更多