最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)抓取百度搜索結(jié)果頁的網(wǎng)站標(biāo)題信息

 更新時間:2015年01月22日 13:13:28   投稿:junjie  
這篇文章主要介紹了Python實(shí)現(xiàn)抓取百度搜索結(jié)果頁的網(wǎng)站標(biāo)題信息,本文使用BeautifulSoup來解析HTML,需要的朋友可以參考下

比如,你想采集標(biāo)題中包含“58同城”的SERP結(jié)果,并過濾包含有“北京”或“廈門”等結(jié)果數(shù)據(jù)。

該P(yáng)ython腳本主要是實(shí)現(xiàn)以上功能。

其中,使用BeautifulSoup來解析HTML,可以參考我的另外一篇文章:Windows8下安裝BeautifulSoup

代碼如下:

復(fù)制代碼 代碼如下:

__author__ = '曾是土木人'
# -*- coding: utf-8 -*-
#采集SERP搜索結(jié)果標(biāo)題
import urllib2
from bs4 import BeautifulSoup
import time
#寫文件
def WriteFile(fileName,content):
    try:
        fp = file(fileName,"a+")
        fp.write(content + "\r")
        fp.close()
    except:
        pass

#獲取Html源碼
def GetHtml(url):
    try:
        req = urllib2.Request(url)
        response= urllib2.urlopen(req,None,3)#設(shè)置超時時間
        data    = response.read().decode('utf-8','ignore')
    except:pass
    return data

#提取搜索結(jié)果SERP的標(biāo)題
def FetchTitle(html):
    try:
        soup = BeautifulSoup(''.join(html))
        for i in soup.findAll("h3"):
            title = i.text.encode("utf-8")      
       if any(str_ in title for str_ in ("北京","廈門")):
          continue
            else:
                print title
            WriteFile("Result.txt",title)
    except:
        pass

keyword = "58同城"
if __name__ == "__main__":
    global keyword
    start = time.time()
    for i in range(0,8):
        url = "http://www.baidu.com/s?wd=intitle:"+keyword+"&rn=100&pn="+str(i*100)
        html = GetHtml(url)
        FetchTitle(html)
        time.sleep(1)
    c = time.time() - start
    print('程序運(yùn)行耗時:%0.2f 秒'%(c))

相關(guān)文章

最新評論

彰化市| 弥渡县| 石屏县| 阳东县| 东光县| 锡林浩特市| 南乐县| 古田县| 鹤岗市| 清苑县| 贺兰县| 丹巴县| 太和县| 洱源县| 平顺县| 南岸区| 营山县| 肥城市| 区。| 高要市| 平泉县| 分宜县| 河北区| 昌宁县| 二手房| 萍乡市| 南江县| 兴仁县| 延长县| 安远县| 涟水县| 华安县| 光泽县| 来安县| 卢湾区| 淳安县| 阳曲县| 闵行区| 恭城| 荥经县| 邵东县|