【POST请求-腾讯翻译君-爬虫案例】

原因:尝试多个在线翻译平台,由于返回数据存在加密原因(暂时不会解密),最总找到 "腾讯翻译君" 完成爬虫案例POST请求测试

案例测试网址

* 复制代码
import requests
import json


class Search():

    def __init__(self,  word) -> None:

        self.url = "https://fanyi.qq.com/api/translate"

        self.headers = {
            'User-Agent':
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36'
        }

        self.data = {
            'source': 'auto',
            'target': 'en',
            'sourceText': word,
            'qtv': 'd6b80aee23530d6d',
            'qtk': 'YEYhu1zSCa2JbdJ16xeGfH12oAHABTokH8NMzPVriyAqD0nbaHOA7BEF3naAeSImmaRMOdAmb2xaToIjtFfitIPLNg3UIbmQLhgXKfnM46FG0s5bngRWAE6tzq6k0g/2XMIGR/JPbIzGelVDqqaZuw==',
            'sessionUuid': 'translate_uuid1696487685473'
        }

    def post_data(self):
        response = requests.post(
            self.url, headers=self.headers, data=self.data)

        # print(response)  打印<Response [200]>

        return response.content

    def parse_data(self, data):

        # loads方法将json字符串转换成python字典

        dict_data = json.loads(data)

        print(dict_data['translate']['records'][0]['targetText'])

        # records使用列表格式获取

    def run(self):

        # 编辑爬虫逻辑

        # url

        # headers

        # data 字典

        # 发送请求响应
        response = self.post_data()

        print(response)  # 打印返回结果字符串
        print("*" * 50)

        # 数据解析
        self.parse_data(response)


if __name__ == '__main__':

    word = input("请输入要翻译的单词或句子:")

    search = Search(word)

    search.run()



"""
    "translate": {
        "errCode": 0,
        "errMsg": "",
        "sessionUuid": "translate_uuid1696487685473",
        "source": "zh",
        "target": "en",
        "records": [
            {
                "sourceText": "时间",
                "targetText": "time",
                "traceId": "1e15f8dc9fcd49b795128356b853db58"
            }
        ],
        "full": true,
        "options": {}
    }
"""
相关推荐
0***h9421 小时前
初级爬虫实战——麻省理工学院新闻
爬虫
是有头发的程序猿1 小时前
Python爬虫实战:面向对象编程在淘宝商品数据抓取中的应用
开发语言·爬虫·python
Onebound_Ed2 小时前
Python爬虫进阶:面向对象设计构建高可维护的1688商品数据采集系统
开发语言·爬虫·python
深蓝电商API10 小时前
爬虫登录态维护高级技巧:Cookie 池 + Session 复用实战
爬虫
嫂子的姐夫11 小时前
01-selenium
爬虫·python·selenium·自动化
岁忧20 小时前
GoLang五种字符串拼接方式详解
开发语言·爬虫·golang
小白学大数据1 天前
基于Splash的搜狗图片动态页面渲染爬取实战指南
开发语言·爬虫·python
嫂子的姐夫1 天前
02-多线程
爬虫·python·多线程·并发爬虫·基础爬虫
r***86981 天前
Python中的简单爬虫
爬虫·python·信息可视化
白菜__1 天前
去哪儿小程序逆向分析(酒店)
前端·javascript·爬虫·网络协议·小程序·node.js