如何使用scrapy从嵌入在h2下的标签中提取文本?

x33g5p2x  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(165)

我想从标记中提取名称。
如果您是一个用户,请使用以下方法:
但它正在返回:

<h2 class="product-names">

\<a target="\_blank" href="https://www.electronicsbazaar.com/dell-inspiron-13-7348-core-i5-5200u-2-20ghz-8gb-500gb-int-webcam-win-10-13-3-touch" title='Refurbished Dell Inspiron 13 7348 (Core I5 5Th Gen/8GB/500GB/Int/Win 10/13.3" Touch)'\>\\n                                                                                                            Refurbished Dell Inspiron 13 7348 (Core I5 5Th Gen/8GB/500GB/Int/Win 10/13.3" Touch)                                                                                                                                          </a>

</h2>

如何获取链接的文本?
我试探着:

response.css('h2.product-names').get()

<h2 class="product-names">

\<a target="\_blank" href="https://www.electronicsbazaar.com/dell-inspiron-13-7348-core-i5-5200u-2-20ghz-8gb-500gb-int-webcam-win-10-13-3-touch" title='Refurbished Dell Inspiron 13 7348 (Core I5 5Th Gen/8GB/500GB/Int/Win 10/13.3" Touch)'\>\\n                                                                                                            Refurbished Dell Inspiron 13 7348 (Core I5 5Th Gen/8GB/500GB/Int/Win 10/13.3" Touch)                                                                                                                                          </a>
</h2>
kjthegm6

kjthegm61#

问题是,如果我从截图中没有读错的话,这个名称包含在标签中。正确的xpath是:

response.xpath('//h2[@class="product-names"]/a/@title').extract()

相关问题