马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 cyy6666 于 2021-2-16 03:16 编辑 import urllib.request
import urllib.parse
import urllib.error
import json
import time
import random
import os
import re
import gzip
'''html中以"b’\x1f\x8b\x08"开头的 ,说明它是gzip压缩过的数据'''
'''不是所有网页都用utf-8'''
def url_open(url,data=None):
head={}
head['User-Agent']='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 Edg/87.0.664.75'
req=urllib.request.Request(url,data,head)
response=urllib.request.urlopen(req)
html=response.read()
'''html=gzip.decompress(html)'''
return html
'''收集ip'''
def get_ip():
url=''
html=url_open(url).decode('utf-8')
p=r'(?:(?:25[0-5]|2[0-4]\d|[01]{0,1}\d{0,1}\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]{0,1}\d{0,1}\d):[^@]{1,}'
iplist=re.findall(p,html)
return iplist
'''代理'''
def proxy_url_open(url,data=None):
ip=get_ip()
while True:
try:
proxy_support=urllib.request.ProxyHandler({'http':random.choice(ip)})
'''创建opener'''
opener=urllib.request.build_opener(proxy_support)
'''隐藏'''
opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 Edg/87.0.664.75')]
'''安装opener'''
response=opener.open(url,data)
except:
pass
else:
break
html=response.read()
'''html=gzip.decompress(html)'''
return html
'''有道翻译'''
def translation(proxy=0):
while True:
s=input("请输入需要翻译的内容(输入'#!'退出翻译程序):")
if s=='#!':
break
url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
data={}
data['from']='AUTO'
data['to']='AUTO'
data['smartresult']='dict'
data['client']='fanyideskweb'
data['doctype']='json'
data['version']='2.1'
data['keyfrom']='fanyi.web'
data['action']='FY_BY_CLICKBUTTION'
data['i']=s
data=urllib.parse.urlencode(data).encode('utf-8')
if proxy==0:
html=url_open(url,data).decode('utf-8')
target=json.loads(html)
target=target['translateResult'][0][0]['tgt']
print("翻译结果:%s"%target)
time.sleep(2)
else:
html=proxy_url_open(url,data).decode('utf-8')
target=json.loads(html)
target=target['translateResult'][0][0]['tgt']
print("翻译结果:%s"%target)
if __name__=='__main__':
proxy=input('是否使用代理(0不使用,1使用):')
proxy=int(proxy)
translation(proxy)
'#!'使用英文输入法
新人,请大佬指教 |