protobuf

文章介绍了如何使用Python库blackboxprotobuf对来自https://s.wanfangdata.com.cn的搜索服务API响应进行解码,包括设置环境变量、构建请求头、序列化数据并发送POST请求,最终解析返回的protobuf数据。
摘要由CSDN通过智能技术生成

打钩的cookie是服务器返回的,没打钩的cookie是页面生成的

import blackboxprotobuf
import requests
import os
import datetime

# 用 blackboxprotobuf 直接解码

os.environ["NO_PROXY"] = 's.wanfangdata.com.cn'
# headers={"User-Agent":UserAgent().chrome}
headers = {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Content-Type": "application/grpc-web+proto",
    "Origin": "https://s..com.cn",
    "host": "s.wanfangdata.com.cn",
    "Referer": "https://s..com.cn/paper?q=%E4%BD%9C%E8%80%85%E5%8D%95%E4%BD%8D%3A%E5%8C%97%E4%BA%AC%E5%A4%A7%E5%AD%A6&p=4",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
}

deserialize_data = {
    "1": {
        "1": "paper",
        "2": "作者单位:北京大学",
        "5": 1,
        "6": 20,
        "8": "\u0000"
    },
    "2": 1
}

message_type = {'1': {'type': 'message', 'message_typedef': {
    '1': {'type': 'bytes', 'name': ''},
    '2': {'type': 'bytes', 'name': ''},
    '5': {'type': 'int', 'name': ''},
    '6': {'type': 'int', 'name': ''},
    '8': {'type': 'bytes', 'name': ''}}, 'name': ''},
                '2': {'type': 'int', 'name': ''}}

form_data = bytes(blackboxprotobuf.encode_message(deserialize_data, message_type))
f_data = bytes([0, 0, 0, 0, len(form_data)]) + form_data

url = 'https://s.wanfangdata.com.cn/SearchService.SearchService/search'

res = requests.post(url, data=f_data, headers=headers, timeout=10)
#print(res.content)
#print(res.text)
response_data, message_type = blackboxprotobuf.protobuf_to_json(res.content[5:])

print(response_data)
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
print(message_type)


参考连接
https://www.cnblogs.com/xuanlanbinfen/articles/16515170.html

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值