Python抓取百度搜索結果


Python 用來做一些簡單的工作還是不錯的,一個練手的代碼,抓取百度關鍵字搜索的結果

 

#  coding=utf-8
import urllib2 as url
import string
import urllib
import re

def baidu_search(keyword):
    p= { ' wd ': keyword}
    res=url.urlopen( " http://www.baidu.com/s? "+urllib.urlencode(p))
    html=res.read()
     return html
def getList(regex,text):
    arr = []
    res = re.findall(regex, text)
     if res:
         for r  in res:
            arr.append(r)
     return arr
def getMatch(regex,text):
    res = re.findall(regex, text)
     if res:
         return res[0]
     return  ""
def clearTag(text):
    p = re.compile(u ' <[^>]+> ')
    retval = p.sub( "",text)
     return retval

html = baidu_search( ' 天下無賊 ')
content = unicode(html,  ' utf-8 ', ' ignore ')

arrList = getList(u " <table.*?class=\"result\".*?>.*?<\/a> ", content)
for item  in arrList:
    regex = u " <h3.*?class=\"t\".*?><a.*?href=\"(.*?)\".*?>(.*?)<\/a> "
    link = getMatch(regex,item)
    url = link[0]
    title = clearTag(link[1]).encode( ' utf8 ')
     print url
     print title

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM