前面写了学习CrawlSpider遇到的问题后,今天学XMLFeedSpider又出现了启动后没爬取到数据,但又不报错的情况
经过排查,发现又是一个粗心大意的错误:
class SpiderUserXMLFeedSpider(XMLFeedSpider):
name = 'xmlspider'
allowed_domains=['cnblogs.com']#这里敲少了一个s
start_urls=['http://feed.cnblogs.com/blog/u/269038/rss']
iterator = 'html'
itertag = 'entry'
另外记录一下,这种结构:
<entry>
<id>http://www.cnblogs.com/qiyeboy/p/9296038.html</id>
<title type="text">基于HTTPS的中间人攻击-BaseProxy - 七夜的故事</title>
<published>2018-07-11T10:16:00Z</published>
<updated>2018-07-11T10:16:00Z</updated>
<link rel="alternate" href="http://www.cnblogs.com/qiyeboy/p/9296038.html"/>
<link rel="alternate" type="text/html" href="http://www.cnblogs.com/qiyeboy/p/9296038.html"/>
</entry>
在匹配数据时,因为设置了起始标签是itertag = 'entry'所以xpath匹配只需这样写:
node.xpath('id/text()').extract()[0]
node.xpath('title/text()').extract()[0]
node.xpath('summary/text()').extract()[0]
不需要在id,title ,summary前面加/
还有在我查这个爬不到数据的问题时,还找到一篇不错的文章:https://blog.csdn.net/lw_power/article/details/77919533
这里讲了使用模板来生成spider的代码,用模板的话就不用所有代码都敲一遍,就设置一下items.py,然后去Spider.py里修改一下allowed_domains ,start_urls的链接,
并补充关键代码(其实自动生成的代码注释有提示你应该怎么写代码)