大数据时代下的Scrapy爬虫框架
生活随笔
收集整理的這篇文章主要介紹了
大数据时代下的Scrapy爬虫框架
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 前言
- 一、Scrapy是什么?
- 二、使用步驟
- 1.安裝Scrapy
- 2.創建Scrapy項目
- 3.Scrapy架構圖
- 三.實戰項目:爬取豆瓣電影TOP250電影信息
- 1.items.py
- 2.pipelines.py
- 3.douban_spider.py
- 4.運行結果
前言
隨著大數據時代的來臨,數據對一個企業越來越重要,沒有數據的支撐,那么這個企業必然會落后于其它企業,那么怎么樣獲取數據呢?本篇文章將告訴你如何從互聯網上抓取有用的數據并持久化存儲
一、Scrapy是什么?
Scrapy 是一套基于基于Twisted的異步處理框架,純python實現的爬蟲框架,用戶只需要定制開發幾個模塊就可以輕松的實現一個爬蟲,用來抓取網頁內容以及各種圖片,非常之方便~
二、使用步驟
1.安裝Scrapy
pip install scrapy2.創建Scrapy項目
scrapy startproject 項目名3.Scrapy架構圖
Item Pipeline:可以簡稱為數據結構,即要存儲的數據的結構,可以理解為面向對象中的類,這個模塊在Spiders模塊解析后,會進行回調。
Spiders:數據解析模塊,即在此模塊中,只是做對數據的解析,并提取鏈接信息發送給Scheduler模塊進行排隊。
Downloader:下載模塊,只做數據請求,并將返回的數據放入Spiders中解析。
Scheduler:隊列模塊,只負責對請求的鏈接進行排序并發送給Downloader.
三.實戰項目:爬取豆瓣電影TOP250電影信息
1.items.py
該模塊對應items模塊
# Define here the models for your scraped items # # See documentation in: # https://docs.scrapy.org/en/latest/topics/items.htmlimport scrapy class DoubanItem(scrapy.Item):# define the fields for your item here like:# name = scrapy.Field()##電影序號movie_number=scrapy.Field()##電影名字movie_name=scrapy.Field()##電影信息movie_tostar=scrapy.Field()##星級movie_star=scrapy.Field()##評論人數movie_evaluate=scrapy.Field()##電影介紹movie_introduction=scrapy.Field()2.pipelines.py
# Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html# useful for handling different item types with a single interface from itemadapter import ItemAdapter import pymongoclass DoubanPipeline:def __init__(self) -> None:host="mongodb://localhost"prot="27017"dbname="movie"client=pymongo.MongoClient("mongodb://localhost:27017")db=client[dbname]self.movie_table=db['movie_table']def process_item(self, item, spider):print(item)data=dict(item)self.movie_table.insert_one(data)return item3.douban_spider.py
import scrapy from douban.items import DoubanItemclass DoubanSpiderSpider(scrapy.Spider):##爬蟲名字name = 'douban_spider'##允許的域名allowed_domains = ['movie.douban.com']##入口urlstart_urls = ['https://movie.douban.com/top250']def parse(self, response):next=response.xpath('//*[@id="content"]/div/div[1]/div[2]/span[3]/a/@href').extract()my_list=response.xpath('//*[@id="content"]/div/div[1]/ol/li')for item in my_list:my_item=DoubanItem()my_item['movie_number']=item.xpath('./div[@class="item"]//em/text()').extract_first()my_item['movie_name']=item.xpath('./div[@class="item"]/div[@class="info"]//a/span[1]/text()').extract_first()my_item['movie_tostar']=item.xpath('./div[@class="item"]/div[@class="info"]//div[@class="bd"]/p/text()').extract_first()my_item['movie_star']=item.xpath('./div[@class="item"]/div[@class="info"]//div[@class="star"]/span[2]/text()').extract_first()my_item['movie_evaluate']=item.xpath('./div[@class="item"]/div[@class="info"]//div[@class="star"]/span[4]/text()').extract_first()my_item['movie_introduction']=item.xpath('./div[@class="item"]/div[@class="info"]//p[@class="quote"]/span[1]/text()').extract_first()yield my_itemif next:yield scrapy.Request("https://movie.douban.com/top250"+next[0],self.parse)4.運行結果
附帶源碼:下載源碼
總結
以上是生活随笔為你收集整理的大数据时代下的Scrapy爬虫框架的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ORA-27101 shared mem
- 下一篇: node 遍历读取制定后缀文件名