python爬蟲

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

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