Python第一個爬蟲,爬取噹噹網 Top 500 本五星好評書籍
來啦,老弟

我們已經知道怎麼使用
Requests]
進行各種請求騷操作
也知道了對伺服器返回的資料如何使用
正規表示式
來過濾我們想要的內容
...
那麼接下來
我們就使用 requests 和 re 來寫一個爬蟲
作為一個愛看書的你(說的跟真的似的)
學習過程中有不懂的可以加入我們的學習交流秋秋圈784中間758後面214,與你分享Python企業當下人才需求及怎麼從零基礎學習Python,和學習什麼內容。相關學習視訊資料、開發工具都有分享

怎麼能發現好書呢?
所以我們
爬取噹噹網的前 **500 **本好五星評書籍
怎麼樣?

ok
接下來就是
學習 python 的正確姿勢
請在電腦的陪同下
邊看本文邊練習
首先我們要對我們的目標網站進行分析
先摸清對方的底
我們才能戰無不勝

開啟這個書籍排行榜的地址
http://bang.dangdang.com/books/fivestars/01.00.00.00.00.00-recent30-0-0-1-1
我們可以看到是這樣的一個網頁

每一頁顯示 20 本書
當我們點選下一頁的時候
你可以發現地址變了
http://bang.dangdang.com/books/fivestars/01.00.00.00.00.00-recent30-0-0-1-2
也就是我們翻到第幾頁的時候
連結地址的最後一個引數會跟著變
那麼我們等會在 python 中可以用一個變數
來實現獲取不同頁數的內容

接著
用我們之前說的 Chrome 騷操作
來分析一下
我們要的內容是怎麼請求的
以及
返回給我們的原始碼是什麼樣的

可以看到
我們通過 GET 請求

我們的請求頭

這是伺服器返回來的資料

接著我們再來分析一下我們要抓取的關鍵資訊

我們要的就是前 500 本書的
排名
書名
圖片地址
作者
推薦指數
五星評分次數
價格
通過原始碼我們可以看到
這些資訊被放在了 <li> 標籤中


那麼我們等會就可以使用
來進行過濾我們要的資訊
一頓分析完了之後
接下來擼程式碼了

主要思路
使用 page 變數來實現翻頁
我們使用 requests 請求噹噹網
然後將返回的 HTML 進行正則解析
由於我們暫時還沒學到 資料庫
所以解析完之後就把內容存到檔案中
def main(page): url = 'http://bang.dangdang.com/books/fivestars/01.00.00.00.00.00-recent30-0-0-1-' + str(page) html = request_dandan(url) items = parse_result(html) # 解析過濾我們想要的資訊 for item in items: write_item_to_file(item)

請求噹噹網
當我們請求成功之後
拿到原始碼
def request_dandan(url): try: response = requests.get(url) if response.status_code == 200: return response.text except requests.RequestException: return None

拿到原始碼了
就要對其解析
使用正規表示式獲取我們想要的關鍵資訊
獲取到了之後我們封裝一下資料
def parse_result(html): pattern = re.compile('<li>.*?list_num.*?(\d+).</div>.*?<img src="(.*?)".*?class="name".*?title="(.*?)">.*?class="star">.*?class="tuijian">(.*?)</span>.*?class="publisher_info">.*?target="_blank">(.*?)</a>.*?class="biaosheng">.*?<span>(.*?)</span></div>.*?<p><span\sclass="price_n">¥(.*?)</span>.*?</li>',re.S) items = re.findall(pattern,html) for item in items: yield { 'range': item[0], 'iamge': item[1], 'title': item[2], 'recommend': item[3], 'author': item[4], 'times': item[5], 'price': item[6] }

列印一下看看結果
for item in items: print(item)

可以看到這樣的資料

沒毛病
現在我們獲取的是第 1 頁的資料
如何自動獲取 25 頁 500 條資料呢
來個 for 迴圈唄
if __name__ == "__main__": for i in range(1,26): main(i)

獲取完 500 本書的資料之後
存到 book.txt 檔案
def write_item_to_file(item): print('開始寫入資料 ====> ' + str(item)) with open('book.txt', 'a', encoding='UTF-8') as f: f.write(json.dumps(item, ensure_ascii=False) + '\n') f.close()

完成
專案跑起來


開啟我們儲存的 book.txt 看看

前 500 本書的資料就被我們拿到啦

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69913713/viewspace-2651170/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python爬蟲教程+書籍分享Python爬蟲
- Python 第一個爬蟲,爬取 147 小說Python爬蟲
- Python爬取噹噹網APP資料PythonAPP
- 【python爬蟲案例】利用python爬取豆瓣讀書評分TOP250排行資料Python爬蟲
- 手機版python爬取網頁書籍Python網頁
- python爬蟲---網頁爬蟲,圖片爬蟲,文章爬蟲,Python爬蟲爬取新聞網站新聞Python爬蟲網頁網站
- 2019最新Python爬蟲教程+書籍分享Python爬蟲
- python爬蟲初探--第一個python爬蟲專案Python爬蟲
- python爬蟲練習之爬取豆瓣讀書所有標籤下的書籍資訊Python爬蟲
- 【python爬蟲案例】利用python爬取豆瓣電影TOP250評分排行資料!Python爬蟲
- Java爬蟲系列之實戰:爬取酷狗音樂網 TOP500 的歌曲Java爬蟲
- Python爬蟲小專案:爬一個圖書網站Python爬蟲網站
- Python爬蟲—爬取某網站圖片Python爬蟲網站
- 【Python爬蟲】正則爬取趕集網Python爬蟲
- Python網路爬蟲實踐案例:爬取貓眼電影Top100Python爬蟲
- python爬蟲學習01--電子書爬取Python爬蟲
- python網路爬蟲_Python爬蟲:30個小時搞定Python網路爬蟲視訊教程Python爬蟲
- 不會Python爬蟲?教你一個通用爬蟲思路輕鬆爬取網頁資料Python爬蟲網頁
- python爬取365好書中小說Python
- python網路爬蟲--爬取淘寶聯盟Python爬蟲
- 精通Scrapy網路爬蟲【一】第一個爬蟲專案爬蟲
- 爬蟲教程——用Scrapy爬取豆瓣TOP250爬蟲
- python例項,python網路爬蟲爬取大學排名!Python爬蟲
- 爬蟲利器Pyppeteer的介紹和使用 爬取京東商城書籍資訊爬蟲
- 用python寫一個豆瓣短評通用爬蟲(登入、爬取、視覺化)Python爬蟲視覺化
- Python爬蟲實踐--爬取網易雲音樂Python爬蟲
- 用PYTHON爬蟲簡單爬取網路小說Python爬蟲
- java爬取豆瓣書籍資訊Java
- 【Python學習】爬蟲爬蟲爬蟲爬蟲~Python爬蟲
- Python爬蟲入門【9】:圖蟲網多執行緒爬取Python爬蟲執行緒
- python 爬蟲 爬取 learnku 精華文章Python爬蟲
- 計算機書籍- 網路爬蟲開發實戰計算機爬蟲
- Python爬蟲入門【3】:美空網資料爬取Python爬蟲
- Python使用多程式提高網路爬蟲的爬取速度Python爬蟲
- python書籍推薦-Python爬蟲開發與專案實戰Python爬蟲
- Python爬蟲入門【10】:電子書多執行緒爬取Python爬蟲執行緒
- Python爬蟲入門教程 50-100 Python3爬蟲爬取VIP視訊-Python爬蟲6操作Python爬蟲
- 教你用Python爬取圖蟲網Python