Python預備知識

Requests庫入門

Requests.requests()

requests.get()

requests.head()

requests.post()

requests.put()

requests.patch()

requests.delete()

作爲爬蟲工具,以上的get()和head()方法是最常用的

一定記住這個拋出異常代碼塊:

try:
    r=requests.get(url,timeout=
30)
    r.raise_for_status()
    r.encoding=r.apparent_encoding
   
return r.text
except:
   
return "產生異常"

 

  1. 網絡爬蟲的尺寸:
  1. 爬取網頁,玩轉網頁(小規模,數據量小,爬取速度不敏感)。使用工具:Requests 庫 (佔比:>90%)
  2. 爬取網站,爬起系列網站(中規模,數據規模較大,對速度敏感)。使用工具:Scrapy庫。
  3. 爬取全網(全Internet)(大規模,搜索引擎爬取速度關鍵)。使用工具:是定製開發的工具。
  1. 限制網絡爬蟲
  1. 來源審查:判斷User-Agent進行限制;檢查來訪HTTP協議頭的User-Agent域,只響應瀏覽器或友好爬蟲的訪問。
  2. 發佈公告:(通過robots協議發佈公告
  1. robots協議的使用

user_agent

Disallow

網絡爬蟲盜亦有道:理論上,都應該遵守robots協議,不然會存在法律風險

但是當我們編寫爬蟲的訪問模式與人爲訪問網頁類似,並不作爲商業用途時,原則上不用遵守robots協議。

  1. 爬取網頁的代碼
  2. import requests url = 'http://growthbox.net/growthhack/category/growthday/' try:     r = requests.get(url)     r.raise_for_status()     r.encoding = r.apparent_encoding     print(r.text[:1000]) except:     print('爬取失敗')

 
  1. 向百度或360提供關鍵詞並返回搜索結果

 

  1. 爬取網絡圖片,存儲圖片(用到文件的讀寫操作)

 

  1. Ip地址歸屬地的查詢(判斷該ip地址來源於北京,上海,還是美國呢)

 

二、解析HTML頁面信息標記與提取方法

使用Beautiful  soup

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章