Issue
When I tried to get the url
of a image via xpath @src
from this link:
https://www.amazon.com/dp/B07FK8SQDQ/ref=twister_B00WS2T4ZA?_encoding=UTF8&th=1
I thought it would return a url html element
but it returned a messy result messy result
Here is the xpath code:
url_img ="//div[@id='imgTagWrapperId']/img[@id='landingImage']/@src"
product_title_xpath = "//h1[@id='title']/span[@id='productTitle']/text()"
product_price_xpath = "//tr[@id='priceblock_ourprice_row']/td[@class='a-> span12']/span[@id='priceblock_ourprice']/text()"
I can get the title and price except the url of image.
How to get the exact url?
Appreciate you time and help!
Solution
If you search for src
attribute of <img>
tag in source page you will find out you are getting a right output which is that messy result.
By searching further in source you will find a JSON object that contains URL that you want which is in same XPath with attribute data-a-dynamic-image
.
>>> import json
>>> json_data = json.loads(
... response.xpath("//div[@id='imgTagWrapperId']/img[@id='landingImage']/@data-a-dynamic-image").get())
>>> list(iter(json_data))[2]
'https://images-na.ssl-images-amazon.com/images/I/51JN-fLvUiL.jpg'
Answered By - Moein Kameli
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.