Below is my code. I want to get historical data from Google Finance, and I tried some methods to fix it. But it still doesn't work. I think it may be because Google Finance has blocked my IP. If so, is there any solution?
url='http://finance.google.com/finance/historical?q=AMZN'
headers = {'Accept':
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','User-Agent':'Mozilla/5.0'}
context = ssl._create_unverified_context()
htmlll=urllib.request.Request(url,headers=headers)
html = urllib.request.urlopen(htmlll,context=context).read().decode()
datalist=html.splitlines()
name = datalist[2].split('>')[2].split(':')[0].replace('amp;','')
print(name)
And the error is as below
Traceback (most recent call last):
File "C:/Users/admin/Desktop/Price & Volume Database/Global Intraday Pricing Data/Asia Intraday Pricing/test.py", line 30, in <module>
html = urllib.request.urlopen(htmlll,context=context).read().decode()
File "C:\Users\admin\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 162, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\admin\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 471, in open
response = meth(req, response)
File "C:\Users\admin\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 581, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\admin\AppData\Local\Programs\Python\Python35-
32\lib\urllib\request.py", line 509, in error
return self._call_chain(*args)
File "C:\Users\admin\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 443, in _call_chain
result = func(*args)
File "C:\Users\admin\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 589, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
I have added header and the web address works. I cannot find what's wrong with it.Thank you so much for your help!