python批量下载csdn文章

声明:该爬虫只可用于提高自己学习、工作效率,请勿用于非法用途,否则后果自负

功能概述:

  1. 根据待爬文章url(文章id)批量保存文章到本地;
  2. 支持将文中图片下载到本地指定文件夹;
  3. 多线程爬取;

1.爬取效果展示

本次示例爬取的链接地址:
https://blog.csdn.net/m0_68111267/article/details/132574687

原文效果:

爬取效果:

文件列表:

2.编写代码

爬虫使用scrapy框架编写,分布式、多线程

2.1编写Items

class ArticleItem(scrapy.Item):

    id = scrapy.Field()  # ID
    title = scrapy.Field()
    html = scrapy.Field()  # html


class ImgDownloadItem(scrapy.Item):

    img_src = scrapy.Field()
    img_name = scrapy.Field()
    image_urls = scrapy.Field()


class LinkIdsItem(scrapy.Item):

    id = scrapy.Field()

2.2添加管道



class ArticlePipeline():

    def open_spider(self, spider):
        if spider.name == 'csdnSpider':

            data_dir = os.path.join(settings.DATA_URI)
            #判断文件夹存放的位置是否存在,不存在则新建文件夹
            if not os.path.exists(data_dir):
                os.makedirs(data_dir)
            self.data_dir = data_dir

    def close_spider(self, spider):  # 在关闭一个spider的时候自动运行
        pass
        # if spider.name == 'csdnSpider':
        #     self.file.close()

    def process_item(self, item, spider):
        try:
            if spider.name == 'csdnSpider' and item['key'] == 'article':
                info = item['info']
                id = info['id']
                title = info['title']
                html = info['html']

                f = open(self.data_dir + '/{}.html'.format(title),
                         'w',
                         encoding="utf-8")
                f.write(html)
                f.close()
        except BaseException as e:
            print("Article错误在这里>>>>>>>>>>>>>", e, "<<<<<<<<<<<<<错误在这里")
        return item

2.3添加配置

2.4添加解析器


    ...
    
    def parse(self, response):
        html = response.body
        a_id = response.meta['a_id']
        soup = BeautifulSoup(html, 'html.parser')

        [element.extract() for element in soup('script')]
        [element.extract() for element in soup.select("head style")]
        [element.extract() for element in soup.select("html > link")]

        # 删除style中包含隐藏的标签
        [
            element.extract() for element in soup.find_all(
                style=re.compile(r'.*display:none.*?'))
        ]
        
        ...

3.获取完整源码

项目说明文档

爱学习的小伙伴,本次案例的完整源码,已上传微信公众号“一个努力奔跑的snail”,后台回复“csdn”即可获取。

源码地址:

https://pan.baidu.com/s/1uLBoygwQGTSCAjlwm13mog?pwd=****

提取码: ****

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玛卡`三少

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值