I'm using python amazon product api and I can't seem to get the url for the image of the product.
Here is my code so far
for book in amz_api.item_search('Books', Keywords='cookies', ResponseGroup='Large', limit=10):
print book.ItemAttributes.Large
But I get this reply
AttributeError: no such child: {http://webservices.amazon.com/AWSECommerceService/2011-08-01}Large
Any help would be apprecicated
To access the image URLs, you can try to change your code to use one of the following:
print book.SmallImage.URL
print book.MediumImage.URL
print book.LargeImage.URL
The error is because there is no "Large" attribute in ItemAttributes. The image URLs are available in a different part of the response.
The Large Response Group (ResponseGroup='Large') returns a lot of data. According to the docs it's for demonstration purposes and not intended for production applications. To make your code production ready, you might need a different approach, such as the Images Response Group (ResponseGroup='Images').
Also, the python type for the book variable in the above code is:
<type 'lxml.objectify.ObjectifiedElement'>
While debugging, you can look at all the data available in book using something like this:
from lxml import objectify
print(objectify.dump(book))
Related
I'm trying to grab tiles from OpenStreetMap. I learned the syntax of their API. I have an "old way" of doing things, using Requests, and a "new way", which is being used by someone else's github project. I'm trying to use their project, but it's failing for me in one spot. I then put together this short script as a minimal example:
from PIL import Image
import requests
import urllib.request
#A tile we want to grab from Open Street Map
tile_url = "https://a.tile.openstreetmap.org/16/19299/24629.png"
#Old way: Get the bytes via requests.get, then parse as image and save.
old_way = Image.open(requests.get(tile_url,stream=True).raw)
old_way.save("oldway.png")
#New way: use urlretrieve to directly copy the file over to newway.png
destination = "newway.png"
try:
path, response = urllib.request.urlretrieve(tile_url, destination)
except urllib.error.URLError as e:
print("URL error!")
print(e.code)
#Expected behavior: Two identical images are created,
#named oldway.png and newway.png
#Actual behavior: We get oldway, but newway gives a 403 error.
What's going wrong here? What's the difference between these two HTTP GET requests which results in the first one working fine, and the second one giving a 403 error? I've tried digging into the source code of the respective Python libraries, but it turns out to be a pretty long mess of functions calling each other. And of course since it's HTTPS, I can't monitor the network connection to evaluate the raw bytes being transferred and figure things out that way.
Is there something wrong with the user-agent? Something with the headers? Please note that I'm not just trying to solve this problem ("What's the issue? You have a method that works. Ignore the broken one"), but I'm trying to learn about the details here and hoping that this odd edge case can benefit me in the future.
I just wanted to ask what can I do to solve this issue I have.
Essentially I am making a stock checker for sneakers from Adidas, I know the endpoint to obtain the stock but the JSON data given back to me whilst readable and contains what I need also contains a bunch of other information that is unnecessary to what I am trying to do.
Example of a link to an endpoint:
http://production.store.adidasgroup.demandware.net/s/adidas-GB/dw/shop/v16_9/products/(BZ0221)?client_id=c1f3632f-6d3a-43f4-9987-9de920731dcb&expand=availability,variations,prices
This is a link to the JSON containing the stock of the shoe, price and availability. However, if you try to open it you'll see that it responds a bunch of useless info such as the description of the shoe and the price which I do not need.
A github repository that I was using to try and get to grips with the requests I am trying to make is:
https://github.com/yzyio/adidas-stock-checker/blob/master/assets/index.js
I can get it to give me the JSON response I am just trying to strip what I don't need and keep what I do need which I am finding very difficult especially in python.
Many Thanks!
Since you've said you can get a JSON response from the server than the first think you need to do is tell python to load it as JSON.
import json
data = json.loads(response_from_server)
After doing this you can now access the values in your JSON object the way you would access them via a Python dict.
data["artist"]["id"]
I am trying to include the Google PageSpeed Insights Score in my application. I came across the api for it and have tried to use it:
https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=http://wikipedia.org&filter_third_party_resources=true&locale=en_US&screenshot=false&strategy=desktop&key=MyAPIKey
After this I got the output as shown in the gist:
https://gist.github.com/JafferWilson/6f8c5661e11654f301247edca45d23df
But when I use the application of PageSpeed Insights, with same domain as : WikiPedia.org, I got different result of score and could not find that in the JSON api: https://developers.google.com/speed/pagespeed/insights/?url=http%3A%2F%2Fwikipedia.org&tab=mobile
I am using Python2.7 with windows10. and have tried this code for accessing the api:
>>> url = "https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=http://wikipedia.org&filter_third_party_resources=true&locale=en_US&screenshot=false&strategy=desktop&key=MYAPIKey"
>>> response = urllib.urlopen(url)
>>> data = json.loads(response.read())
print data.
But I want to have the exact scoring as shown on the PageSpeedInsights of Google. Kindly suggest me what is the way to have the same score as that of Google Insights Page. I could not see the same score in the API result anyways.
For Desktop/Mobile: set strategy=desktop to strategy=mobile in the url.
Discrepancies between the JSON and the website could possibly just be variation within multiple runs, since it's likely the website doesn't fall squarely within scoring buckets. However, it seems that the score is relatively stable within a 1-score range for both desktop and mobile.
I'd like to know how can I simply get the title or other information about a video using Youtube API, in case the only thing I know is the url of the video (so basically the video ID).
What other info can I get about a video? eg: Length, Category, Uploader name, Country of origin, ... ???
Can somebody provide me a usable code snippet and the library to use for this data collecting?
Thanks for the help in advance.
There are plenty of examples and documentation about what you can get using the YouTube API's Python bindings.
Here are Python code samples as provided by YouTube:
https://developers.google.com/youtube/v3/code_samples/python#create_and_manage_youtube_video_caption_tracks
And the code samples can also be downloaded from their GitHub repository:
https://github.com/youtube/api-samples/tree/master/python
Try below code:
payload = {'id': search_result["id"]["videoId"], 'part': 'contentDetails,statistics,snippet', 'key': DEVELOPER_KEY}
l = requests.Session().get('https://www.googleapis.com/youtube/v3/videos', params=payload)
resp_dict = json.loads(l.content)
print "Title: ",resp_dict['items'][0]['snippet']['title']
Try using this API: https://pypi.org/project/python-youtube/
To grab the title, you can do something like this:
from pyyoutube import Api
playlistVideoItems = api.get_playlist_items(playlist_id='PLOU2XLYxmsIKpaV8h0AGE05so0fAwwfTw').items
print(playlistVideoItems[0].snippet.title)
Note that the above is somewhat untested code. I copied the relevant bits and pieces from what I currently have, but I did not test this exact set lines of code. And of course, I'm not actually using that playlist ID for my purposes.
In regards to what other types of information can be gathered, I would recommend reading the documentation or running in a debugger. As of this writing, I am trying to figure out how to obtain the video uploader name, but I don't really need this data although it would be nice to have. I will update this answer if I figure it out.
A beginner here, I am using the imgur python library to get tags related to an image. For this I am using the gallery_item_tags method as mentioned here.
However whenever i call the method it gives me an output as shown here.
I have followed the authorization procedure using the needed client id and client secret and i can run all methods not involving TagVotes array. How can i get the required information from this?
You're getting a list of TagVote instances. You probably want the name, you can access it like this:
for tag in tags:
print tag.name