2

如何使用 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()

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

4

1 回答 1

-1

你被robots.txt禁止了。您需要在 中更改此行为settings.py并更改ROBOTSTXT_OBEY=TrueROBOTSTXT_OBEY=False.

于 2019-03-07T17:35:07.873 回答