我的代码中有这个:
link_tag = "//div[@class= 'yuRUbf']//a/@href"
使用此代码,我收到此错误
The result of the xpath expression "//div[@class= 'yuRUbf']//a/@href" is: [object Attr]. It should be an element.
我不知道从该 div 类中抓取 URL 的任何其他方法。我怎样才能解决这个问题?此代码适用于 scraper chrome 扩展,但不适用于 python。
您必须更改 XPath 以选择元素节点(如错误消息所示) - 而不是属性节点 - 之后,获取其属性。所以使用
link_tag = "//div[@class= 'yuRUbf']//a"
links = driver.find_elements_by_xpath(link_tag)
然后提取属性
links[0].get_attribute("href")
获取@href
第一个匹配元素的属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句