Skip to main content

Baidux - Python SDK for index and search

Install

pip install baidux

Usage

打开百度首页,登录后,找到 www.baidu.com 此条 GET 请求,并复制此条请求 request headers 里的 cookies

使用样例如下:

from baidux.utils import test_cookies
from baidux import config
from baidux import BaiduIndex, ExtendedBaiduIndex

cookies = """这里放cookie"""

# 测试cookies是否配置正确
# True为配置成功,False为配置不成功
print(test_cookies(cookies))

keywords = [['英雄联盟'], ['冠军杯', '英雄联盟'], ['抑郁', '自杀', '明星']]

# 获取城市代码, 将代码传入area可以获取不同城市的指数, 不传则为全国
# 媒体指数不能分地区获取
print(config.PROVINCE_CODE)
print(config.CITY_CODE)

# 获取百度搜索指数(地区为山东)
baidu_index = BaiduIndex(
    keywords=keywords,
    start_date='2018-01-01',
    end_date='2019-01-01',
    cookies=cookies,
    area=901
)
for index in baidu_index.get_index():
    print(index)

# 获取百度媒体指数
news_index = ExtendedBaiduIndex(
    keywords=keywords,
    start_date='2018-01-01',
    end_date='2019-01-01',
    cookies=cookies,
    kind='news'
)
for index in news_index.get_index():
    print(index)

# 获取百度咨询指数
feed_index = ExtendedBaiduIndex(
    keywords=keywords,
    start_date='2018-01-01',
    end_date='2019-01-01',
    cookies=cookies,
    kind='feed'
)
for index in feed_index.get_index():
    print(index)

Result

百度搜索指数: {'keyword': ['抑郁', '自杀', '明星'], 'type': 'wise', 'date': '2018-06-10', 'index': '1835'}
百度媒体指数: {'keyword': ['抑郁', '自杀', '明星'], 'date': '2018-12-29', 'index': '0'}
百度咨询指数: {'keyword': ['抑郁', '自杀', '明星'], 'date': '2018-12-29', 'index': '1102911'}

Tips

  • 不限制传入关键词的数量
  • 搜索指数最早的数据日期为2011-01-01
  • 开始时间超过最早的数据日期会导致数据不准确
  • 初始化类时传入area可以查询指定区域的百度指数, 默认为全国
  • 有些代码不是特别严谨, 有需要请自己DIY
  • 媒体指数不支持细分地域查询
  • 当查询百度指数未收录的关键词时也会出现报错,这个之后会修复

Changelog

2018/02/10 更新格式化数据的方法format_data
2018/12/29 更新查询指定区域百度指数的功能
2018/11/07 更新
2019/05/31 更新
2020/02/14 添加咨询指数和媒体指数的功能
2020/04/16 重构项目结构
2020/05/08 百度指数修改传递参数
2020/07/13 添加组合词查询

Release files for baidux 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for baidux 0.1.2
File Size Uploaded
baidux-0.1.2.tar.gz 12.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for baidux 0.1.2
File Interpreter ABI Platform
baidux-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 31.8 kB

Release files / baidux-0.1.2.tar.gz

Download URL baidux-0.1.2.tar.gz
Size 12.5 kB
Tags Source
SHA-256 checksum
How to use checksums
78be5b111f344566464588405833041bc84eb0e9d75a53d168a6bbb6bc05bdf5
BLAKE2b-256 checksum
How to use checksums
062cb936e90c00e47602d6915bd39e289e44b1638b52f7ea1ef2a43f2af8f563
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

Release files / baidux-0.1.2-py3-none-any.whl

Download URL baidux-0.1.2-py3-none-any.whl
Size 19.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7c140d55190edad3f3683c4e12aa484166ec714f63e69bb2e0d2db807ba22525
BLAKE2b-256 checksum
How to use checksums
f7e5e5248d259b7b33c9997f90a9bf847fae42c272cbebfa3b2e00fc97bfe34b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.0

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page