欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實現(xiàn)百度關(guān)鍵詞排名查詢

 更新時間:2014年03月30日 10:29:08   作者:  
這篇文章主要介紹了python實現(xiàn)百度關(guān)鍵詞排名查詢,需要的朋友可以參考下

就是一個簡單的python查詢百度關(guān)鍵詞排名的函數(shù),以下是一些簡介:
1、UA隨機
2、操作簡單方便,直接getRank(關(guān)鍵詞,域名)就可以了
3、編碼轉(zhuǎn)化。編碼方面應(yīng)該沒啥問題了。
4、結(jié)果豐富。不僅有排名,還有搜索結(jié)果的title,URL,快照時間,符合SEO需求
5、拿來做個軟件或者自己用都很方便。

功能是單線程實現(xiàn),速度慢,大家可以參考修改成自己需要的。

復(fù)制代碼 代碼如下:

#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})&nbsp;</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)文章

  • python之如何實現(xiàn)延遲操作

    python之如何實現(xiàn)延遲操作

    這篇文章主要介紹了python之如何實現(xiàn)延遲操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • numpy矩陣數(shù)值太多不能全部顯示的解決

    numpy矩陣數(shù)值太多不能全部顯示的解決

    這篇文章主要介紹了numpy矩陣數(shù)值太多不能全部顯示的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • numpy.reshape(-1,1)的具體使用

    numpy.reshape(-1,1)的具體使用

    本文主要介紹了numpy.reshape(-1,1)的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 在Python的Django框架中獲取單個對象數(shù)據(jù)的簡單方法

    在Python的Django框架中獲取單個對象數(shù)據(jù)的簡單方法

    這篇文章主要介紹了在Python的Django框架中獲取單個對象數(shù)據(jù)的簡單方法,Django為數(shù)據(jù)的操作提供了諸多方便的功能,需要的朋友可以參考下
    2015-07-07
  • Python真題案例之二分法查找詳解

    Python真題案例之二分法查找詳解

    這篇文章主要介紹了python實操案例練習(xí),本文給大家分享的案例中主要講解了二分法查找,需要的小伙伴可以參考一下
    2022-03-03
  • Python實現(xiàn)Opencv cv2.Canny()邊緣檢測

    Python實現(xiàn)Opencv cv2.Canny()邊緣檢測

    這篇博客將介紹Canny邊緣檢測的概念,并利用cv2.Canny()實現(xiàn)邊緣檢測,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • python批量解壓zip文件的方法

    python批量解壓zip文件的方法

    這篇文章主要介紹了python批量解壓zip文件的方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • python中添加模塊導(dǎo)入路徑的方法

    python中添加模塊導(dǎo)入路徑的方法

    這篇文章主要介紹了python中添加模塊導(dǎo)入路徑的方法 ,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • Selenium執(zhí)行完畢未關(guān)閉chromedriver/geckodriver進程的解決辦法(java版+python版)

    Selenium執(zhí)行完畢未關(guān)閉chromedriver/geckodriver進程的解決辦法(java版+python版

    這篇文章主要介紹了Selenium執(zhí)行完畢未關(guān)閉chromedriver/geckodriver進程的解決辦法(java版+python版),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • python和go語言的區(qū)別是什么

    python和go語言的區(qū)別是什么

    在本篇文章中小編給大家整理的是一篇關(guān)于go語言和python的區(qū)別點,需要的朋友們可以學(xué)習(xí)下。
    2020-07-07

最新評論