用Scrapy获取arXiv xml数据

2024-04-25 00:44:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用scrapy从arXiv的页面获取信息,但无法从它们的xml page中选择“items”:

from scrapy.spider import BaseSpider
from scrapy.selector import XmlXPathSelector

class arXivSpider(BaseSpider):
    name = "arxiv"
    allowed_domains = ["arxiv.org"]
    start_urls = ["http://export.arxiv.org/rss/hep-th/recent"]

    def parse(self, response):
        xxs = XmlXPathSelector(response)
        papers = xxs.select('//item')
        print papers

item对象非常简单,如果我可以提取它。。。在

^{pr2}$

脚本运行得很好,只是papers = [],所以spider没有收集item的。它可能需要使用名称空间。。。在


Tags: fromorgimportresponsearxiv页面itemspider
2条回答

It may have to do w/ namespaces...

是的。在

XmlXPathSelector可以通过注册名称空间(examples in documentation)来处理名称空间。在您的情况下:

$ scrapy shell http://export.arxiv.org/rss/hep-th/recent
In [1]: xxs.register_namespace('g', 'http://purl.org/rss/1.0/')

In [2]: xxs.namespaces
Out[2]: {'g': 'http://purl.org/rss/1.0/'}

In [3]: xxs.select('//item')
Out[3]: []

In [4]: xxs.select('//g:item')
Out[4]:
[<XmlXPathSelector xpath='//g:item' data=u'<item xmlns="http://purl.org/rss/1.0/" x'>,
 <XmlXPathSelector xpath='//g:item' data=u'<item xmlns="http://purl.org/rss/1.0/" x'>,
...

我觉得你应该试试你那破破烂烂的壳,做实验。 1破壳“http://export.arxiv.org/rss/hep-th/recent

  1. 在sel.remove_名称空间()

  2. a=选择xpath('//title/text()')

enter image description here

相关问题 更多 >