scrapy 中crawlspider 爬虫

本文介绍了一个使用Scrapy框架实现的RSS爬虫实例,通过CrawlSpider类从ChinaNews网站抓取RSS源,解析并提取新闻标题、链接、描述及发布日期等关键信息。

 

爬取目标网站:

http://www.chinanews.com/rss/rss_2.html

获取url后进入另一个页面进行数据提取

检查网页:

 

爬虫该页数据的逻辑:

Crawlspider爬虫类:

# -*- coding: utf-8 -*-
import scrapy
import re
#from scrapy import Selector
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

class NwSpider(CrawlSpider):
    name = 'nw'
    # allowed_domains = ['www.new.com']
    start_urls = ['http://www.chinanews.com/rss/rss_2.html']

    rules = (
      Rule(LinkExtractor(allow
='http://www.chinanews.com/rss/.*?\.xml'), callback='parse_item'), ) def parse_item(self, response): selector = Selector(response) items =response.xpath('//item').extract() for node in items: # print(type(node)) # item = {} item['title'] = re.findall(r'<title>(.*?)</title>',node,re.S)[0] item['link'] = re.findall(r'<link>(.*?)</link>',node,re.S)[0] item['desc'] = re.findall(r'<description>(.*?)</description>',node,re.S)[0] item['pub_date'] =re.findall(r'<pubDate>(.*?)</pubDate>',node,re.S)[0] print(item) #item['domain_id'] = response.xpath('//input[@id="sid"]/@value').get() #item['name'] = response.xpath('//div[@id="name"]').get() #item['description'] = response.xpath('//div[@id="description"]').get() # yield item

 

转载于:https://www.cnblogs.com/knighterrant/p/10743532.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值