【问题标题】:How to parse xml using scrapy如何使用scrapy解析xml
【发布时间】:2019-03-07 16:58:50
【问题描述】:

如何使用 scrapy 抓取 XML。

我的 XML 看起来像这样:

    <rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
    <generator>NFE/5.0</generator>
    <title>"python" - Google News</title>
    <link>
    https://news.google.com/search?q=python&hl=en-IN&gl=IN&ceid=IN:en
    </link>
    <language>en-IN</language>
    <webMaster>news-webmaster@google.com</webMaster>
    <copyright>2019 Google Inc.</copyright>
    <lastBuildDate>Thu, 07 Mar 2019 16:48:55 GMT</lastBuildDate>
    <description>Google News</description>
    <item>
    <title>
    Brown snake attacks python eating a rat - NEWS.com.au
    </title>
    </channel>
    </rss>

我的代码如下所示:

from scrapy.spiders import XMLFeedSpider
from scrapy.http import HtmlResponse
from scrapy.selector import Selector


response = HtmlResponse(url='https://news.google.com/rss/search?q=python&hl=en-IN&gl=IN&ceid=IN:en')
xxs = Selector(response)
obj = xxs.xpath('//title/text()').extract()

我想获取标题标签中的文本。但在这里我得到一个空列表。请帮帮我。这一点很重要。 非常感谢

【问题讨论】:

    标签: python xml web-scraping scrapy


    【解决方案1】:

    您正在被 robots.txt禁止。 您需要在settings.py 中更改此行为并将ROBOTSTXT_OBEY=True 更改为ROBOTSTXT_OBEY=False

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-17
      • 2013-01-31
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 2018-01-22
      • 2012-04-10
      相关资源
      最近更新 更多