python實(shí)戰(zhàn)項(xiàng)目scrapy管道學(xué)習(xí)爬取在行高手?jǐn)?shù)據(jù)_第1頁
python實(shí)戰(zhàn)項(xiàng)目scrapy管道學(xué)習(xí)爬取在行高手?jǐn)?shù)據(jù)_第2頁
python實(shí)戰(zhàn)項(xiàng)目scrapy管道學(xué)習(xí)爬取在行高手?jǐn)?shù)據(jù)_第3頁
python實(shí)戰(zhàn)項(xiàng)目scrapy管道學(xué)習(xí)爬取在行高手?jǐn)?shù)據(jù)_第4頁
python實(shí)戰(zhàn)項(xiàng)目scrapy管道學(xué)習(xí)爬取在行高手?jǐn)?shù)據(jù)_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第python實(shí)戰(zhàn)項(xiàng)目scrapy管道學(xué)習(xí)爬取在行高手?jǐn)?shù)據(jù)爬取目標(biāo)站點(diǎn)分析

本次采集的目標(biāo)站點(diǎn)為:/falcon/mentors,目標(biāo)數(shù)據(jù)為在行高手?jǐn)?shù)據(jù)。

本次數(shù)據(jù)保存到MySQL數(shù)據(jù)庫中,基于目標(biāo)數(shù)據(jù),設(shè)計(jì)表結(jié)構(gòu)如下所示。

對比表結(jié)構(gòu),可以直接將scrapy中的items.py文件編寫完畢。

classZaihangItem(scrapy.Item):

#definethefieldsforyouritemherelike:

name=scrapy.Field()#姓名

city=scrapy.Field()#城市

industry=scrapy.Field()#行業(yè)

price=scrapy.Field()#價(jià)格

chat_nums=scrapy.Field()#聊天人數(shù)

score=scrapy.Field()#評分

項(xiàng)目的創(chuàng)建過程參考上一案例即可,本文直接從采集文件開發(fā)進(jìn)行編寫,該文件為zh.py。

本次目標(biāo)數(shù)據(jù)分頁地址需要手動(dòng)拼接,所以提前聲明一個(gè)實(shí)例變量(字段),該字段為page,每次響應(yīng)之后,判斷數(shù)據(jù)是否為空,如果不為空,則執(zhí)行+1操作。

請求地址模板如下:

/falcon/mentorsfirst_tag_id=479first_tag_name=心理page={}

當(dāng)頁碼超過最大頁數(shù)時(shí),返回如下頁面狀態(tài),所以數(shù)據(jù)為空狀態(tài),只需要判斷是否存在>

解析數(shù)據(jù)與數(shù)據(jù)清晰直接參考下述代碼即可。

importscrapy

fromzaihang_spider.itemsimportZaihangItem

classZhSpider(scrapy.Spider):

name=zh

allowed_domains=[]

page=1#起始頁碼

url_format=/falcon/mentorsfirst_tag_id=479first_tag_name=%E5%BF%83%E7%90%86page={}#模板

start_urls=[url_format.format(page)]

defparse(self,response):

empty=response.css(section.empty)#判斷數(shù)據(jù)是否為空

iflen(empty)0:

return#存在空標(biāo)簽,直接返回

mentors=response.css(.mentor-boarda)#所有高手的超鏈接

forminmentors:

item=ZaihangItem()#實(shí)例化一個(gè)對象

name=m.css(.mentor-card__name::text).extract_first()

city=m.css(.mentor-card__location::text).extract_first()

industry=m.css(.mentor-card__title::text).extract_first()

price=self.replace_space(m.css(.mentor-card__price::text).extract_first())

chat_nums=self.replace_space(m.css(.mentor-card__number::text).extract()[0])

score=self.replace_space(m.css(.mentor-card__number::text).extract()[1])

#格式化數(shù)據(jù)

item[name]=name

item[city]=city

item[industry]=industry

item[price]=price

item[chat_nums]=chat_nums

item[score]=score

yielditem

#再次生成一個(gè)請求

self.page+=1

next_url=format(self.url_format.format(self.page))

yieldscrapy.Request(url=next_url,callback=self.parse)

defreplace_space(self,in_str):

in_str=in_str.replace(\n,).replace(\r,).replace(¥,)

returnin_str.strip()

開啟settings.py文件中的ITEM_PIPELINES,注意類名有修改

ITEM_PIPELINES={

zaihang_spider.pipelines.ZaihangMySQLPipeline:300,

修改pipelines.py文件,使其能將數(shù)據(jù)保存到MySQL數(shù)據(jù)庫中

在下述代碼中,首先需要了解類方法from_crawler,該方法是__init__的一個(gè)代理,如果其存在,類被初始化時(shí)會被調(diào)用,并得到全局的crawler,然后通過crawler就可以獲取settings.py中的各個(gè)配置項(xiàng)。

除此之外,還存在一個(gè)from_settings方法,一般在官方插件中也有應(yīng)用,示例如下所示。

@classmethod

deffrom_settings(cls,settings):

host=settings.get(HOST)

returncls(host)

@classmethod

deffrom_crawler(cls,crawler):

#FIXME:fornow,statsareonlysupportedfromthisconstructor

returncls.from_settings(crawler.settings)

在編寫下述代碼前,需要提前在settings.py中寫好配置項(xiàng)。

settings.py文件代碼

HOST=

PORT=3306

USER=root

PASSWORD=123456

DB=zaihang

pipelines.py文件代碼

importpymysql

classZaihangMySQLPipeline:

def__init__(self,host,port,user,password,db):

self.host=host

self.port=port

self.user=user

self.password=password

self.db=db

self.conn=None

self.cursor=None

@classmethod

deffrom_crawler(cls,crawler):

returncls(

host=crawler.settings.get(HOST),

port=crawler.settings.get(PORT),

user=crawler.settings.get(USER),

password=crawler.settings.get(PASSWORD),

db=crawler.settings.get(DB)

defopen_spider(self,spider):

self.conn=pymysql.connect(host=self.host,port=self.port,user=self.user,password=self.password,db=self.db)

defprocess_item(self,item,spider):

#print(item)

#存儲到MySQL

name=item[name]

city=item[city]

industry=item[industry]

price=item[price]

chat_nums=item[chat_nums]

score=item[score]

sql=insertintousers(name,city,industry,price,chat_nums,score)values(%s,%s,%s,%.1f,%d,%.1f)%(

name,city,industry,float(price),int(chat_nums),float(score))

print(sql)

self.cursor=self.conn.cursor()#設(shè)置游標(biāo)

try:

self.cursor.execute(sql)#執(zhí)行sql

mit()

exceptExceptionase:

print(e)

self.conn.rollback()

returnitem

defclose_spider(self,spider):

self.cursor.close()

self.conn.close()

管道文件中三個(gè)重要函數(shù),分別是open_spider,process_item,close_spider。

#爬蟲開啟時(shí)執(zhí)行,只執(zhí)行一次

defopen_spider(self,spider):

#=橡皮擦#spider對象動(dòng)態(tài)添加實(shí)例變量,可以在spider模塊中獲取該變量值,比如在parse(self,response)函數(shù)中通過self獲取屬性

#一些初始化動(dòng)作

pass

#處理提取的數(shù)據(jù),數(shù)據(jù)保存代碼編寫位置

defprocess_

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論