【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": {}
    }
"""
相关推荐
深蓝电商API1 小时前
处理字体反爬:woff字体文件解析实战
爬虫·python
NPE~2 小时前
自动化工具Drissonpage 保姆级教程(含xpath语法)
运维·后端·爬虫·自动化·网络爬虫·xpath·浏览器自动化
喵手8 小时前
Python爬虫实战:电商价格监控系统 - 从定时任务到历史趋势分析的完整实战(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·电商价格监控系统·从定时任务到历史趋势分析·采集结果sqlite存储
摘星|8 小时前
正则匹配与爬虫爬取图片路径综合练习
爬虫
喵手8 小时前
Python爬虫实战:京东/淘宝搜索多页爬虫实战 - 从反爬对抗到数据入库的完整工程化方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·京东淘宝页面数据采集·反爬对抗到数据入库·采集结果csv导出
0思必得09 小时前
[Web自动化] Selenium获取元素的子元素
前端·爬虫·selenium·自动化·web自动化
搂着猫睡的小鱼鱼1 天前
Ozon 商品页数据解析与提取 API
爬虫·php
深蓝电商API1 天前
住宅代理与数据中心代理在爬虫中的选择
爬虫·python
csdn_aspnet1 天前
Libvio.link爬虫技术深度解析:反爬机制破解与高效数据抓取
爬虫·反爬·libvio
0思必得01 天前
[Web自动化] Selenium处理滚动条
前端·爬虫·python·selenium·自动化