iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 17
1
AI & Data

爬蟲在手、資料我有 - 30 天 Scrapy 爬蟲實戰系列 第 17

【Day 16】資料持久化 - RDBMS (3/4)

延續昨天的內容,今天會把回應的資料存到資料庫中~

建立資料表

建立 ithome_response 資料表,並定位相關欄位如下:

https://ithelp.ithome.com.tw/upload/images/20191001/201078759oaop2hmYN.png

https://ithelp.ithome.com.tw/upload/images/20191001/20107875SlAnWczz5x.png

https://ithelp.ithome.com.tw/upload/images/20191001/20107875hty7d7b1gW.png

這張表比較需要注意的是有建立一個外來鍵,作為跟原文的關聯。

執行程式

因為可能會有重複的資料,我們先清除昨天抓到的資料,明天會再說明怎麼過濾重複的資料。

https://ithelp.ithome.com.tw/upload/images/20191001/20107875qpB16Xf81z.png

今天的程式碼比較長,就不貼上來佔篇幅了,可以直接到 gist 上看。這邊會說明跟昨天的差異。

  1. insert_article() 方法中修改了執行的 SQL 語法,讓資料庫回傳 sequence id 供回應的外來鍵使用。
  2. 新增 crawl_response() 方法,邏輯跟 Day 13 文章中相同。
  3. 新增 insert_response() 方法,把回應新增到資料庫中。

執行程式後就可以在資料庫中看到抓下來的回應內容了!

https://ithelp.ithome.com.tw/upload/images/20191001/20107875FP1nzh25w6.png


上一篇
【Day 15】資料持久化 - RDBMS (2/4)
下一篇
【Day 17】資料持久化 - RDBMS (4/4)
系列文
爬蟲在手、資料我有 - 30 天 Scrapy 爬蟲實戰33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言