XML

进入新浪博客点击订阅后会提示订阅地址为 XML 格式的地址,如图

博客地址

http://blog.sina.com.cn/u/3980770831

XML 地址内容,如图

XML 地址:

http://blog.sina.com.cn/rss/3980770831.xml

创建一个项目:

$ scrapy startproject blogxml

填写 item 需要定义的存储数据:

import scrapy
 
class BlogxmlItem(scrapy.Item):
    # 存储标题
    title = scrapy.Field()
    # 存储对应连接
    link = scrapy.Field()
    # 存储对应文章作者
    author = scrapy.Field()

创建一个 xml 模板:

$ cd blogxml
$ scrapy genspider -l
$ scrapy genspider -t xmlfeed blogxmlspider sina.com.cn

打开创建的 blogxmlspider.py 文件更改代码:

# -*- coding: utf-8 -*-
from scrapy.spiders import XMLFeedSpider
from blogxml.items import BlogxmlItem
 
class BlogxmlspiderSpider(XMLFeedSpider):
    name = 'blogxmlspider'
    allowed_domains = ['sina.com.cn']
    # 设置 xml 地址
    iterator = 'iternodes' # you can change this; see the docs
    # 此时将开始迭代的节点设置为第一个节点 rss
    itertag = 'rss' # change it accordingly
 
    def parse_node(self, response, node):
        = BlogxmlItem()
        # 利用 XPath 表达式将对应信息提取出来,并存储到对应的 Item 中
        i['title'= node.xpath("//rss/channel/item/title/text()").extract()
        i['link'= node.xpath("//rss/channel/item/link/text()").extract()
        i['author'= node.xpath("//rss/channel/item/author/text()").extract()
        for in range(len(i['title'])):
            print(" 第 "+str(j+1)+" 篇文章 ")
            print(" 标题是: ")
            print(i['title'][j])
            print(" 对应链接是: ")
            print(i['link'][j])
            print(" 对应作者是: ")
            print(i['author'][j])
            print("------------------------")
        return i

修改前:

修改后:

写入一个 main.py 内容为:

from scrapy import cmdline
cmdline.execute("scrapy crawl blogxmlspider".split())

直接执行即可。

posted @ 2019-08-14 18:26  翡翠嫩白菜  阅读(111)  评论(0编辑  收藏  举报