我想使用Scrapy从HTML格式正确的站点中提取一些数据。使用XPath,我可以提取项目列表,但不能使用XPath从列表中的元素中提取额外数据
所有XPath已使用XPather进行了测试。我已经使用包含网页的本地文件测试了该问题,同样的问题。
开始:
# Get the webpage
fetch("https://www.someurl.com")
# The following gives me the expected items from the HTML
products = response.xpath("//*[@id='product-list-146620']/div/div")
这些项目是这样的:
<div data-pageindex="1" data-guid="13157582" class="col ">
<div class="item item-card item-card--static">
<div class="item-card__inner">
<div class="item__image item__image--overlay">
<a href="/www.something.anywhere?ref_gr=9801" class="ratio_custom" style="padding-bottom:100%">
</a>
</div>
<div class="item__text-container">
<div class="item__name">
<a class="item__name-link" href="/c.aspx?ref_gr=9801">The text I want</a>
</div>
</div>
</div>
</div>
</div>
当使用以下Xpath提取“我想要的文本”时,我什么也没得到:
XPATH_PRODUCT_NAME = "/div/div/div/div/div[contains(@class,'item__name')]/a/text()"
products[0].xpath(XPATH_PRODUCT_NAME).extract()
输出为空,为什么?
试试下面的代码。
XPATH_PRODUCT_NAME = ".//div[@class='item__name']/a[@class='item__name-link']/text()"
products[0].xpath(XPATH_PRODUCT_NAME).extract()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句