一步步教你用python的scrapy編寫一個爬蟲
介紹
本文將介紹我是如何在python爬蟲里面一步一步踩坑,然后慢慢走出來的,期間碰到的所有問題我都會詳細說明,讓大家以后碰到這些問題時能夠快速確定問題的來源,后面的代碼只是貼出了核心代碼,更詳細的代碼暫時沒有貼出來。
流程一覽
首先我是想爬某個網(wǎng)站上面的所有文章內(nèi)容,但是由于之前沒有做過爬蟲(也不知道到底那個語言最方便),所以這里想到了是用python來做一個爬蟲(畢竟人家的名字都帶有爬蟲的含義😄),我這邊是打算先將所有從網(wǎng)站上爬下來的數(shù)據(jù)放到ElasticSearch里面, 選擇ElasticSearch的原因是速度快,里面分詞插件,倒排索引,需要數(shù)據(jù)的時候查詢效率會非常好(畢竟爬的東西比較多😄),然后我會將所有的數(shù)據(jù)在ElasticSearch的老婆kibana里面將數(shù)據(jù)進行可視化出來,并且分析這些文章內(nèi)容,可以先看一下預期可視化的效果(上圖了),這個效果圖是kibana6.4系統(tǒng)給予的幫助效果圖(就是說你可以弄成這樣,我也想弄成這樣😁)。后面我會發(fā)一個dockerfile上來(現(xiàn)在還沒弄😳)。

環(huán)境需求
- Jdk (Elasticsearch需要)
- ElasticSearch (用來存儲數(shù)據(jù))
- Kinaba (用來操作ElasticSearch和數(shù)據(jù)可視化)
- Python (編寫爬蟲)
- Redis (數(shù)據(jù)排重)
這些東西可以去找相應的教程安裝,我這里只有ElasticSearch的安裝😢點我獲取安裝教程
第一步,使用python的pip來安裝需要的插件(第一個坑在這兒)
1.tomd:將html轉(zhuǎn)換成markdown
pip3 install tomd
2.redis:需要python的redis插件
pip3 install redis
3.scrapy:框架安裝(坑)
1、首先我是像上面一樣執(zhí)行了
pip3 install scrapy
2、然后發(fā)現(xiàn)缺少gcc組件 error: command 'gcc' failed with exit status 1

3、然后我就找啊找,找啊找,最后終于找到了正確的解決方法(期間試了很多錯誤答案😭)。最終的解決辦法就是使用yum來安裝python34-devel, 這個python34-devel根據(jù)你自己的python版本來,可能是python-devel,是多少版本就將中間的34改成你的版本, 我的是3.4.6
yum install python34-devel
4、安裝完成過后使用命令 scrapy 來試試吧。

第二步,使用scrapy來創(chuàng)建你的項目
輸入命令scrapy startproject scrapyDemo, 來創(chuàng)建一個爬蟲項目
liaochengdeMacBook-Pro:scrapy liaocheng$ scrapy startproject scrapyDemo New Scrapy project 'scrapyDemo', using template directory '/usr/local/lib/python3.7/site-packages/scrapy/templates/project', created in: /Users/liaocheng/script/scrapy/scrapyDemo You can start your first spider with: cd scrapyDemo scrapy genspider example example.com liaochengdeMacBook-Pro:scrapy liaocheng$
使用genspider來生成一個基礎的spider,使用命令scrapy genspider demo juejin.im, 后面這個網(wǎng)址是你要爬的網(wǎng)站,我們先爬自己家的😂
liaochengdeMacBook-Pro:scrapy liaocheng$ scrapy genspider demo juejin.im Created spider 'demo' using template 'basic' liaochengdeMacBook-Pro:scrapy liaocheng$
查看生成的目錄結(jié)構(gòu)

第三步,打開項目,開始編碼
查看生成的的demo.py的內(nèi)容
# -*- coding: utf-8 -*- import scrapy class DemoSpider(scrapy.Spider): name = 'demo' ## 爬蟲的名字 allowed_domains = ['juejin.im'] ## 需要過濾的域名,也就是只爬這個網(wǎng)址下面的內(nèi)容 start_urls = ['https://juejin.im/post/5c790b4b51882545194f84f0'] ## 初始url鏈接 def parse(self, response): ## 如果新建的spider必須實現(xiàn)這個方法 pass
可以使用第二種方式,將start_urls給提出來
# -*- coding: utf-8 -*- import scrapy class DemoSpider(scrapy.Spider): name = 'demo' ## 爬蟲的名字 allowed_domains = ['juejin.im'] ## 需要過濾的域名,也就是只爬這個網(wǎng)址下面的內(nèi)容 def start_requests(self): start_urls = ['http://juejin.im/'] ## 初始url鏈接 for url in start_urls: # 調(diào)用parse yield scrapy.Request(url=url, callback=self.parse) def parse(self, response): ## 如果新建的spider必須實現(xiàn)這個方法 pass
編寫articleItem.py文件(item文件就類似java里面的實體類)
import scrapy class ArticleItem(scrapy.Item): ## 需要實現(xiàn)scrapy.Item文件 # 文章id id = scrapy.Field() # 文章標題 title = scrapy.Field() # 文章內(nèi)容 content = scrapy.Field() # 作者 author = scrapy.Field() # 發(fā)布時間 createTime = scrapy.Field() # 閱讀量 readNum = scrapy.Field() # 點贊數(shù) praise = scrapy.Field() # 頭像 photo = scrapy.Field() # 評論數(shù) commentNum = scrapy.Field() # 文章鏈接 link = scrapy.Field()
編寫parse方法的代碼
def parse(self, response):
# 獲取頁面上所有的url
nextPage = response.css("a::attr(href)").extract()
# 遍歷頁面上所有的url鏈接,時間復雜度為O(n)
for i in nextPage:
if nextPage is not None:
# 將鏈接拼起來
url = response.urljoin(i)
# 必須是掘金的鏈接才進入
if "juejin.im" in str(url):
# 存入redis,如果能存進去,就是一個沒有爬過的鏈接
if self.insertRedis(url) == True:
# dont_filter作用是是否過濾相同url true是不過濾,false為過濾,我們這里只爬一個頁面就行了,不用全站爬,全站爬對對掘金不是很友好,我么這里只是用來測試的
yield scrapy.Request(url=url, callback=self.parse,headers=self.headers,dont_filter=False)
# 我們只分析文章,其他的內(nèi)容都不管
if "/post/" in response.url and "#comment" not in response.url:
# 創(chuàng)建我們剛才的ArticleItem
article = ArticleItem()
# 文章id作為id
article['id'] = str(response.url).split("/")[-1]
# 標題
article['title'] = response.css("#juejin > div.view-container > main > div > div.main-area.article-area.shadow > article > h1::text").extract_first()
# 內(nèi)容
parameter = response.css("#juejin > div.view-container > main > div > div.main-area.article-area.shadow > article > div.article-content").extract_first()
article['content'] = self.parseToMarkdown(parameter)
# 作者
article['author'] = response.css("#juejin > div.view-container > main > div > div.main-area.article-area.shadow > article > div:nth-child(6) > meta:nth-child(1)::attr(content)").extract_first()
# 創(chuàng)建時間
createTime = response.css("#juejin > div.view-container > main > div > div.main-area.article-area.shadow > article > div.author-info-block > div > div > time::text").extract_first()
createTime = str(createTime).replace("年", "-").replace("月", "-").replace("日","")
article['createTime'] = createTime
# 閱讀量
article['readNum'] = int(str(response.css("#juejin > div.view-container > main > div > div.main-area.article-area.shadow > article > div.author-info-block > div > div > span::text").extract_first()).split(" ")[1])
# 點贊數(shù)
article['badge'] = response.css("#juejin > div.view-container > main > div > div.article-suspended-panel.article-suspended-panel > div.like-btn.panel-btn.like-adjust.with-badge::attr(badge)").extract_first()
# 評論數(shù)
article['commentNum'] = response.css("#juejin > div.view-container > main > div > div.article-suspended-panel.article-suspended-panel > div.comment-btn.panel-btn.comment-adjust.with-badge::attr(badge)").extract_first()
# 文章鏈接
article['link'] = response.url
# 這個方法和很重要(坑),之前就是由于執(zhí)行yield article, pipeline就一直不能獲取數(shù)據(jù)
yield article
# 將內(nèi)容轉(zhuǎn)換成markdown
def parseToMarkdown(self, param):
return tomd.Tomd(str(param)).markdown
# url 存入redis,如果能存那么就沒有該鏈接,如果不能存,那么就存在該鏈接
def insertRedis(self, url):
if self.redis != None:
return self.redis.sadd("articleUrlList", url) == 1
else:
self.redis = self.redisConnection.getClient()
self.insertRedis(url)
編寫pipeline類,這個pipeline是一個管道,可以將所有yield關(guān)鍵字返回的數(shù)據(jù)都交給這個管道處理,但是需要在settings里面配置一下pipeline才行
from elasticsearch import Elasticsearch
class ArticlePipelines(object):
# 初始化
def __init__(self):
# elasticsearch的index
self.index = "article"
# elasticsearch的type
self.type = "type"
# elasticsearch的ip加端口
self.es = Elasticsearch(hosts="localhost:9200")
# 必須實現(xiàn)的方法,用來處理yield返回的數(shù)據(jù)
def process_item(self, item, spider):
# 這里是判斷,如果是demo這個爬蟲的數(shù)據(jù)才處理
if spider.name != "demo":
return item
result = self.checkDocumentExists(item)
if result == False:
self.createDocument(item)
else:
self.updateDocument(item)
# 添加文檔
def createDocument(self, item):
body = {
"title": item['title'],
"content": item['content'],
"author": item['author'],
"createTime": item['createTime'],
"readNum": item['readNum'],
"praise": item['praise'],
"link": item['link'],
"commentNum": item['commentNum']
}
try:
self.es.create(index=self.index, doc_type=self.type, id=item["id"], body=body)
except:
pass
# 更新文檔
def updateDocument(self, item):
parm = {
"doc" : {
"readNum" : item['readNum'],
"praise" : item['praise']
}
}
try:
self.es.update(index=self.index, doc_type=self.type, id=item["id"], body=parm)
except:
pass
# 檢查文檔是否存在
def checkDocumentExists(self, item):
try:
self.es.get(self.index, self.type, item["id"])
return True
except:
return False
第四步,運行代碼查看效果
使用scrapy list查看本地的所有爬蟲
liaochengdeMacBook-Pro:scrapyDemo liaocheng$ scrapy list demo liaochengdeMacBook-Pro:scrapyDemo liaocheng$
使用scrapy crawl demo來運行爬蟲
scrapy crawl demo
到kibana里面看爬到的數(shù)據(jù),執(zhí)行下面的命令可以看到數(shù)據(jù)
GET /article/_search
{
"query": {
"match_all": {}
}
}
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "article2",
"_type": "type",
"_id": "5c790b4b51882545194f84f0",
"_score": 1,
"_source": {}
}
]
}
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。
相關(guān)文章
python如何通過twisted實現(xiàn)數(shù)據(jù)庫異步插入
這篇文章主要為大家詳細介紹了python如何通過twisted實現(xiàn)數(shù)據(jù)庫異步插入,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
Python 協(xié)程與 JavaScript 協(xié)程的對比
當漸漸對 JavaScript 了解后,一查發(fā)現(xiàn) Python 和 JavaScript 的協(xié)程發(fā)展史簡直就是一毛一樣!接下來小編就大致做下橫向?qū)Ρ群涂偨Y(jié),便于對這兩個語言有興趣的新人理解和吸收。2021-09-09
Face++ API實現(xiàn)手勢識別系統(tǒng)設計
這篇文章主要為大家詳細介紹了Face++ API實現(xiàn)手勢識別系統(tǒng)設計,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11
python3?chromedrivers簽到的簡單實現(xiàn)
本文主要介紹了python3?chromedrivers簽到的簡單實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03

