【发布时间】:2021-04-02 23:08:19
【问题描述】:
我正在尝试在我的应用中获取亚马逊产品的图像。我在浏览器中检查了图像,发现他们的class-Name 是gc-design-img-preview。实际上有更多 Elements 同一个班级,所以我尝试只获得第一个。
这是我尝试过的:
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
}
但是,这并没有返回任何内容,而只是返回了一个空的String...我在这里缺少什么?我正在使用SwiftSoup,也许还有其他方法可以做到这一点?
更新:
我认为这是我需要的,但在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
【问题讨论】:
-
我去了亚马逊产品页面,但没有找到任何具有此类名称的产品。您能否提供一个示例页面以进一步调查?
-
@ManuelMB 我试着找到它,但我找不到......不知道我做了什么。无论如何,主要问题仍然是一样的。我想从任何亚马逊产品中获取主图像。
标签: ios swift parsing html-parsing swiftsoup