Press ESC to close

百度下拉词收集

运行效果:

代码:

 

# -*- coding: utf-8 -*-
#百度移动端下拉词搜索
#@author 微信公众号:Python与SEO
#20220524 by微信:huguo00289
import requests
import json
import logging

logging.basicConfig(filename='xlclog.txt', level=logging.DEBUG,
                    format='%(asctime)s - %(levelname)s - %(message)s')

#获取百度PC端下拉关键词
def bdxl(word):
    requests.packages.urllib3.disable_warnings()
    url = 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=%s&sugmode=2&json=1&p=3&sid=1427_21091_21673_22581&req=2&pbs=%%E5%%BF%%AB%%E6%%89%%8B&csor=2&pwd=%%E5%%BF%%AB%%E6%%89%%8B&cb=jQuery11020924966752020363_1498055470768&_=1498055470781' % word
    r = requests.get(url, verify=False)  # 请求API接口,取消了HTTPS验证
    cont = r.content  # 获取返回的内容
    res = cont[41: -2].decode('gbk')  # 只取返回结果中json格式一段,并且解码为unicode
    res_json = json.loads(res)  # json格式转换
    keys=res_json['s']
    print(f'>> 已获取百度搜索下拉词:{keys}')
    logging.info(f'>> 已获取百度搜索下拉词:{keys}')
    return keys  # 返回下拉词

#获取百度移动端下拉关键词
def mbdxl(word):
    url=f"https://m.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=wise&from=wise_web&net=1&os=1&sp=300&rm_brand=0&callback=jsonp2&wd={word}"
    r = requests.get(url=url)
    cont = r.content.decode('utf-8')  # 获取返回的内容
    # print(cont)
    res = cont[7: -1]
    res_json = json.loads(res)  # json格式转换
    keys=[]
    qs=res_json['g']
    for q in qs:
        keys.append(q['q'])

    print(f'>> 已获取百度移动搜索下拉词:{keys}')
    logging.info(f'>> 百度移动搜索下拉词:{keys}')


#获取百度移动端下拉关键词(添加了ua协议头)
def mmbdxl(word):
    headers={"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1",}
    url=f"https://m.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=wise&from=wise_web&net=1&os=1&sp=300&rm_brand=0&callback=jsonp2&wd={word}"
    r = requests.get(url=url,headers=headers)
    cont = r.content.decode('utf-8')  # 获取返回的内容
    # print(cont)
    res = cont[7: -1]
    res_json = json.loads(res)  # json格式转换
    keys=[]
    qs=res_json['g']
    for q in qs:
        keys.append(q['q'])

    print(f'>> 已获取百度移动搜索下拉词:{keys}')
    logging.info(f'>> 百度移动搜索下拉词:{keys}')


if __name__=='__main__':
    word="植物提取物"
    bdxl(word)
    mbdxl(word)
    mmbdxl(word)

发表回复