抓取 div 标签内的内容,不显示为文本

Scrape the content inside of a div tag, which is not displayed as text

我正在抓取亚马逊评论,他们为我想抓取的每条评论提供了唯一标识符。但是,标识符从不显示为文本,而是以以下形式存在:

<div id="R2XLFP626GRWEM" data-hook="review" class="a-section review aok-relative">

我要"R2XLFP626GRWEM"退货

使用时

response.xpath('.//div[@data-hook="review"]').extract()

我得到了div标签的全部内容,考虑到整个评论都嵌入其中,内容相当多。

Product I'm scraping

我需要的内容:

您可以使用 CSS 选择器而不是 xpath 来获取 id 值,如下所示。

response.css('.a-section .review::attr(id)').extract()

或使用 xpath

response.xpath('//*[@class="a-section review aok-relative"]/@id').extract()

或者通过修改原来的xpath查询

response.xpath('.//div[@data-hook="review"]/@id').extract()

要使用 xpath 收集属性数据,请使用 @。你可以阅读更多相关信息 here 例如你的情况:

response.xpath(".//div[@class='a-section review aok-relative']/@id").extract()