Python 为什么不是';XMLFeedSpider未能遍历指定的节点?
我正试图通过解析的RSS提要来获取新的出版物。RSS提要位于 下面是我的蜘蛛:Python 为什么不是';XMLFeedSpider未能遍历指定的节点?,python,xml,rss,scrapy,scrapy-spider,Python,Xml,Rss,Scrapy,Scrapy Spider,我正试图通过解析的RSS提要来获取新的出版物。RSS提要位于 下面是我的蜘蛛: from scrapy.contrib.spiders import XMLFeedSpider class PLoSSpider(XMLFeedSpider): name = "plos" itertag = 'entry' allowed_domains = ["plosone.org"] start_urls = [
from scrapy.contrib.spiders import XMLFeedSpider
class PLoSSpider(XMLFeedSpider):
name = "plos"
itertag = 'entry'
allowed_domains = ["plosone.org"]
start_urls = [
('http://www.plosone.org/article/feed/search'
'?unformattedQuery=*%3A*&sort=Date%2C+newest+first')
]
def parse_node(self, response, node):
pass
此配置生成以下日志输出(请注意异常):
以下是输出:
$ scrapy crawl plos
2015-02-06 18:33:01+0100 [scrapy] INFO: Scrapy 0.24.4 started (bot: plos)
2015-02-06 18:33:01+0100 [scrapy] INFO: Optional features available: ssl, http11, boto
2015-02-06 18:33:01+0100 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'plos.spiders', 'SPIDER_MODULES': ['plos.spiders'], 'BOT_NAME': 'plos'}
2015-02-06 18:33:01+0100 [scrapy] INFO: Enabled extensions: LogStats, TelnetConsole, CloseSpider, WebService, CoreStats, SpiderState
2015-02-06 18:33:02+0100 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2015-02-06 18:33:02+0100 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2015-02-06 18:33:02+0100 [scrapy] INFO: Enabled item pipelines:
2015-02-06 18:33:02+0100 [plos] INFO: Spider opened
2015-02-06 18:33:02+0100 [plos] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2015-02-06 18:33:02+0100 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2015-02-06 18:33:02+0100 [scrapy] DEBUG: Web service listening on 127.0.0.1:6080
2015-02-06 18:33:02+0100 [plos] INFO: Closing spider (finished)
2015-02-06 18:33:02+0100 [plos] INFO: Dumping Scrapy stats:
{'finish_reason': 'finished',
'finish_time': datetime.datetime(2015, 2, 6, 17, 33, 2, 65414),
'log_count/DEBUG': 2,
'log_count/INFO': 7,
'start_time': datetime.datetime(2015, 2, 6, 17, 33, 2, 60311)}
2015-02-06 18:33:02+0100 [plos] INFO: Spider closed (finished)
还应注意的是,运行scrapy shell“http://www.plosone.org/article/feed/search?unformattedQuery=*%3A*&sort=Date%2C+newest+first“
后跟响应。xpath('//entry')
生成一个空列表([]
)。然而,如果您查看原始XML数据,您可以看到
标记与day一样简单。我完全不知所措,这里…您需要处理:
另见:
from scrapy.contrib.spiders import XMLFeedSpider
class PLoSSpider(XMLFeedSpider):
name = "plos"
namespaces = [('atom', 'http://www.w3.org/2005/Atom')]
itertag = 'atom:entry'
iterator = 'xml'
allowed_domains = ["plosone.org"]
start_urls = [
('http://www.plosone.org/article/feed/search'
'?unformattedQuery=*%3A*&sort=Date%2C+newest+first')
]
def parse_node(self, response, node):
print node
您能否在此基础上进一步展开,并提供一个可行的示例?我试图修改我的代码(请参见编辑),但我仍然没有看到任何证据表明调用了
parse_node
。@blz当然,更新了答案(注意iterator='xml'
的重要性-这是我在初始答案中缺少的,抱歉)。感谢更新!您选择名称空间的背后是否有特定的原因?为什么是“atom”及其URL?@blz名称并不重要,但URL是-我从根xml节点获取的。希望这会对你有所帮助,它也会对你有所帮助。@blz谢谢,好吧,这不是我真正的主题,但这篇文章应该澄清一下:)
$ scrapy crawl plos
2015-02-06 18:33:01+0100 [scrapy] INFO: Scrapy 0.24.4 started (bot: plos)
2015-02-06 18:33:01+0100 [scrapy] INFO: Optional features available: ssl, http11, boto
2015-02-06 18:33:01+0100 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'plos.spiders', 'SPIDER_MODULES': ['plos.spiders'], 'BOT_NAME': 'plos'}
2015-02-06 18:33:01+0100 [scrapy] INFO: Enabled extensions: LogStats, TelnetConsole, CloseSpider, WebService, CoreStats, SpiderState
2015-02-06 18:33:02+0100 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2015-02-06 18:33:02+0100 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2015-02-06 18:33:02+0100 [scrapy] INFO: Enabled item pipelines:
2015-02-06 18:33:02+0100 [plos] INFO: Spider opened
2015-02-06 18:33:02+0100 [plos] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2015-02-06 18:33:02+0100 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2015-02-06 18:33:02+0100 [scrapy] DEBUG: Web service listening on 127.0.0.1:6080
2015-02-06 18:33:02+0100 [plos] INFO: Closing spider (finished)
2015-02-06 18:33:02+0100 [plos] INFO: Dumping Scrapy stats:
{'finish_reason': 'finished',
'finish_time': datetime.datetime(2015, 2, 6, 17, 33, 2, 65414),
'log_count/DEBUG': 2,
'log_count/INFO': 7,
'start_time': datetime.datetime(2015, 2, 6, 17, 33, 2, 60311)}
2015-02-06 18:33:02+0100 [plos] INFO: Spider closed (finished)
class PLoSSpider(XMLFeedSpider):
name = "plos"
namespaces = [('atom', 'http://www.w3.org/2005/Atom')]
itertag = 'atom:entry'
iterator = 'xml' # this is also important
from scrapy.contrib.spiders import XMLFeedSpider
class PLoSSpider(XMLFeedSpider):
name = "plos"
namespaces = [('atom', 'http://www.w3.org/2005/Atom')]
itertag = 'atom:entry'
iterator = 'xml'
allowed_domains = ["plosone.org"]
start_urls = [
('http://www.plosone.org/article/feed/search'
'?unformattedQuery=*%3A*&sort=Date%2C+newest+first')
]
def parse_node(self, response, node):
print node