百度系接口分析

本文介绍了两个基于Python的搜索接口实现,一个是针对开发者搜索的百度接口,用于获取技术文档;另一个是文心百中的搜索接口,用于知识搜索。示例代码展示了如何使用这些接口查询‘python音频音色矫正’的相关信息,包括ID、URL和标题等关键数据。
摘要由CSDN通过智能技术生成
# !/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@author  : v_jiaohaicheng@baidu.com
@des     : 开发者搜索接口

"""
import requests




def get_result(search_info):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36",
    }

    url = "https://kaifa.baidu.com/rest/v1/search"

    params = {
        "wd": search_info,
        "pageNum": "1",
    }
    response = requests.get(url, headers=headers, params=params)
    if response.json()["status"] == "OK":
        result_list = response.json()["data"]["documents"]["data"]
        for id,args in enumerate(result_list):
            url = args["techDocDigest"]["url"]
            title = args["techDocDigest"]["realTitle"]
            # print(id,url,title)
            yield (id,url,title)

search_info = "python 音频音色矫正"
for id,url,title in get_result(search_info):
    print(id,url,title)

# !/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@author  : v_jiaohaicheng@baidu.com
@des     : 文心百中搜索接口

"""
import requests



def get_data(search_info):
    headers = {
        "Referer": "https://wenxin.baidu.com/baizhong/knowledgesearch/",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36",
    }
    cookies = {
           "BDUSS_BFESS": "I3ZVJPSjRWSGNQRG5zUldoSmRVSVB3M3NTamg1bEtBTkR0bUI4QTM1Q2dsUTFrRVFBQUFBJCQAAAAAAAAAAAEAAAAQdrtY0fS54rXE37nfud-537kAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKAI5mOgCOZjY1",

    }
    url = "https://wenxin.baidu.com/baizhong/demo-api/demo-collection/search"
    params = {
        "q": search_info,
    }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)
    data = response.json()
    if data["errCode"] == 0:
        hits_list = data["baizhong"]["hits"]

        for args in hits_list:
            id = args["_id"]
            title = args["_source"]["title"]
            para = args["_source"]["para"]
            doc = args["_source"]["doc"]
            # print(id,title,para,doc)
            yield "百中", id,title,para,doc

        keyword_engine_list = data["keyword_engine"]["hits"]
        for args in keyword_engine_list:
            id = args["_id"]
            title = args["_source"]["title"]
            para = args["_source"]["para"]
            doc = args["_source"]["doc"]

            yield "常规",id,title,para,doc


if __name__ == '__main__':
    search_info = "二杆子"
    for kind,id,title,para,doc in get_data(search_info):
        print(kind,id,title,para,doc)```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值