基于Scrapy框架的Python新闻爬虫

发布时间:2018-08-31

概述

该项目是基于Scrapy框架的Python新闻爬虫,能够爬取网易,搜狐,凤凰和澎湃网站上的新闻,将标题,内容,评论,时间等内容整理并保存到本地

详细

一、开发背景

Python作为数据处理方面的一把好手,近年来的热度不断增长。网络爬虫可以说是Python最具代表性的应用之一,那么通过网络爬虫来学习Python以及网络和数据处理的相关内容可以说是再合适不过了。

Scrapy是由Python语言开发的一个快速、高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据。相比于传统的爬虫来说,基于scrapy框架的爬虫更加结构化,同时也更加高效,能完成更加复杂的爬取任务。

二、爬虫效果

1、标题

image.png

2、内容

image.png

3、评论

image.png

4、日期,热度和ID

image.png

5、程序运行图

image.png

三、具体开发

1、任务需求

1. 爬取网易,搜狐,凤凰和澎湃新闻网站的文章及评论

2. 新闻网页数目不少于10万页

3. 每个新闻网页及其评论能在1天内更新

2、功能设计

1. 设计一个网络爬虫,能够爬取指定网站的全部页面,并提取其中的文章及评论内容

2. 定时运行网络爬虫,实现每日更新数据

3、系统架构

首先简单介绍下scrapy框架,这是一个爬虫框架

Scrapy

绿线是数据流向,

(1)首先从初始URL 开始,Scheduler 会将其交给 Downloader 进行下载,

(2)下载之后会交给 Spider 进行分析,这里的spider就是爬虫的核心功能代码

(3)Spider分析出来的结果有两种:一种是需要进一步抓取的链接,它们会通过middleware传回 Scheduler ;另一种是需要保存的数据,送入Item Pipeline ,进行处理和存储

(4)最后将所有数据输出并保存为文件

4、实际项目

(1)项目结构

image.png

可以看到,NewsSpider-master是完整项目文件夹,下面存放有对应各个网站的爬虫启动脚本debug_xx.py,scrapyspider文件夹存放scrapy框架所需的相关文件,spiders文件夹存放实际的爬虫代码

(2)爬虫引擎

以网易新闻的爬虫news_163.py为例,简要说明部分核心代码:

①定义一个爬虫类:

class news163_Spider(CrawlSpider):
    # 网易新闻爬虫名称
    name = "163news"
    # 伪装成浏览器
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
    }
    #网易全网
    allowed_domains = [
        "163.com"
    ]
    #新闻版
    start_urls = [
        'http://news.163.com/'
    ]
    #正则表达式表示可以继续访问的url规则,http://news.163.com/\d\d\d\d\d(/([\w\._+-])*)*$
    rules = [
        Rule(LinkExtractor(
        allow=(
            ('http://news\.163\.com/.*$')
        ),
        deny = ('http://.*.163.com/photo.*$')
        ),
        callback="parse_item",
        follow=True)
    ]

②网页内容分析模块

根据不同内容的Xpath路径从页面中提取内容,由于网站在不同时间的页面结构不同,因此按照不同页面版式划分成几个if判断句块;

def parse_item(self, response):
    # response是当前url的响应
    article = Selector(response)
    article_url = response.url
    global count
    # 分析网页类型
    # 比较新的网易新闻 http://news.163.com/05-17/
    if get_category(article) == 1:
        articleXpath = '//*[@id="epContentLeft"]'
        if article.xpath(articleXpath):
            titleXpath = '//*[@id="epContentLeft"]/h1/text()'
            dateXpath = '//*[@id="epContentLeft"]/div[1]/text()'
            contentXpath = '//*[@id="endText"]'
            news_infoXpath ='//*[@id="post_comment_area"]/script[3]/text()'

            # 标题
            if article.xpath(titleXpath):
                news_item = newsItem()
                news_item['url'] = article_url
                get_title(article, titleXpath, news_item)
                # 日期
                if article.xpath(dateXpath):
                    get_date(article, dateXpath, news_item)
                # 内容
                if article.xpath(contentXpath):
                    get_content(article, contentXpath, news_item)
                    count = count + 1
                    news_item['id'] = count
                # 评论
                try:
                    comment_url = get_comment_url(article, news_infoXpath)
                    # 评论处理
                    comments = get_comment(comment_url, news_item)[1]
                    news_item['comments'] = comments
                except:
                    news_item['comments'] = ' '
                    news_item['heat'] = 0
                yield news_item

根据正则表达式匹配页面内容中的日期信息:

'''通用日期处理函数'''
def get_date(article, dateXpath, news_item):
    # 时间
    try:
        article_date = article.xpath(dateXpath).extract()[0]
        pattern = re.compile("(\d.*\d)")  # 正则匹配新闻时间
        article_datetime = pattern.findall(article_date)[0]
        #article_datetime = datetime.datetime.strptime(article_datetime, "%Y-%m-%d %H:%M:%S")
        news_item['date'] = article_datetime
    except:
        news_item['date'] = '2010-10-01 17:00:00'

其他函数:

'''网站分类函数'''
def get_category(article):

'''字符过滤函数'''
def str_replace(content):

'''通用正文处理函数'''
def get_content(article, contentXpath, news_item):

'''评论信息提取函数'''
def get_comment_url(article, news_infoXpath):

'''评论处理函数'''
def get_comment(comment_url, news_item):

(3)运行爬虫并格式化存储

①在settings.py中进行配置

import sys
# 这里改成爬虫项目的绝对路径,防止出现路径搜索的bug
sys.path.append('E:\Python\以前的项目\\NewsSpider-master\scrapyspider')

# 爬虫名称
BOT_NAME = 'scrapyspider'

# 设置是否服从网站的爬虫规则
ROBOTSTXT_OBEY = True

# 同时并发请求数,越大则爬取越快同时负载也大
CONCURRENT_REQUESTS = 32

#禁止cookies,防止被ban
COOKIES_ENABLED = False

# 输出的编码格式,由于Excel默认是ANSI编码,所以这里保持一致
# 如果有其他编码需求如utf-8等可自行更改
FEED_EXPORT_ENCODING = 'ANSI'

# 增加爬取延迟,降低被爬网站服务器压力
DOWNLOAD_DELAY = 0.01

# 爬取的新闻条数上限
CLOSESPIDER_ITEMCOUNT = 500

# 下载超时设定,超过10秒没响应则放弃当前URL
DOWNLOAD_TIMEOUT = 100
ITEM_PIPELINES = {
    'scrapyspider.pipelines.ScrapyspiderPipeline': 300,# pipeline中的类名
}

②运行爬虫并保存新闻内容

爬取下来的新闻内容及评论需要格式化存储,如果在IDE中运行debug脚本,则效果如下:

image.png

爬取后会保存为.csv文件,使用Excel打开即可查看:

image.png

image.png

③如果需要将评论单独提取出来,可以使用csv_process.py,效果如下:

image.png

四、其他补充

暂时没有

本实例支付的费用只是购买源码的费用,如有疑问欢迎在文末留言交流,如需作者在线代码指导、定制等,在作者开启付费服务后,可以点击“购买服务”进行实时联系,请知悉,谢谢
手机上随时阅读、收藏该文章 ?请扫下方二维码