I am trying to get the image of a amazon product inside my app. I inspected the image in my browser and found out that their class-Name is gc-design-img-preview. There are actually more Elements with the same class so I tried only getting the first one.
This is what I tried:
func getAmazonImage(doc: Document) -> String {
let images: Elements? = try? doc.getElementsByClass("gc-design-img-preview")
guard (images?.first()) != nil else { return "nope" }
guard let imageUrl : String = try! images?.first()!.text() else { return "nope2" }
print("image: " + imageUrl)
return imageUrl
}
However this is not returning anything and instead just returning an empty String... What am I missing here? I am using SwiftSoup, maybe there is another way to do this?
Update:
I think this is what I need but in Swift:
const imgSrc = document.querySelector('li.image.item.itemNo0.maintain-height.selected img').getAttribute('src')
console.log(imgSrc) // https://images-na.ssl-images-amazon.com/images/I/71y%2BUGuJl5L._SX522_.jpg
For instance, in this url: https://www.amazon.com/DualSense-Wireless-Controller-PlayStation-5/dp/B08FC6C75Y/ref=sr_1_1?dchild=1&fst=as%3Aoff&pf_rd_i=16225016011&pf_rd_m=ATVPDKIKX0DER&pf_rd_p=03b28c2c-71e9-4947-aa06-f8b5dc8bf880&pf_rd_r=CSWVBS40MDKKJYXEJ0AH&pf_rd_s=merchandised-search-3&pf_rd_t=101&qid=1489016289&rnid=16225016011&s=videogames-intl-ship&sr=1-1