python實(shí)現(xiàn)百度關(guān)鍵詞排名查詢
就是一個(gè)簡(jiǎn)單的python查詢百度關(guān)鍵詞排名的函數(shù),以下是一些簡(jiǎn)介:
1、UA隨機(jī)
2、操作簡(jiǎn)單方便,直接getRank(關(guān)鍵詞,域名)就可以了
3、編碼轉(zhuǎn)化。編碼方面應(yīng)該沒啥問題了。
4、結(jié)果豐富。不僅有排名,還有搜索結(jié)果的title,URL,快照時(shí)間,符合SEO需求
5、拿來做個(gè)軟件或者自己用都很方便。
功能是單線程實(shí)現(xiàn),速度慢,大家可以參考修改成自己需要的。
#coding=utf-8
import requests
import BeautifulSoup
import re
import random
def decodeAnyWord(w):
try:
w.decode('utf-8')
except:
w = w.decode('gb2312')
else:
w = w.decode('utf-8')
return w
def createURL(checkWord): #create baidu URL with search words
checkWord = checkWord.strip()
checkWord = checkWord.replace(' ', '+').replace('\n', '')
baiduURL = 'http://www.baidu.com/s?wd=%s&rn=100' % checkWord
return baiduURL
def getContent(baiduURL): #get the content of the serp
uaList = ['Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1;+.NET+CLR+1.1.4322;+TencentTraveler)',
'Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1;+.NET+CLR+2.0.50727;+.NET+CLR+3.0.4506.2152;+.NET+CLR+3.5.30729)',
'Mozilla/5.0+(Windows+NT+5.1)+AppleWebKit/537.1+(KHTML,+like+Gecko)+Chrome/21.0.1180.89+Safari/537.1',
'Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1)',
'Mozilla/5.0+(Windows+NT+6.1;+rv:11.0)+Gecko/20100101+Firefox/11.0',
'Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+5.1;+Trident/4.0;+SV1)',
'Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+5.1;+Trident/4.0;+GTB7.1;+.NET+CLR+2.0.50727)',
'Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+5.1;+Trident/4.0;+KB974489)']
headers = {'User-Agent': random.choice(uaList)}
r = requests.get(baiduURL, headers = headers)
return r.content
def getLastURL(rawurl): #get final URL while there're redirects
r = requests.get(rawurl)
return r.url
def getAtext(atext): #get the text with <a> and </a>
pat = re.compile(r'<a .*?>(.*?)</a>')
match = pat.findall(atext.replace('\n', ''))
pureText = match[0].replace('<em>', '').replace('</em>', '')
return pureText.replace('\n', '')
def getCacheDate(t): #get the date of cache
pat = re.compile(r'<span class="g">.*?(\d{4}-\d{1,2}-\d{1,2}) </span>')
match = pat.findall(t)
cacheDate = match[0]
return cacheDate
def getRank(checkWord, domain): #main line
checkWord = checkWord.replace('\n', '')
checkWord = decodeAnyWord(checkWord)
baiduURL = createURL(checkWord)
cont = getContent(baiduURL)
soup = BeautifulSoup.BeautifulSoup(cont)
results = soup.findAll('table', {'class': 'result'}) #find all results in this page
for result in results:
checkData = unicode(result.find('span', {'class': 'g'}))
if re.compile(r'^[^/]*%s.*?' %domain).match(checkData.replace('<b>', '').replace('</b>', '')): #改正則
nowRank = result['id'] #get the rank if match the domain info
resLink = result.find('h3').a
resURL = resLink['href']
domainURL = getLastURL(resURL) #get the target URL
resTitle = getAtext(unicode(resLink)) #get the title of the target page
rescache = result.find('span', {'class': 'g'})
cacheDate = getCacheDate(unicode(rescache)) #get the cache date of the target page
res = u'%s, 第%s名, %s, %s, %s' % (checkWord, nowRank, resTitle, cacheDate, domainURL)
return res.encode('gb2312')
break
else:
return '>100'
domain = 'www.baidu.com' #set the domain which you want to search.
print getRank('百度', domain)
相關(guān)文章
一文教會(huì)你用Python獲取網(wǎng)頁(yè)指定內(nèi)容
Python用做數(shù)據(jù)處理還是相當(dāng)不錯(cuò)的,如果你想要做爬蟲,Python是很好的選擇,它有很多已經(jīng)寫好的類包,只要調(diào)用即可完成很多復(fù)雜的功能,下面這篇文章主要給大家介紹了關(guān)于Python獲取網(wǎng)頁(yè)指定內(nèi)容的相關(guān)資料,需要的朋友可以參考下2022-03-03
python機(jī)器學(xué)習(xí)XGBoost梯度提升決策樹的高效且可擴(kuò)展實(shí)現(xiàn)
這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)XGBoost梯度提升決策樹的高效且可擴(kuò)展實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
python的virtualenv虛擬環(huán)境常見問題和命令
在Python中,venv是一個(gè)用于創(chuàng)建和管理虛擬環(huán)境的模塊,虛擬環(huán)境可以幫助你在項(xiàng)目之間隔離不同的Python包和依賴關(guān)系,這篇文章主要介紹了python的virtualenv虛擬環(huán)境常見問題和命令,需要的朋友可以參考下2024-07-07
Python中Async語法協(xié)程的實(shí)現(xiàn)
這篇文章主要介紹了Python中Async語法協(xié)程的實(shí)現(xiàn),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
Python數(shù)據(jù)可視化實(shí)踐之使用Matplotlib繪制圖表
數(shù)據(jù)可視化是數(shù)據(jù)分析的重要環(huán)節(jié),通過將數(shù)據(jù)轉(zhuǎn)化為圖形,可以更直觀地展示數(shù)據(jù)特征和規(guī)律。Python中的Matplotlib庫(kù)是一個(gè)強(qiáng)大的數(shù)據(jù)可視化工具,本文將帶您了解Matplotlib的基本使用方法,以及如何繪制常見的圖表2023-05-05
如何解決pycharm調(diào)試報(bào)錯(cuò)的問題
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于如何解決pycharm調(diào)試報(bào)錯(cuò)的問題文章,需要的朋友們可以學(xué)習(xí)參考下。2020-08-08

