【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": {}
    }
"""
相关推荐
B站计算机毕业设计之家2 小时前
基于Python+Django+双协同过滤豆瓣电影推荐系统 协同过滤推荐算法 爬虫 大数据毕业设计(源码+文档)✅
大数据·爬虫·python·机器学习·数据分析·django·推荐算法
孤狼warrior7 小时前
目前最新同花顺金融股市数据爬取 JS逆向+node.js补浏览器环境
javascript·爬虫·python·金融·node.js
xiaoxiongip66618 小时前
假设两个设备在不同网段,网关怎么设置才能通呢
网络·爬虫·python·https·智能路由器
tryCbest18 小时前
Python基础之爬虫技术(一)
开发语言·爬虫·python
疏狂难除18 小时前
某个网址的爬虫——mitmproxy的简单使用
爬虫·mitmproxy
想要打 Acm 的小周同学呀18 小时前
爬虫相关的面试问题
爬虫·selenium·职场和发展
QMY52052021 小时前
爬虫技术抓取网站数据的方法
运维·爬虫·自动化
傻啦嘿哟1 天前
房地产爬虫实战:链家二手房数据抓取与深度分析
爬虫
雨中散步撒哈拉2 天前
16、做中学 | 初三上期 Golang面向对象_进阶
爬虫·python·golang
召唤神龙2 天前
爬虫代理IP池搭建指南:实测推荐高可用服务商
爬虫·tcp/ip