I am getting a 403 Forbidden error when trying to load information for the following event. Any ideas what is causing this??
import json
from pprint import pprint
from urllib.request import urlopen
url = 'http://www.stubhub.com/ticketAPI/restSvc/event/104836772'
data = json.load(urlopen(url))
tickets = data['eventTicketListing']['eventTicket']
prices = [ticket['tc']['amount'] for ticket in tickets]
print(sorted(prices))
Error:
Traceback (most recent call last):
File "C:\Users\rcarmody\Desktop\Python\Tickets\tix_seatgeak.py", line 24, in <module>
data = json.load(urlopen(url))
File "C:\Program Files\Python39\lib\urllib\request.py", line 214, in urlopen
return opener.open(url, data, timeout)
File "C:\Program Files\Python39\lib\urllib\request.py", line 523, in open
response = meth(req, response)
File "C:\Program Files\Python39\lib\urllib\request.py", line 632, in http_response
response = self.parent.error(
File "C:\Program Files\Python39\lib\urllib\request.py", line 561, in error
return self._call_chain(*args)
File "C:\Program Files\Python39\lib\urllib\request.py", line 494, in _call_chain
result = func(*args)
File "C:\Program Files\Python39\lib\urllib\request.py", line 641, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
This link redirects to an actual webpage. Are you meaning to use api.stubhub.com instead of www.stubhub.com
you aren't sending credentials with the request. This explains the 403. Web error code 403 is a credentials error of some kind. You need to include credentials in your request via JSON.
Related
When i try this line:
import urllib.request
urllib.request.urlretrieve("https://i.redd.it/53tfh959wnv41.jpg", "photo.jpg")
i get the following error:
Traceback (most recent call last):
File "scraper.py", line 26, in <module>
urllib.request.urlretrieve("https://i.redd.it/53tfh959wnv41.jpg", "photo.jpg")
File "/usr/lib/python3.6/urllib/request.py", line 248, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "/usr/lib/python3.6/urllib/request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.6/urllib/request.py", line 532, in open
response = meth(req, response)
File "/usr/lib/python3.6/urllib/request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.6/urllib/request.py", line 570, in error
return self._call_chain(*args)
File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain
result = func(*args)
File "/usr/lib/python3.6/urllib/request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
But the link works fine in my browser? Why does it work in the browser but not for a request? It works with other pictures from the same site.
The request returns
If you check your developer console, It's a 404:
So what you see is imgur's custom 404 "page" (which is an image).
EDIT:
So urlretrieve fails on 404 status code. If you want to use the contents of the request (even if the statuscode is 404) you can do the following:
try:
urllib.request.urlretrieve("https://i.redd.it/53tfh959wnv41.jpg", "photo.jpg")
except Exception as e:
with open("error_photo.jpg", 'wb') as fp:
fp.write(e.read())
Try to change user-agent. You can just add a kwarg:
req = urllib.request.urlretrieve("https://i.redd.it/53tfh959wnv41.jpg", "photo.jpg", headers={"User-Agent": "put custom user agent here"})
I am trying to download error log files such as IIS logs, HTTP API error logs from the server and save it to my disk. These logs contain the .log extension.
Have tried below code to download, it works well for text files but it didn't work for the files which are of type .log:
from urllib.request import urlopen
response = urlopen('https://servername/path/errorlog.txt') #doesn't work for response = urlopen('https://servername/path/errorlog.log')
data = response.read()
# Write data to file
filename = "test.txt"
file_ = open(filename, 'w')
file_.write(data)
file_.close()
This is the error messages i am getting:
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
response = urlopen(url)
File "C:\Python\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "C:\Python\lib\urllib\request.py", line 532, in open
response = meth(req, response)
File "C:\Python\lib\urllib\request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python\lib\urllib\request.py", line 570, in error
return self._call_chain(*args)
File "C:\Python\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "C:\Python\lib\urllib\request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
Do you have a solution to this problem?
After installing bottlenose and getting my API keys and associate tags I tried following the instructions in this guide: https://github.com/lionheart/bottlenose
(I have removed my api keys)
This is the error I am getting:
>>> import bottlenose
>>> amazon = bottlenose.Amazon(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ASSOCIATE_TAG)
>>> response = amazon.ItemLookup(ItemId="B007OZNUCE")
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
response = amazon.ItemLookup(ItemId="B007OZNUCE")
File "C:\Users\Windows\AppData\Local\Programs\Python\Python35\lib\site-packages\bottlenose\api.py", line 265, in __call__
{'api_url': api_url, 'cache_url': cache_url})
File "C:\Users\Windows\AppData\Local\Programs\Python\Python35\lib\site-packages\bottlenose\api.py", line 226, in _call_api
return urllib2.urlopen(api_request, timeout=self.Timeout)
File "C:\Users\Windows\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 162, in urlopen
return opener.open(url, data, timeout)
File"C:\Users\Windows\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 471, in open
response = meth(req, response)
File"C:\Users\Windows\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 581, in http_response
'http', request, response, code, msg, hdrs)
File"C:\Users\Windows\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 509, in error
return self._call_chain(*args)
File"C:\Users\Windows\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 443, in _call_chain
result = func(*args)
File"C:\Users\Windows\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 589, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
Check that you have the system clock correctly configured.
I have the same problem and I fixed it changing the date and time to the correct one.
You have to provide the value for keys
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_ASSOCIATE_TAG
In order to get the keys you have to register on AWS
I've access data using the amazon product API before and that was easy. All I had to do was make some api keys and use a module in Python and it gave back the data.
however, this was 4-5 months ago and now when I try to use the api keys, its giving me 400 error.
Here is my code
from amazon.api import AmazonAPI
asin = 'B00CAB5ZKC'
AMAZON_ACCESS_KEY='AKIAI4ZP7EZGNSTAWKTA'
AMAZON_SECRET_KEY ='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'
AMAZON_ASSOC_TAG =246152698300
amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, region = "DE")
product = amazon.lookup(ItemId=asin, Condition='All', MerchantId='All')
This is the error I get
Traceback (most recent call last):
File "C:\Users\Hari\Documents\Python\data\amazon_test_script.py", line 22, in <module>
product = amazon.lookup(ItemId=asin, Condition='All', MerchantId='All')
File "build\bdist.win-amd64\egg\amazon\api.py", line 173, in lookup
response = self.api.ItemLookup(ResponseGroup=ResponseGroup, **kwargs)
File "C:\Python27\lib\site-packages\bottlenose\api.py", line 265, in __call__
{'api_url': api_url, 'cache_url': cache_url})
File "C:\Python27\lib\site-packages\bottlenose\api.py", line 226, in _call_api
return urllib2.urlopen(api_request, timeout=self.Timeout)
File "C:\Python27\lib\urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 437, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 550, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 475, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 409, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 558, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 400: Bad Request
>>>
I've been debugging this for 2 days now, with no success. Could anyone tell me what am I missing.
Anyway, I'm trying to write a simple request to the Amazon API using the following code:
ak = "***"
sk = "***"
at = "***"
import bottlenose
amazon = bottlenose.Amazon(ak, sk, at, "DE")
response=amazon.ItemLookup(ItemId="B00KWAO4CI")
print(response.price_and_currency)
It should return an XML object. Instead I get the following result:
Traceback (most recent call last):
File "simpleamazon.py", line 7, in <module>
response=amazon.ItemLookup(ItemId="B00KWAO4CI")
File "/Library/Python/2.7/site-packages/bottlenose/api.py", line 251, in __call__
{'api_url': api_url, 'cache_url': cache_url})
File "/Library/Python/2.7/site-packages/bottlenose/api.py", line 212, in _call_api
return urllib2.urlopen(api_request, timeout=self.Timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 437, in open
response = meth(req, response)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 550, in http_response
'http', request, response, code, msg, hdrs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 475, in error
return self._call_chain(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 558, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden
Up until recently I received HTTP Error 400 instead. To my knowledge I haven't changed anything. I've also tried using response groups, but it resulted in the same error(s).
Do you have any leads?
Using Python 3.5.2