首页 后端开发 Python教程 python实现百度关键词排名查询

python实现百度关键词排名查询

Jun 16, 2016 am 08:44 AM
python

就是一个简单的python查询百度关键词排名的函数,以下是一些简介:
1、UA随机
2、操作简单方便,直接getRank(关键词,域名)就可以了
3、编码转化。编码方面应该没啥问题了。
4、结果丰富。不仅有排名,还有搜索结果的title,URL,快照时间,符合SEO需求
5、拿来做个软件或者自己用都很方便。

功能是单线程实现,速度慢,大家可以参考修改成自己需要的。

复制代码 代码如下:

#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 and
    pat = re.compile(r'(.*?)')
    match = pat.findall(atext.replace('\n', ''))
    pureText = match[0].replace('', '').replace('', '')
    return pureText.replace('\n', '')

def getCacheDate(t):    #get the date of cache
    pat = re.compile(r'.*?(\d{4}-\d{1,2}-\d{1,2}) ')
    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('', '').replace('', '')): #改正则
            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)

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

怎么下载deepseek 小米 怎么下载deepseek 小米 Feb 19, 2025 pm 05:27 PM

怎么下载deepseek 小米

模板化的优点和缺点有哪些? 模板化的优点和缺点有哪些? May 08, 2024 pm 03:51 PM

模板化的优点和缺点有哪些?

Google AI 为开发者发布 Gemini 1.5 Pro 和 Gemma 2 Google AI 为开发者发布 Gemini 1.5 Pro 和 Gemma 2 Jul 01, 2024 am 07:22 AM

Google AI 为开发者发布 Gemini 1.5 Pro 和 Gemma 2

仅用250美元,Hugging Face技术主管手把手教你微调Llama 3 仅用250美元,Hugging Face技术主管手把手教你微调Llama 3 May 06, 2024 pm 03:52 PM

仅用250美元,Hugging Face技术主管手把手教你微调Llama 3

golang 函数调试与分析的完整指南 golang 函数调试与分析的完整指南 May 06, 2024 pm 02:00 PM

golang 函数调试与分析的完整指南

分享几个.NET开源的AI和LLM相关项目框架 分享几个.NET开源的AI和LLM相关项目框架 May 06, 2024 pm 04:43 PM

分享几个.NET开源的AI和LLM相关项目框架

deepseek怎么问他 deepseek怎么问他 Feb 19, 2025 pm 04:42 PM

deepseek怎么问他

NET40是什么软件 NET40是什么软件 May 10, 2024 am 01:12 AM

NET40是什么软件

See all articles