Python爬虫实现百度翻译功能过程详解
作者:javascript痴痴 时间:2024-01-01 08:02:10
首先,需要简单的了解一下爬虫,尽可能简单快速的上手,其次,需要了解的是百度的API的接口,搞定这个之后,最后,按照官方给出的demo,然后写自己的一个小程序
打开浏览器 F12 打开百度翻译网页源代码:
我们可以轻松的找到百度翻译的请求接口为:http://fanyi.baidu.com/sug
然后我们可以从方法为POST的请求中找到参数为:kw:job(job是输入翻译的内容)
下面是代码部分:
from urllib import request,parse
import json
def translate(content):
url = "http://fanyi.baidu.com/sug"
data = parse.urlencode({"kw":content}) # 将参数进行转码
headers = {
'User-Agent': 'Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10'
}
req = request.Request(url,data=bytes(data,encoding="utf-8"),headers=headers)
r = request.urlopen(req)
# print(r.code) 查看返回的状态码
html = r.read().decode('utf-8')
# json格式化
html = json.loads(html)
# print(html)
for k in html["data"]:
print(k["k"],k["v"])
if __name__ == '__main__':
content = input("请输入您要翻译的内容:")
translate(content)
结果如下
来源:https://www.cnblogs.com/homehtml/p/12821609.html
标签:Python,爬虫,百度,翻译
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python reduce()函数的用法小结
2022-02-27 11:28:45
![](https://img.aspxhome.com/file/2023/4/121004_0s.png)
由浅到深了解JavaScript类
2008-06-16 13:20:00
My Sql 1067错误与编码问题的解决方案
2024-01-19 12:53:53
![](https://img.aspxhome.com/file/2023/7/122897_0s.png)
python3爬虫中多线程的优势总结
2023-05-15 02:41:07
python中使用Celery容联云异步发送验证码功能
2023-07-29 08:27:06
![](https://img.aspxhome.com/file/2023/2/135412_0s.jpg)
vue-form表单验证是否为空值的实例详解
2024-04-30 10:40:03
vue判断input输入内容全是空格的方法
2024-04-09 10:48:01
Python入门教程(三十三)Python的字符串格式化
2023-04-03 11:58:17
![](https://img.aspxhome.com/file/2023/6/112736_0s.jpg)
python多次绘制条形图的方法
2021-07-22 09:36:45
![](https://img.aspxhome.com/file/2023/5/81625_0s.jpg)
Go外部依赖包从vendor,$GOPATH和$GOPATH/pkg/mod查找顺序
2024-04-28 10:49:59
![](https://img.aspxhome.com/file/2023/0/134120_0s.jpg)
WEB2.0网页制作标准教程(7)CSS学习入门
2007-12-13 13:12:00
在Python中使用MySQL--PyMySQL的基本使用方法
2024-01-22 20:32:49
利用pyinstaller或virtualenv将python程序打包详解
2022-08-01 10:37:52
浅析Go语言中的Range关键字
2024-02-10 10:57:42
八卦调侃Reset CSS
2010-01-13 13:01:00
vue使用@scroll监听滚动事件时,@scroll无效问题的解决方法详解
2024-05-09 15:24:55
django和flask哪个值得研究学习
2021-10-02 16:52:46
python下MySQLdb用法实例分析
2024-01-18 11:50:27
JavaScript图片放大镜效果
2009-10-19 22:15:00
![](https://img.aspxhome.com/file/UploadPic/up/2009101922390143.gif)
Keras神经网络efficientnet模型搭建yolov3目标检测平台
2021-10-08 11:45:33
![](https://img.aspxhome.com/file/2023/2/95202_0s.png)