RestAPI数据使用Python进行疤痕导致403 FORBIDDEN

时间:2018-01-30 13:03:27

标签: python-3.x

我正在尝试使用Python抓取REST API进行练习,但我一直在获取HTTP错误403

这是我的代码:

import json
from pprint import pprint
import datetime
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = 'https://cex.io/api/ohlcv/hd/20180124/BTC/USD'
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")

我得到的错误是:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\mubee\Downloads\Annaconda\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\mubee\Downloads\Annaconda\lib\urllib\request.py", line 532, in open
response = meth(req, response)
File "C:\Users\mubee\Downloads\Annaconda\lib\urllib\request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\mubee\Downloads\Annaconda\lib\urllib\request.py", line 570, in error
return self._call_chain(*args)
File "C:\Users\mubee\Downloads\Annaconda\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "C:\Users\mubee\Downloads\Annaconda\lib\urllib\request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

1 个答案:

答案 0 :(得分:0)

在请求之前尝试使用User-Agent,

import urllib2

my_url = 'https://cex.io/api/ohlcv/hd/20180124/BTC/USD'
opener = urllib2.build_opener()
opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
response = opener.open(my_url)
page_html = response.read()
response.close()
page_soup = soup(page_html, "html.parser")