国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Python:抓取百度SERP搜索結果頁的網站標題信息

2019-11-14 17:45:32
字體:
來源:轉載
供稿:網友

比如,你想采集標題中包含“58同城”的SERP結果,并過濾包含有“北京”或“廈門”等結果數據。

該Python腳本主要是實現以上功能。

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

代碼如下:

__author__ = '曾是土木人'# -*- coding: utf-8 -*-#采集SERP搜索結果標題import urllib2from bs4 import BeautifulSoupimport 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)#設置超時時間        data    = response.read().decode('utf-8','ignore')    except:pass    return data#提取搜索結果SERP的標題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: passkeyWord = "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('程序運行耗時:%0.2f 秒'%(c))

原文地址:曾是土木人

新浪微博:http://weibo.com/cstmr

轉載請注明出處:http://m.survivalescaperooms.com/hongfei/p/3764181.html


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新竹市| 南召县| 嘉兴市| 泸州市| 环江| 津市市| 黔江区| 鄂伦春自治旗| 长宁区| 安仁县| 濉溪县| 扬中市| 渝中区| 环江| 枣阳市| 东乌珠穆沁旗| 介休市| 清河县| 封开县| 新昌县| 大姚县| 横峰县| 洪雅县| 洛隆县| 景德镇市| 通城县| 泸州市| 金湖县| 杭州市| 肇源县| 荥阳市| 炎陵县| 吕梁市| 达州市| 陈巴尔虎旗| 天津市| 朝阳县| 荆州市| 兴仁县| 申扎县| 乌拉特中旗|