天下的網站沒有我爬不到的,只有不想爬的(有吹牛逼之嫌)。
Python2慢慢被Python3所代替了,主要以3爲主,話不多說,直接看技術點吧
- 正則表達式re(難)
- 獲取<tr></tr>標籤之間內容
- 獲取<a href..></a>超鏈接之間內容
- 獲取URL最後一個參數命名圖片或傳遞參數
- 爬取網頁中所有URL鏈接
- 爬取網頁標題title兩種方法
- 定位table位置並爬取屬性-屬性值
- 過濾<span></span>等標籤
- 獲取<script></script>等標籤內容
- 通過replace函數過濾<br />標籤
- 獲取<img ../>中超鏈接及過濾<img>標籤
- 代碼:
-
import re content = <td> <a href="https://www.baidu.com/articles/zj.html" title="山西省">山西煤多</a> <a href="https://www.baidu.com//articles/gz.html" title="北京市">北京人多</a> </td> # 獲取<a href></a>之間的內容 print(u'獲取鏈接文本內容:') res = r'<a .*?>(.*?)</a>' mm = re.findall( res, content, re.S | re.M) for value in mm: print(value) # 獲取所有<a href></a>鏈接所有內容 print(u'\n