python實(shí)現(xiàn)提取百度搜索結(jié)果的方法
本文實(shí)例講述了python實(shí)現(xiàn)提取百度搜索結(jié)果的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
# coding=utf8
import urllib2
import string
import urllib
import re
import random
#設(shè)置多個(gè)user_agents,防止百度限制IP
user_agents = ['Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20130406 Firefox/23.0', \
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0', \
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533+ \
(KHTML, like Gecko) Element Browser 5.0', \
'IBM WebExplorer /v0.94', 'Galaxy/1.0 [en] (Mac OS X 10.5.6; U; en)', \
'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)', \
'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14', \
'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) \
Version/6.0 Mobile/10A5355d Safari/8536.25', \
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) \
Chrome/28.0.1468.0 Safari/537.36', \
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0; TheWorld)']
def baidu_search(keyword,pn):
p= {'wd': keyword}
res=urllib2.urlopen(("http://www.baidu.com/s?"+urllib.urlencode(p)+"&pn={0}&cl=3&rn=100").format(pn))
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
def geturl(keyword):
for page in range(10):
pn=page*100+1
html = baidu_search(keyword,pn)
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]
#獲取標(biāo)題
#title = clearTag(link[1]).encode('utf8')
try:
domain=urllib2.Request(url)
r=random.randint(0,11)
domain.add_header('User-agent', user_agents[r])
domain.add_header('connection','keep-alive')
response=urllib2.urlopen(domain)
uri=response.geturl()
print uri
except:
continue
if __name__=='__main__':
geturl('python')
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
- Python使用Selenium自動(dòng)進(jìn)行百度搜索的實(shí)現(xiàn)
- Python通過(guò)tkinter實(shí)現(xiàn)百度搜索的示例代碼
- Python爬蟲(chóng)爬取百度搜索內(nèi)容代碼實(shí)例
- python采集百度搜索結(jié)果帶有特定URL的鏈接代碼實(shí)例
- python+selenium實(shí)現(xiàn)自動(dòng)化百度搜索關(guān)鍵詞
- python實(shí)現(xiàn)百萬(wàn)答題自動(dòng)百度搜索答案
- Python實(shí)現(xiàn)抓取百度搜索結(jié)果頁(yè)的網(wǎng)站標(biāo)題信息
- Python10行代碼實(shí)現(xiàn)模擬百度搜索的示例
相關(guān)文章
Python?Pandas多種添加行列數(shù)據(jù)方法總結(jié)
在進(jìn)行數(shù)據(jù)分析時(shí)經(jīng)常需要按照一定條件創(chuàng)建新的數(shù)據(jù)列,然后進(jìn)行進(jìn)一步分析,下面這篇文章主要給大家介紹了關(guān)于Python?Pandas多種添加行列數(shù)據(jù)方法的相關(guān)資料,需要的朋友可以參考下2022-07-07
Python socket實(shí)現(xiàn)簡(jiǎn)單聊天室
這篇文章主要為大家詳細(xì)介紹了Python socket實(shí)現(xiàn)簡(jiǎn)單聊天室,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Pygame游戲開(kāi)發(fā)之太空射擊實(shí)戰(zhàn)圖像精靈下篇
相信大多數(shù)8090后都玩過(guò)太空射擊游戲,在過(guò)去游戲不多的年代太空射擊自然屬于經(jīng)典好玩的一款了,今天我們來(lái)自己動(dòng)手實(shí)現(xiàn)它,在編寫(xiě)學(xué)習(xí)中回顧過(guò)往展望未來(lái),下面開(kāi)始入門(mén)篇2022-08-08
Python制作運(yùn)行進(jìn)度條的實(shí)現(xiàn)效果(代碼運(yùn)行不無(wú)聊)
這篇文章主要介紹了Python制作運(yùn)行進(jìn)度條的實(shí)現(xiàn)效果(代碼運(yùn)行不無(wú)聊),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
python機(jī)器學(xué)習(xí)基礎(chǔ)特征工程算法詳解
這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)基礎(chǔ)特征工程的算法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-11-11
Jmeter通過(guò)OS進(jìn)程取樣器調(diào)用Python腳本實(shí)現(xiàn)參數(shù)互傳
這篇文章主要介紹了Jmeter通過(guò)OS進(jìn)程取樣器調(diào)用Python腳本實(shí)現(xiàn)參數(shù)互傳,描述在cmd中調(diào)用上面的Python腳本并傳入兩個(gè)參數(shù)展開(kāi)主題,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-03-03
python實(shí)現(xiàn)獲取序列中最小的幾個(gè)元素
這篇文章主要介紹了python實(shí)現(xiàn)獲取序列中最小的幾個(gè)元素,是非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09
Python 輸出時(shí)去掉列表元組外面的方括號(hào)與圓括號(hào)的方法
今天小編就為大家分享一篇Python 輸出時(shí)去掉列表元組外面的方括號(hào)與圓括號(hào)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12

