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

歡迎訪問 生活随笔!

生活随笔

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

python

手把手带你入门Python爬虫(四、ORM与peewee)

發布時間:2025/3/15 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 手把手带你入门Python爬虫(四、ORM与peewee) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ORM與peewee

  • 一、為什么要用ORM
  • 二、ORM的選擇
  • 三、peewee使用
    • 1. 安裝
    • 2. 創建并使用
    • 3. 增刪查改
      • (1) 新增
      • (2) 查詢數據
      • (3) 修改數據
      • (4) 刪除數據

一、為什么要用ORM

  • 隔離數據庫和數據庫版本之間的差異
  • 便于維護
  • ORM會提供防sql注入等功能
  • 變量傳遞式的調用更加簡單
  • ORM越來越流行

二、ORM的選擇

框架優點缺點
peeweeDjango式的API,使其易用 輕量實現,很容易和任意web框架集成不支持自動化 schema 遷移 多對多查詢寫起來不直觀
SQLObject采用了易懂的ActiveRecord 模式 ;一個相對較小的代碼庫方法和類的命名遵循了Java 的小駝峰風格 ;不支持數據庫session隔離工作單元
Storm清爽輕量的API,短學習曲線和長期可維護性;不需要特殊的類構造函數,也沒有必要的基類迫使程序員手工寫表格創建的DDL語句,而不是從模型類自動派生;Storm的貢獻者必須把他們的貢獻的版權給Canonical公司
Django’s ORM易用,學習曲線短;和Django緊密集合,用Django時使用約定俗成的方法去操作數據庫不好處理復雜的查詢,強制開發者回到原生SQL;緊密和Django集成,使得在Django環境外很難使用
SQLAlchemy企業級 API,使得代碼有健壯性和適應性;靈活的設計,使得能輕松寫復雜查詢工作單元概念不常見;重量級 API,導致長學習曲線

我們選擇peewee這個框架來學習,因為它簡單、靈活、申明方式和django的ORM接近。且star數量高,活躍度高,文檔質量高。

官方文檔:http://docs.peewee-orm.com/en/latest/

三、peewee使用

1. 安裝

切換到虛擬環境,然后安裝

pip install peewee

2. 創建并使用

from peewee import *db = MySQLDatabase("py_spider", host="localhost", port=3307, user="root", password="root")class Person(Model):name = CharField()birthday = DateField()class Meta:database = db # This model uses the "people.db" database.if __name__ == "__main__":db.create_tables([Person]) # 根據模型創建數據表

生成的數據表,表名默認為類名,默認會加一個ID字段(主鍵):

Field types table(數據庫與模型字段對應表)

Field TypeSqlitePostgresqlMySQL
AutoFieldintegerserialinteger
BigAutoFieldintegerbigserialbigint
IntegerFieldintegerintegerinteger
BigIntegerFieldintegerbigintbigint
SmallIntegerFieldintegersmallintsmallint
IdentityFieldnot supportedint identitynot supported
FloatFieldrealrealreal
DoubleFieldrealdouble precisiondouble precision
DecimalFielddecimalnumericnumeric
CharFieldvarcharvarcharvarchar
FixedCharFieldcharcharchar
TextFieldtexttexttext
BlobFieldblobbyteablob
BitFieldintegerbigintbigint
BigBitFieldblobbyteablob
UUIDFieldtextuuidvarchar(40)
BinaryUUIDFieldblobbyteavarbinary(16)
DateTimeFielddatetimetimestampdatetime
DateFielddatedatedate
TimeFieldtimetimetime
TimestampFieldintegerintegerinteger
IPFieldintegerbigintbigint
BooleanFieldintegerbooleanbool
BareFielduntypednot supportednot supported
ForeignKeyFieldintegerintegerinteger

3. 增刪查改

(1) 新增

if __name__ == "__main__":# db.create_tables([Person]) # 創建數據表from datetime import date# 生成數據bob = Person(name="Bob", birthday=date(2020, 12, 12))# 新增數據到數據庫bob.save()

(2) 查詢數據

if __name__ == "__main__":# 只查詢一條數據 get方法在取不到數據會拋出異常,需try catchBob = Person.select().where(Person.name == 'Bob').get()print(Bob.name) # Bobprint(Bob.birthday) # 2020-12-12# 同上Bob2 = Person.get(Person.name == 'Bob')print(Bob2.name) # Bobprint(Bob2.birthday) # 2020-12-12# 查詢多條數據Bobs = Person.select().where(Person.name == 'Bob')for b in Bobs:print(b.name)print(b.birthday)

(3) 修改數據

if __name__ == "__main__":from datetime import date# 修改數據Bobs = Person.select().where(Person.name == 'Bob')for b in Bobs:b.birthday = date(1997, 10, 16)b.save() # 在沒有數據的時候新增,存在的時候修改

(4) 刪除數據

if __name__ == "__main__":# 刪除數據Bobs = Person.select().where(Person.name == 'Bob')for b in Bobs:b.delete_instance()

總結

以上是生活随笔為你收集整理的手把手带你入门Python爬虫(四、ORM与peewee)的全部內容,希望文章能夠幫你解決所遇到的問題。

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