資源簡介
Python腳本用于獲取百度搜索結(jié)果中的標(biāo)題和URL。
輸入:百度關(guān)鍵詞及搜索頁面數(shù)
輸出:標(biāo)題和URL
代碼片段和文件信息
#encoding:?utf-8
#采集SERP搜索結(jié)果標(biāo)題
import?urllib2
from?bs4?import?BeautifulSoup
#獲取title和url
def?GettitleUrl(urlres_titleres_urlbShowRes):
??try:
????##獲取Html源碼
????req?=?urllib2.Request(url)
????response=?urllib2.urlopen(reqNone3)#設(shè)置超時時間
????data?=?response.read()#.decode(‘utf-8‘‘ignore‘)
????##提取搜索結(jié)果SERP的標(biāo)題和鏈接
????soup?=?BeautifulSoup(‘‘.join(data)‘lxml‘)
????for?i?in?soup.findAll(“h3“):
??????temp_title?=?i.a.text.encode(“utf-8“)
??????temp_url?=?i.a.get(‘href‘)
??????if?bShowRes==1:
????????print?temp_title
????????print?temp_url
??????res_title.append(temp_title)
??????res_url.append(temp_url)
??except:
??????pass
評論
共有 條評論