初步学习爬虫,写一个关于模拟有道词典翻译文本的脚本
代码import urllib.requestimport urllib.parseimport Jsonimport timewhile True: content = input("请输入你想翻译的内容:") if content == 'q': braek #url中带—_o会导致{"errorCode":50} #url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule' url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule' #获取浏览器类型的'User-Agent信息,避免禁止访问错误 head = {} head['User-Agent'] = 'Mozilla/5.0 (windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36 SLbrowser/7.0.0.1071 SLBChan/1' data = { } data['i'] = content data['from'] = 'auto' data['to'] = 'auto' data['smartresult'] = 'dict' data['clIEnt'] = 'fanyIDeskweb' data['salt'] = '16137170355211' data['sign'] = '37d618ef49272a7dd5325e8a375b261b' data['lts'] = '1613717035521' data['bv'] = '8ec2482c48c5d4cf3ff41e80cd83571b' data['DOCTYPE'] = 'Json' data['version'] = '2.1' data['keyfrom'] = 'fanyi.web' data['action'] = 'FY_BY_REALTlME' data = urllib.parse.urlencode(data).encode('utf-8') #实例化对象 req = urllib.request.Request(url,data,head) response = urllib.request.urlopen(req) HTML = response.read().decode('utf-8') HTML_1 = Json.loads(HTML)['translateResult'][0][0]['tgt'] print("翻译结果—》" + HTML_1) time.sleep(2)
运行结果总结继续努力!!!
总结以上是内存溢出为你收集整理的python爬虫:模拟有道词典翻译文本全部内容,希望文章能够帮你解决python爬虫:模拟有道词典翻译文本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)