简体   繁体   English

Scrapy Shell XPath

[英]Scrapy Shell XPath

I am trying to get links and category from this http://www.npr.org/rss/#feeds news feed website. 我正在尝试从此http://www.npr.org/rss/#feeds新闻提要网站获得链接和类别。

This is my xpath in scrapy shell: 这是我在scrapy shell中的xpath:

a = sel.xpath('//ul[@class="rsslinks"]/li/a/@href').extract()

b = sel.xpath('//ul[@class="rsslinks"]/li/a/text()').extract()

But length of b is one lesser than length of a. 但是b的长度比a的长度小一。 I don't know what I am missing here. 我不知道我在这里想念的是什么。 But this is causing problems in data. 但这会导致数据问题。

From the image below,the category name is "Most Emailed Stories" but link is for "News Headlines" 在下图中,类别名称为“电子邮件最多的故事”,但链接为“新闻头条”

Any help would be appreciated 任何帮助,将不胜感激 Xpath屏幕

This is because of the first link in the results: 这是因为结果中的第一个链接:

<a class="iconlink xml" href="/rss/rss.php?id=1001" target="blank"><strong>News Headlines</strong></a>

As you can see, there is no direct child "text" nodes, only one strong element. 如您所见,没有直接的子“文本”节点,只有一个strong元素。 Your xpath would not match it. 您的xpath不匹配。

Add an another slash to get all text nodes from the a tag: 添加另一个斜杠以从a标签获取所有文本节点:

//ul[@class="rsslinks"]/li/a//text()
                         HERE^

标签为“新闻标题”的/rss/rss.php?id=1001文本在<strong> </strong>下似乎又向下了一层,而其他链接则没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM