国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現百度關鍵詞排名查詢

2020-02-23 05:16:21
字體:
來源:轉載
供稿:網友

就是一個簡單的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 <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>')

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 施秉县| 岱山县| 响水县| 郯城县| 平利县| 长岭县| 广平县| 华坪县| 长汀县| 剑川县| 潍坊市| 菏泽市| 河池市| 宁强县| 灵寿县| 成安县| 正安县| 鸡西市| 彰化市| 汉阴县| 嘉禾县| 遂平县| 东丰县| 石渠县| 台东县| 兴化市| 民勤县| 临澧县| 游戏| 龙泉市| 龙海市| 斗六市| 白银市| 曲麻莱县| 永年县| 芒康县| 长宁区| 宁海县| 东平县| 龙海市| 肇州县|