使用requests访问必应在线翻译

本文介绍了如何使用Python的requests库访问必应在线翻译服务。通过抓包获取请求头和表单参数,利用session对象发起POST请求,从而实现对文本的翻译操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@导包

import requests
import json

@在浏览器中访问必应在线翻译首页,抓包获得相应的请求头

# 请求必应在线翻译首页,通过在浏览器中访问抓包请求头
headers_1 = {
    'Host': 'www.bing.com',
    'Connection': 'keep-alive',
    'Cache-Control': 'max-age=0',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0
请修复以上谷歌翻译版本代码的问题:(style_tune) C:\Users\28996\Desktop\AI\persona_contrastive_finetuning\data>python datatrans.py 已添加第三列 '中文翻译' 翻译进度: 0%| | 0/204 [00:00<?, ?it/s]行 1 翻译失败 (尝试 1/3): 'int' object has no attribute 'as_dict' 行 1 翻译失败 (尝试 2/3): 'int' object has no attribute 'as_dict' 行 1 翻译失败 (尝试 3/3): 'int' object has no attribute 'as_dict' 行 1 最终失败,跳过 翻译进度: 0%|▎ | 1/204 [00:03<10:14, 3.03s/it]行 2 翻译失败 (尝试 1/3): 'int' object has no attribute 'as_dict' 行 2 翻译失败 (尝试 2/3): 'int' object has no attribute 'as_dict' 行 2 翻译失败 (尝试 3/3): 'int' object has no attribute 'as_dict' 行 2 最终失败,跳过 翻译进度: 1%|▋ | 2/204 [00:06<10:10, 3.02s/it]行 3 翻译失败 (尝试 1/3): 'int' object has no attribute 'as_dict' 行 3 翻译失败 (尝试 2/3): 'int' object has no attribute 'as_dict' 行 3 翻译失败 (尝试 3/3): 'int' object has no attribute 'as_dict' 行 3 最终失败,跳过 翻译进度: 1%|█ | 3/204 [00:09<10:08, 3.03s/it]行 4 翻译失败 (尝试 1/3): 'int' object has no attribute 'as_dict' 行 4 翻译失败 (尝试 2/3): 'int' object has no attribute 'as_dict' 翻译进度: 1%|█ | 3/204 [00:10<11:19, 3.38s/it] Traceback (most recent call last): File "C:\Users\28996\Desktop\AI\persona_contrastive_finetuning\data\datatrans.py", line 47, in translate_excel translation = translator.translate(english_text, src='en', dest='zh-cn') File "C:\Users\28996\miniconda3\envs\style_tune\lib\site-packages\googletrans\client.py", line 194, in translate data, response = self._translate(text, dest, src) File "C:\Users\28996\miniconda3\envs\style_tune\lib\site-packages\googletrans\client.py", line 120, in _translate r = self.client.post(url, params=params, data=data) File "C:\Users\28996\miniconda3\envs\style_tune\lib\site-packages\httpx\_client.py", line 824, in post return self.request( File "C:\Users\28996\miniconda3\envs\style_tune\lib\site-packages\httpx\_client.py", line 600, in request return self.send( File "C:\Users\28996\miniconda3\envs\style_tune\lib\site-packages\httpx\_client.py", line 620, in send response = self.send_handling_redirects( File "C:\Users\28996\miniconda3\envs\style_tune\lib\site-packages\httpx\_client.py", line 647, in send_handling_redirects response = self.send_handling_auth( File "C:\Users\28996\miniconda3\envs\style_tune\lib\site-packages\httpx\_client.py", line 684, in send_handling_auth response = self.send_single_request(request, timeout) File "C:\Users\28996\miniconda3\envs\style_tune\lib\site-packages\httpx\_client.py", line 719, in send_single_request timeout=timeout.as_dict(), AttributeError: 'int' object has no attribute 'as_dict' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\28996\Desktop\AI\persona_contrastive_finetuning\data\datatrans.py", line 86, in <module> translate_excel(input_path, output_path) File "C:\Users\28996\Desktop\AI\persona_contrastive_finetuning\data\datatrans.py", line 71, in translate_excel time.sleep(1) KeyboardInterrupt
最新发布
07-31
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬砖的乔布梭

你好我是秦始皇转世,资助请从速

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值