欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【Python爬虫】爬取百度翻译的结果

程序员文章站 2022-05-04 12:30:31
...
#coding:utf8
import urllib.request as ur
import urllib.parse as up
import json
while(1):
    word = input("请输入要翻译的中文:");
    data = {
        'kw':word
    }
    data_url =  up.urlencode(data)
    request = ur.Request(
        url='https://fanyi.baidu.com/sug',
        data = data_url.encode('utf-8'),
    )

    response = ur.urlopen(request).read()
    ret = json.loads(response)
    translate = ret['data'][0]['v']
    print(translate)

运行结果:
【Python爬虫】爬取百度翻译的结果