当前位置: 首页 > news >正文

Scrapy爬取heima论坛所有页面内容并保存到数据库中

前期准备:

Scrapy入门_win10安装scrapy-CSDN博客

新建 Scrapy项目

scrapy startproject mySpider03   # 项目名为mySpider03

进入到spiders目录

 cd mySpider03/mySpider03/spiders

创建爬虫

 scrapy genspider heima  bbs.itheima.com      # 爬虫名为heima ,爬取域为bbs.itheima.com

 制作爬虫

items.py:

import scrapyclass heimaItem(scrapy.Item):title = scrapy.Field()url = scrapy.Field()

heima.py:

import scrapy
from scrapy.selector import Selector
from mySpider03.items import heimaItemclass HeimaSpider(scrapy.Spider):name = 'heima'allowed_domains = ['bbs.itheima.com']start_urls = ['http://bbs.itheima.com/forum-425-1.html']def parse(self, response):print('response.url: ', response.url)selector = Selector(response)node_list = selector.xpath("//th[@class='new forumtit'] | //th[@class='common forumtit']")for node in node_list:# 文章标题title = node.xpath('./a[1]/text()')[0].extract()# 文章链接url = node.xpath('./a[1]/@href')[0].extract()# 创建heimaItem类item = heimaItem()item['title'] = titleitem['url'] = urlyield item

pipelines.py:

from itemadapter import ItemAdapter
from pymongo import MongoClientclass heimaPipeline:def open_spider(self, spider):# MongoDB 连接设置  self.MONGO_URI = 'mongodb://localhost:27017/'  self.DB_NAME = 'heima'  # 数据库名称  self.COLLECTION_NAME = 'heimaNews'  # 集合名称self.client = MongoClient(self.MONGO_URI)self.db = self.client[self.DB_NAME]self.collection = self.db[self.COLLECTION_NAME]# 如果集合中已有数据,清空集合self.collection.delete_many({})print('爬取开始')def process_item(self, item, spider):title = item['title']url = item['url']# 将item转换为字典item_dict = {'title': title,'url': url,}# 插入数据self.collection.insert_one(item_dict)return item   def close_spider(self, spider):print('爬取结束,显示数据库中所有元素')cursor = self.collection.find()for document in cursor:print(document)self.client.close()

settings.py,解开ITEM_PIPELINES的注释,并修改其内容:

ITEM_PIPELINES = {

   'mySpider03.pipelines.heimaPipeline': 300,

}

创建run.py:

from scrapy import cmdlinecmdline.execute("scrapy crawl heima -s LOG_ENABLED=False".split())# cd mySpider03/mySpider03/spiders

运行run.py文件,即可实现爬取第一页'http://bbs.itheima.com/forum-425-1.html'内容并保存到数据库中的功能。

结果如下图:

爬取到了50条数据。

爬取所有页面

方法一:通过获取下一页url地址的方法爬取所有页面。

在heima.py的parse方法结尾加上以下内容:

        # 获取下一页的链接

        if '下一页' in response.text:

            next_url = selector.xpath("//a[@class='nxt']/@href").extract()[0]

            yield scrapy.Request(next_url, callback=self.parse)

即heima.py:

import scrapy
from scrapy.selector import Selector
from mySpider03.items import heimaItemclass HeimaSpider(scrapy.Spider):name = 'heima'allowed_domains = ['bbs.itheima.com']start_urls = ['http://bbs.itheima.com/forum-425-1.html']def parse(self, response):print('response.url: ', response.url)selector = Selector(response)node_list = selector.xpath("//th[@class='new forumtit'] | //th[@class='common forumtit']")for node in node_list:# 文章标题title = node.xpath('./a[1]/text()')[0].extract()# 文章链接url = node.xpath('./a[1]/@href')[0].extract()# 创建heimaItem类item = heimaItem()item['title'] = titleitem['url'] = urlyield item# 获取下一页的链接if '下一页' in response.text:next_url = selector.xpath("//a[@class='nxt']/@href").extract()[0]yield scrapy.Request(next_url, callback=self.parse)

爬取结果:

爬取到了70页,一共3466条数据。

# 在cmd中输入以下命令,查看数据库中的数据:
 > mongosh      # 启动mongoDB
 > show dbs      # 查看所有数据库
 > use heima      # 使用heima数据库
 > db.stats()        # 查看当前数据库的信息
 > db.heimaNews.find()        # 查看heimaNews集合中的所有文档

方法二:使用crawlspider提取url链接

新建crawlspider类的爬虫

scrapy genspider -t crawl heimaCrawl  bbs.itheima.com     

# 爬虫名为heimaCrawl ,爬取域为bbs.itheima.com

2.1在rules中通过xpath提取链接

修改heimaCrawl.py文件:

import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from mySpider03.items import heimaItemclass HeimacrawlSpider(CrawlSpider):name = 'heimaCrawl'allowed_domains = ['bbs.itheima.com']start_urls = ['http://bbs.itheima.com/forum-425-1.html']rules = (Rule(LinkExtractor(restrict_xpaths=r'//a[@class="nxt"]'), callback='parse_item', follow=True),)# 处理起始页面内容,如果不重写该方法,则只爬取满足rules规则的链接,不会爬取起始页面内容def parse_start_url(self, response):# 调用 parse_item 处理起始页面return self.parse_item(response)def parse_item(self, response):print('CrawlSpider的response.url: ', response.url)node_list = response.xpath("//th[@class='new forumtit'] | //th[@class='common forumtit']")for node in node_list:# 文章标题title = node.xpath('./a[1]/text()')[0].extract()# 文章链接url = node.xpath('./a[1]/@href')[0].extract()# 创建heimaItem类item = heimaItem()item['title'] = titleitem['url'] = urlyield item

修改run.py:

# heimaCrawl

cmdline.execute("scrapy crawl heimaCrawl -s LOG_ENABLED=False".split())

爬取结果:

爬取到全部70页,一共3466条数据。

2.2在rules中通过正则表达式提取链接 

修改heimaCrawl.py文件:

    rules = (

        Rule(LinkExtractor(allow=r'forum-425-\d+\.html'), callback='parse_item', follow=True),

    )

 结果:

一共爬取到3516条数据。

http://www.lryc.cn/news/482310.html

相关文章:

  • Kafka参数了解
  • sql专题 之 where和join on
  • day12:版本控制器
  • 第四十一章 Vue之初识VueX
  • GIT的基本使用与进阶
  • 【Linux系统】—— 基本指令(二)
  • MFC工控项目实例三十实现一个简单的流程
  • 【Android、IOS、Flutter、鸿蒙、ReactNative 】文本点击事件
  • json转excel,读取json文件写入到excel中【rust语言】
  • Java面试要点06 - static关键字、静态属性与静态方法
  • 动态规划-背包问题——416.分割等和子集
  • Pr:视频过渡快速参考(合集 · 2025版)
  • 网络安全---安全见闻2
  • 解决因为TortoiseSVN未安装cmmand line client tools组件,导致idea无法使用svn更新、提交代码
  • Ubuntu 20.04安装CUDA 11.0、cuDNN 8.0.5
  • 鸿蒙 APP 发布上架
  • 【C++笔记】C++三大特性之继承
  • 如何在CentOS 7上搭建SMB服务
  • linux详解,基本网络枚举
  • 5G智能对讲终端|北斗有源终端|北斗手持机|单兵|单北斗
  • 第七部分:2. STM32之ADC实验--AD多通道(AD采集三路传感器模块实验:光敏传感器、热敏传感器、反射式传感器附赠温湿度传感器教程)
  • js.零钱兑换
  • GitHub 上的开源项目推荐
  • 实现Reactor反应堆模型:框架搭建
  • UE5 样条线组件(未完待续)
  • 计算机网络常见面试题(一):TCP/IP五层模型、TCP三次握手、四次挥手,TCP传输可靠性保障、ARQ协议
  • sql速度优化多条合并为一条语句
  • 用 PHP或Python加密字符串,用iOS解密
  • docker容器启动报错error creating overlay mount to /var/lib/docker/overlay2解决办法
  • 人工智能在智能家居中的应用