Python爬蟲爬取百度搜索內(nèi)容代碼實(shí)例
這篇文章主要介紹了Python爬蟲爬取百度搜索內(nèi)容代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
搜索引擎用的很頻繁,現(xiàn)在利用Python爬蟲提取百度搜索內(nèi)容,同時(shí)再進(jìn)一步提取內(nèi)容分析就可以簡便搜索過程。詳細(xì)案例如下:
代碼如下
# 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="(.*?)" rel="external nofollow" rel="external nofollow" .*?>(.*?)</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')</a.*?href="(.*?)" rel="external nofollow" rel="external nofollow" .*?></h3.*?class="t".*?></table.*?class="result".*?>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python爬蟲爬取指定內(nèi)容的解決方法
- python爬蟲爬取bilibili網(wǎng)頁基本內(nèi)容
- python爬蟲開發(fā)之使用python爬蟲庫requests,urllib與今日頭條搜索功能爬取搜索內(nèi)容實(shí)例
- python爬取內(nèi)容存入Excel實(shí)例
- Python爬蟲爬取新浪微博內(nèi)容示例【基于代理IP】
- python爬取網(wǎng)頁內(nèi)容轉(zhuǎn)換為PDF文件
- Python下使用Scrapy爬取網(wǎng)頁內(nèi)容的實(shí)例
- 基于Python實(shí)現(xiàn)web網(wǎng)頁內(nèi)容爬取的方法
相關(guān)文章
詳解利用Pytorch實(shí)現(xiàn)ResNet網(wǎng)絡(luò)之評估訓(xùn)練模型
這篇文章主要為大家介紹了利用Pytorch實(shí)現(xiàn)ResNet網(wǎng)絡(luò)之評估訓(xùn)練模型詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Pandas中Series和DataFrame的索引實(shí)現(xiàn)
這篇文章主要介紹了Pandas中Series和DataFrame的索引實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
教你用一行Python代碼實(shí)現(xiàn)GUI圖形界面
這篇文章主要介紹了教你用一行Python代碼實(shí)現(xiàn)GUI圖形界面,通過使用PySimpleGUI的popup_get_folder()方法,一行代碼就能實(shí)現(xiàn)選擇文件夾的操作,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
opencv中cv2.minAreaRect函數(shù)輸出角度問題詳解
minAreaRect返回的數(shù)據(jù)包括了矩形的中心點(diǎn),寬、高,和旋轉(zhuǎn)角度,下面這篇文章主要給大家介紹了關(guān)于opencv中cv2.minAreaRect函數(shù)輸出角度問題的相關(guān)資料,需要的朋友可以參考下2022-11-11
基于python介紹pytorch保存和恢復(fù)參數(shù)
這篇文章主要介紹了基于python介紹pytorch保存和恢復(fù)參數(shù),為了恢復(fù)模型,我們需要用代碼生成框架,然后從磁盤加載參數(shù),下面具體的相關(guān)介紹,需要的小伙伴可以參考一下2022-03-03
Python使用get_text()方法從大段html中提取文本的實(shí)例
今天小編就為大家分享一篇Python使用get_text()方法從大段html中提取文本的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python生產(chǎn)者與消費(fèi)者模型中的優(yōu)勢介紹
這篇文章主要介紹了python多進(jìn)程中的生產(chǎn)者和消費(fèi)者模型優(yōu)勢,生產(chǎn)者是指生產(chǎn)數(shù)據(jù)的任務(wù),消費(fèi)者是指消費(fèi)數(shù)據(jù)的任務(wù)。當(dāng)生產(chǎn)者的生產(chǎn)能力遠(yuǎn)大于消費(fèi)者的消費(fèi)能力,生產(chǎn)者就需要等消費(fèi)者消費(fèi)完才能繼續(xù)生產(chǎn)新的數(shù)據(jù)2023-03-03

