最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)化。編碼方面應該沒啥問題了。
4、結(jié)果豐富。不僅有排名,還有搜索結(jié)果的title,URL,快照時間,符合SEO需求
5、拿來做個軟件或者自己用都很方便。

功能是單線程實現(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})&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獲取網(wǎng)頁指定內(nèi)容

    一文教會你用Python獲取網(wǎng)頁指定內(nèi)容

    Python用做數(shù)據(jù)處理還是相當不錯的,如果你想要做爬蟲,Python是很好的選擇,它有很多已經(jīng)寫好的類包,只要調(diào)用即可完成很多復雜的功能,下面這篇文章主要給大家介紹了關(guān)于Python獲取網(wǎng)頁指定內(nèi)容的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • 使用django自帶的user做外鍵的方法

    使用django自帶的user做外鍵的方法

    這篇文章主要介紹了使用django自帶的user做外鍵的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • python機器學習XGBoost梯度提升決策樹的高效且可擴展實現(xiàn)

    python機器學習XGBoost梯度提升決策樹的高效且可擴展實現(xiàn)

    這篇文章主要為大家介紹了python機器學習XGBoost梯度提升決策樹的高效且可擴展實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • Python進行文件路徑處理的模塊詳解

    Python進行文件路徑處理的模塊詳解

    我相信很多人和小編一樣,從來沒有好好研究過Python的文件路徑,Python作為一個magical語言,肯定有其他更直觀的文件路徑處理方法,下面就跟隨小編一起了解一下吧
    2025-04-04
  • python的virtualenv虛擬環(huán)境常見問題和命令

    python的virtualenv虛擬環(huán)境常見問題和命令

    在Python中,venv是一個用于創(chuàng)建和管理虛擬環(huán)境的模塊,虛擬環(huán)境可以幫助你在項目之間隔離不同的Python包和依賴關(guān)系,這篇文章主要介紹了python的virtualenv虛擬環(huán)境常見問題和命令,需要的朋友可以參考下
    2024-07-07
  • Python中Async語法協(xié)程的實現(xiàn)

    Python中Async語法協(xié)程的實現(xiàn)

    這篇文章主要介紹了Python中Async語法協(xié)程的實現(xiàn),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-06-06
  • 詳解Python字符串切片

    詳解Python字符串切片

    這篇文章主要介紹了Python字符串切片,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05
  • Python數(shù)據(jù)可視化實踐之使用Matplotlib繪制圖表

    Python數(shù)據(jù)可視化實踐之使用Matplotlib繪制圖表

    數(shù)據(jù)可視化是數(shù)據(jù)分析的重要環(huán)節(jié),通過將數(shù)據(jù)轉(zhuǎn)化為圖形,可以更直觀地展示數(shù)據(jù)特征和規(guī)律。Python中的Matplotlib庫是一個強大的數(shù)據(jù)可視化工具,本文將帶您了解Matplotlib的基本使用方法,以及如何繪制常見的圖表
    2023-05-05
  • 如何解決pycharm調(diào)試報錯的問題

    如何解決pycharm調(diào)試報錯的問題

    在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于如何解決pycharm調(diào)試報錯的問題文章,需要的朋友們可以學習參考下。
    2020-08-08
  • python django生成遷移文件的實例

    python django生成遷移文件的實例

    今天小編就為大家分享一篇python django生成遷移文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08

最新評論

临汾市| 临颍县| 改则县| 会同县| 桂林市| 德格县| 田阳县| 枣阳市| 襄城县| 高淳县| 根河市| 峨山| 大荔县| 金塔县| 那坡县| 平顶山市| 洪江市| 修水县| 津南区| 阳江市| 梅河口市| 平阳县| 黔东| 阿坝县| 三门县| 石河子市| 镇巴县| 姜堰市| 富川| 万安县| 玉门市| 宝山区| 蓝山县| 曲阳县| 邓州市| 康马县| 巴林右旗| 东明县| 澄迈县| 博乐市| 长治县|