日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python的scrapy爬虫可以将爬去的数据放入数据库吗_Python基于Scrapy的爬虫 数据采集(写入数据库)...

發布時間:2025/4/16 python 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python的scrapy爬虫可以将爬去的数据放入数据库吗_Python基于Scrapy的爬虫 数据采集(写入数据库)... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

上一節已經學了如何在spider里面對網頁源碼進行數據過濾。

這一節將繼續學習scrapy的另一個組件-pipeline,用來2次處理數據

(本節中將以儲存到mysql數據庫為例子)

雖然scrapy架構下,可自定義的模塊很多,其實實現一個完整的scrapy爬蟲,僅僅只需要我們寫好

spider和pipeline,一個用來收集數據,一個用來處理數據

其他如下載中間件、引擎核心,都是自動運行的。

環境設置:

既然要寫入到MySQL,那得先讓python支持mysql的寫入工作,也就是先安裝mysql驅動pymysql

pip install pymysql

item是scrapy中,連結spider和pipeline的橋梁,

spider爬取了數據,過濾后寫入到item中,

再通過yield返回給核心引擎并交付給pipeline,

由pipeline建立到數據庫的連接并寫入

Item:

在Item.py中聲明需要傳入的數據

import scrapy

class MyItem(scrapy.Item):

content = scrapy.Field()

author = scrapy.Field()

tags = scrapy.Field()

是的,只需要這么幾行,聲明名字就可以

Spider:

對上一節的Spider01.py中的parse()函數進行一些修改

import scrapy

from MyScrapyProject.items import MyItem #注意這里的文件和類名都是自己定義的 要一致

class Spider01 (scrapy.Spider):

name= 'MyMainSpider'

start_urls=[

'http://quotes.toscrape.com/'

]

def parse(self,response):

quote_list=response.css('div.quote')

item = MyItem()#默認構造函數

for quote in quote_list:

print('Now loading a quote...')

content = quote.css('span.text::text').get()

item['content'] = content

author = quote.css('small.author::text').get()

item['author'] = author

tags = quote.css('a.tag::text').getall()

item['tags'] = ",".join(tags)#tags是字符串的列表,用join表示以‘,’為連接組成一個大串

#這里用getall() 不像上面用循環 根據需求來

yield item

# with open('text.txt','w') as f:

# for oneSentence in quote_list:

# f.write(oneSentence.css('span.text::text').get()+'\n')

# f.write(oneSentence.css('small.author::text').get()+'\n')

# tag_list=oneSentence.css('a.tag');

# for tag in tag_list:

# f.write(tag.css('::text').get()+' ')

# f.write('\n')

這邊記錄一個小問題,

在一個代碼塊中注釋大段代碼,有可能會導致奇怪的縮進錯誤,

所以這里把大段的注釋放在了最后

這里對yield進行一些說明,不一定完全準確:

在yield之前,已經對item封裝完畢了,通過yield返回給引擎,再傳給pipeline

pipeline對item處理完畢之后,回到parse繼續運行,這時會從yield的下一句開始,

也就是進入for語句的下一個循環

這樣的好處是保持只有一個item對象,節約空間和構造對象的時間

對于使用了pipeline的scrapy spider的parse中必須包含yield

這里的原因主要是,scrapy核心會對spider yield的返回值類型進行判斷,為request時會傳給一個放置request對象的隊列(由scrapy自己維護,我們不用管),而為item時才會傳給pipeline

pipeline:

這基本上可以當作模板使用,

不要忘記在setting.py中開啟pipeline!!!

這里初始是被注釋的

#from itemadapter import ItemAdapter

import pymysql.cursors

class MysqlPipeline:

def __init__(self):

self.mysql_url='localhost'

self.mysql_db='mydatabase'

def open_spider(self,spider):

self.mysql_conn = pymysql.connect(

host = self.mysql_url,

user = 'root',

password = 'xxxxxxxxx',#填寫你的mysql密碼

db = 'mydatabase',

charset = 'utf8mb4',

cursorclass = pymysql.cursors.DictCursor

)

def process_item(self, item, spider):

print('process the item')

try :

cursor = self.mysql_conn.cursor()

try:

sql_write = "INSERT INTO quotes (content, author, tags) VALUES (%s, %s, %s);"

cursor.execute(sql_write, (item.get("content", ""), item.get("author", ""), item.get("tags", "")))

cursor.connection.commit()

except Exception as e:

print('Something wrong with Table INSERT')

print(e)

except Exception as e:

print('Something wrong with MYSQL')

print(e)

return item

核心在cursor.execute() 和cursor.connection.commit()

一定一定一定一定一定要寫cursor.connection.commit()

本文地址:https://blog.csdn.net/Cake_C/article/details/107135741

如您對本文有疑問或者有任何想說的,請點擊進行留言回復,萬千網友為您解惑!

總結

以上是生活随笔為你收集整理的python的scrapy爬虫可以将爬去的数据放入数据库吗_Python基于Scrapy的爬虫 数据采集(写入数据库)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。