如何翻译脚本i python 2,7
这个脚本在Python34中工作 如何翻译Python27如何翻译脚本i python 2,7,python,Python,这个脚本在Python34中工作 如何翻译Python27 import urllib.request, re, urllib.parse from base64 import b64encode import codecs import time def send_cap(key, fn): print ('1') data=open(fn, 'rb') print ('2') s = b64encode(data.read()) print ('3'
import urllib.request, re, urllib.parse
from base64 import b64encode
import codecs
import time
def send_cap(key, fn):
print ('1')
data=open(fn, 'rb')
print ('2')
s = b64encode(data.read())
print ('3')
image = s.decode('latin-1')
print ('4')
params = urllib.parse.urlencode({'method': 'base64', 'key': key, 'body':image, 'ext':'jpg'})
print ('5')
bindata=params.encode('ascii')
f = urllib.request.urlopen("http://antigate.com/in.php", bindata)#params)
print ('6')
res = str(f.read().decode())
print(res)
cap_id = re.search('\d+',res)
if cap_id == None:
return False
else:
idc = cap_id.group(0)
return idc
def get_cap_text(key, cap_id):
time.sleep(5)
res_url= 'http://antigate.com/res.php'
res_url+= "?" + urllib.parse.urlencode({'key': key, 'action': 'get', 'id': cap_id})
flag=False
while flag==False:
res= urllib.request.urlopen(res_url).read()
res = res.decode()
if res == 'CAPCHA_NOT_READY':
time.sleep(1)
else:
flag=True
res= re.split('[\W]+',res)
if len(res[0]) == 2:
return res[1]
else:
return ('ERROR', res[0])
res = send_cap('eeeee','capcha2.jpg')
print(res)
text= get_cap_text('eeeee', res)
print (text)
如果这在Python 3中有效,那么您应该将包含
urllib
的几行代码更改为转换:
import urllib, re # first line of imports
urllib.urlopen # no urllib.request
urllib.urlencode # no urllib.parse
不要忘记将
print(x)
更改为print x
@alexeygorozanov从Python 3转换为Python 2并不重要。请为REs使用原始字符串:ap\u id=re.search(r'\d+',REs)
和re.split(r'[\W]+',REs否则\字符将仅转义后面的字符(不是特定于Python 3)。