python用有道词典翻译文本(代理)

import urllib.request
import urllib.parse
import json
import time

while True:
   
    
    list1 = input('请输入你要翻译的内容(输入"q!"退出程序):')
    if list1 == 'q!':
        break
    
    head = {}
    head['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36'
    
    url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
    data = {}
    data['tyep'] = 'AUTO'
    data['i'] = list1
    
    data['smartresult'] = 'dict'
    data['client'] =  'fanyideskweb'
    data['salt'] =  '1536914428238'
    data['sign'] =  'a067b3b10b892683417e05611c41d0dc'
    data['doctype'] = 'json'
    data['version'] = '2.1'
    data['keyfrom'] = 'fanyi.web'
    data['action'] =  'FY_BY_CLICKBUTTION'
    data['typoResult'] = 'false'
    
    data = urllib.parse.urlencode(data).encode('utf-8') 
    
    req = urllib.request.Request(url,data,head)
    #req.add_header('User-Agent',
    #              'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36')
    
    response = urllib.request.urlopen(req)
    html = response.read().decode('utf-8')
    
    
    dic = json.loads(html)
    print('翻译结果:%s'%(dic['translateResult'][0][0]['tgt']))
    
    time.sleep(5)

这样效率太低了,so…………

代理

步骤

  1. List item
    参数是一个字典{‘类型’,‘代理ip:端口号’}
    proxy_support = urllib.request.ProxyHandler({})

  2. 定制、创建一个opener
    opener = urllib.request.build_opener( proxy_support)

  3. a安装opener
    urllib.request.install_opener(opener)

  4. b调用opener
    opener.open(url)

import urllib.request
import random

url = 'https://blog.csdn.net/'

iplist = ['61.135.217.7:80','106.75.164.15:3128','115.154.1.195:8118']
proxy_support = urllib.request.ProxyHandler({'http':random.choice(iplist)})
opener = urllib.request.build_opener(proxy_support)
opener.addheaders = [('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36')]

urllib.request.install_opener(opener)
response = urllib.request.urlopen(url)
html = response.read().decode('utf-8')

print(html)

找了好久就找到一个能用的IP


import urllib.request
import urllib.parse
import json
import time
import random

while True:
 
  
  list1 = input('请输入你要翻译的内容(输入"q!"退出程序):')
  if list1 == 'q!':
      break
  
  iplist = ['61.135.217.7:80']
  proxy_support = urllib.request.ProxyHandler({'http':random.choice(iplist)})
  opener = urllib.request.build_opener(proxy_support)
  opener.addheaders = [('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36')]
  
  urllib.request.install_opener(opener)

  head = {}
  head['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36'
  
  url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
  data = {}
  data['tyep'] = 'AUTO'
  data['i'] = list1
  
  data['smartresult'] = 'dict'
  data['client'] =  'fanyideskweb'
  data['salt'] =  '1536914428238'
  data['sign'] =  'a067b3b10b892683417e05611c41d0dc'
  data['doctype'] = 'json'
  data['version'] = '2.1'
  data['keyfrom'] = 'fanyi.web'
  data['action'] =  'FY_BY_CLICKBUTTION'
  data['typoResult'] = 'false'
  
  data = urllib.parse.urlencode(data).encode('utf-8') 
  
  req = urllib.request.Request(url,data,head)
  #req.add_header('User-Agent',
  #              'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36')
  
  response = urllib.request.urlopen(req)
  html = response.read().decode('utf-8')
  
  
  dic = json.loads(html)
  print('翻译结果:%s'%(dic['translateResult'][0][0]['tgt']))
  

你可能感兴趣的:(笔记)