Connecting to API via web vs python
我试图通过API访问XML文档。 当我尝试在python中连接时,我得到403状态代码。 但是,当我将链接粘贴到Chrome浏览器时,数据将按原样显示。 我知道我可能需要在Python中添加一些标题,但我不知道该怎么做。
1 2 | 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上打开链接,打开开发工具,网络选项卡,然后右键单击并"复制为cURL"。 粘贴到控制台并检查它是否足够:
1 | $ 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 ; |
输出:
1 2 3 4 5 6 7 8 9 | <?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
1 2 3 4 | >>> url = 'https://api.github.com/some/endpoint' >>> headers = {'user-agent': 'my-app/0.0.1'} >>> r = requests.get(url, headers=headers) |