pythonテキストを有道辞書で翻訳(エージェント)
4518 ワード
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......
エージェント
ステップ
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)
長いこと探して1つの使うことができる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']))