#练习入门 import urllib.request response = urllib.request.urlopen("http://blog.fishc.com/") html = response.read() print(html) #爬虫图片 import urllib.request response = urllib.request.urlopen("http://placekitten.com/g/500/600") cat_img = response.read() with open('cat_500_600','wb')as f: f.write(cat_img)''' #有道翻译 import urllib.request import urllib.parse #parse解析 import json while True: content = input("请输入需要翻译的内容(按q!退出程序):") if content == 'q!': break url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule' data = {} data['i']= content data['from']='AUTO' data['to']='AUTO' data['smartresult']='dict' data['client']='fanyideskweb' data['salt']=1513569368760 data['sign']='de9f67e5eb175603a1a18ab02d28da35' data['doctype']='json' data['version']='2.1' data['keyfrom']='fanyi.web' data['action']='FY_BY_REALTIME' data['typoResult']='false' #encode硬编码 data = urllib.parse.urlencode(data).encode('utf-8') response = urllib.request.urlopen(url,data) #将接收来的utf-8页面解码为unicode html = response.read().decode('utf-8') #打印出来的是字典格式 #print(html) target = json.loads(html) print("翻译结果是:%s" % (target['translateResult'][0][0]['tgt']))
练习有道翻译时,遇见两个坑,一个是url里面_o不去掉的话报错为:50,第二是我把content变量加了单引号,变成了字符串。