【问题标题】:How to scrape xml feed with xmlfeedspider如何使用 xmlfeedspider 抓取 xml 提要
【发布时间】:2012-07-25 08:23:29
【问题描述】:

我正在尝试使用以下格式抓取一个 xml 文件

file_sample.xml:

<rss version="2.0">
 <channel>
   <item>
       <title>SENIOR BUDGET ANALYST (new)</title>
       <link>https://hr.example.org/psp/hrapp&SeqId=1</link>
       <pubDate>Wed, 18 Jul 2012 04:00:00 GMT</pubDate>
       <category>All Open Jobs</category>
   </item>
   <item>
       <title>BUDGET ANALYST (healthcare)</title>
       <link>https://hr.example.org/psp/hrapp&SeqId=2</link>
       <pubDate>Wed, 18 Jul 2012 04:00:00 GMT</pubDate>
       <category>All category</category>
   </item>
 </channel>
</rss>

下面是我的 spider.py 代码

class TestSpider(XMLFeedSpider):
    name = "testproject"
    allowed_domains = {"www.example.com"}
    start_urls = [
        "https://www.example.com/hrapp/rss/careers_jo_rss.xml"
        ]
    iterator = 'iternodes'
    itertag = 'channel'


    def parse_node(self, response, node):
        title = node.select('item/title/text()').extract()
        link  = node.select('item/link/text()').extract()
        pubdate  = node.select('item/pubDate/text()').extract()
        category  = node.select('item/category/text()').extract()
        item = TestprojectItem()
        item['title'] = title
        item['link'] = link
        item['pubdate'] = pubdate
        item['category'] = category
        return item

结果:

2012-07-25 13:24:14+0530 [testproject] DEBUG: Scraped from <200 https://hr.templehealth.org/hrapp/rss/careers_jo_rss.xml>
    {'title': [u'SENIOR BUDGET ANALYST (hospital/healthcare)',
               u'BUDGET ANALYST'],
     'link': [u'https://hr.example.org/psp/hrapp&SeqId=1',
               u'https://hr.example.org/psp/hrapp&SeqId=2'] 
     'pubdate': [u'Wed, 18 Jul 2012 04:00:00 GMT',
               u'Wed, 18 Jul 2012 04:00:00 GMT'] 
     'category': [u'All Open Jobs',
               u'All category'] 
      }

您可以从上面的结果中观察到,来自相应标签的所有结果都被合并到一个列表中,但是我想根据它们各自的项目标签进行映射,如下所示,就像我们为 html 抓取所做的那样。

    {'title': u'SENIOR BUDGET ANALYST (hospital/healthcare)'
     'link': u'https://hr.example.org/psp/hrapp&SeqId=1'
     'pubdate': u'Wed, 18 Jul 2012 04:00:00 GMT'
     'category': u'All Open Jobs'
      }
    {'title': u'BUDGET ANALYST'
     'link': u'https://hr.example.org/psp/hrapp&SeqId=2' 
     'pubdate': u'Wed, 18 Jul 2012 04:00:00 GMT'
     'category': u'All category'
      }

我们如何根据上面的item标签等单独的主标签来抓取xml标签数据。

提前致谢......

【问题讨论】:

    标签: python xml scrapy web-crawler


    【解决方案1】:

    尝试将您的 itertagitertag = 'channel' 更改为 'itertag = 'item'

    【讨论】:

      【解决方案2】:

      只需更改 itertag = 'item'。

      如果您参考parse_node 方法的文档,它声明该方法是为与提供的标记名称(itertag)匹配的节点调用的。在您的情况下,它是“项目”(“通道”根节点的子节点)。

      【讨论】:

        【解决方案3】:

        我推荐使用feedparser

        feedparser.parse(url)
        

        结果

        {'bozo': 1,
         'bozo_exception': xml.sax._exceptions.SAXParseException("EntityRef: expecting ';'\n"),
         'encoding': u'utf-8',
         'entries': [{'link': u'https://hr.example.org/psp/hrapp&SeqId=1',
           'links': [{'href': u'https://hr.example.org/psp/hrapp&SeqId=1',
             'rel': u'alternate',
             'type': u'text/html'}],
           'tags': [{'label': None, 'scheme': None, 'term': u'All Open Jobs'}],
           'title': u'SENIOR BUDGET ANALYST (new)',
           'title_detail': {'base': u'',
            'language': None,
            'type': u'text/plain',
            'value': u'SENIOR BUDGET ANALYST (new)'},
           'updated': u'Wed, 18 Jul 2012 04:00:00 GMT',
           'updated_parsed': time.struct_time(tm_year=2012, tm_mon=7, tm_mday=18, tm_hour=4, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=200, tm_isdst=0)},
          {'link': u'https://hr.example.org/psp/hrapp&SeqId=2',
           'links': [{'href': u'https://hr.example.org/psp/hrapp&SeqId=2',
             'rel': u'alternate',
             'type': u'text/html'}],
           'tags': [{'label': None, 'scheme': None, 'term': u'All category'}],
           'title': u'BUDGET ANALYST (healthcare)',
           'title_detail': {'base': u'',
            'language': None,
            'type': u'text/plain',
            'value': u'BUDGET ANALYST (healthcare)'},
           'updated': u'Wed, 18 Jul 2012 04:00:00 GMT',
           'updated_parsed': time.struct_time(tm_year=2012, tm_mon=7, tm_mday=18, tm_hour=4, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=200, tm_isdst=0)}],
         'feed': {},
         'namespaces': {},
         'version': u'rss20'}
        

        【讨论】:

        • 哇!非常感谢这有很大帮助。但实际上我们需要通过搜索来选择标签各自的值。但是无论如何只显示标签和标签值
        • 你的意思是这样的吗:[entry.tags[0]["term"] for entry in feedparser.parse(open("/tmp/feed.rss")).entries] => [u'All Open Jobs', u'All category']?
        • 实际上我的想法是什么,当我们使用一些代码运行 xml url 时,它应该自动解析标​​签并动态映射它们的值(不管知道标签并手动从标签中检索值) , 但您给出的上述代码是可以接受的。
        • 是否有其他库或模块可以做同样的事情
        猜你喜欢
        • 1970-01-01
        • 2010-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-04
        • 1970-01-01
        • 1970-01-01
        • 2012-02-24
        相关资源
        最近更新 更多