python2.7爬蟲
有幾點需要注意的地方:
1、正則表示的方法match、search、findall,使用方法都不一樣
- match:只匹配整個字符串第一個字母,如果第一個字母沒有匹配到則返回none
- search:查找整個文章但是隻返回最後一個結果
- findall:查詢整個文章返回全部結果
2、中文查找
- 下載的頁面需使用unicode轉碼後方可進行查找
- 錄入的中文符號需要在字符串前加u表明是unicode纔可以查找使用
#!/usr/bin/env python # -*- coding: utf-8 -*- import urllib2 import urllib import re if __name__ == '__main__': resp=urllib2.urlopen("http://www.weizhang8.cn/Article/9.html") content=resp.read() content=unicode(content,'utf-8') # print content m=re.findall(u'[\((](.)[\))]',content) if m : for i in m : print i