我如何 运行 我的 python 代码作为脚本
How can I run my python code as a script
我是 python 的新手,所以请多多包涵。
我想创建 python 在线解决验证码的代码。我在 windows 而不是 linux 开发,现在虽然我有很多问题。
1st 我不明白我的 python 文件 运行 会如何出现在实时网站上。解决网站显示的验证码。
2nd 我已经设法开发并获得了一些代码,但我认为它不能正常工作,或者至少不能按照我想要的方式工作。当我 运行 它与 cmd 没有任何反应。
这是我的代码:
from PIL import Image
import ImageEnhance
from pytesser import *
from urllib import urlretrieve
def get(link):
urlretrieve(link,'temp.png')
get('http://www.example.com/');
im = Image.open("temp.png")
nx, ny = im.size
im2 = im.resize((int(nx*5), int(ny*5)), Image.BICUBIC)
im2.save("temp2.png")
enh = ImageEnhance.Contrast(im)
enh.enhance(1.3).show("30% more contrast")
imgx = Image.open('temp2.png')
imgx = imgx.convert("RGBA")
pix = imgx.load()
for y in xrange(imgx.size[1]):
for x in xrange(imgx.size[0]):
if pix[x, y] != (0, 0, 0, 255):
pix[x, y] = (255, 255, 255, 255)
imgx.save("bw.gif", "GIF")
original = Image.open('bw.gif')
bg = original.resize((116, 56), Image.NEAREST)
ext = ".tif"
bg.save("input-NEAREST" + ext)
image = Image.open('input-NEAREST.tif')
print image_to_string(image)
有人可以帮我修复这段代码并向我解释如何在网站上使用它 e.x
I cannot understand how will my python file run on a live website.
我想我可以帮助你理解。您不会 运行 您的 python 脚本“在实时网站上”。你想要的是 运行 你 python 在你的机器上本地编写脚本,但是作为一个更大的程序的一部分,这个程序充当一个自动客户端,旨在与你正在破解其验证码的服务器进行交互。
比较这两个程序:
- Google Chrome 是一个 人工引导的网络客户端 ,它可以 与任何网络服务器交互.
- 您的脚本是一个自动客户端,并且它只能与您设计的网络服务器交互.
这就是我所说的特定设计:您将客户端设计为从特定 URL 的网络服务器获取验证码图像,并以特定于网络服务器的格式提交数据。像这样,例如:
- 加载您想要的网站,例如
httplib
执行 HTTP GET
。
- 提取验证码图像并解决
- 提交带有已解决的验证码字符串和所需数据的其余部分的表单,再次使用像
httplib
这样的 http 客户端执行 HTTP POST
。 (当您在网站上填写表格时,POST
与 "submit" 按钮的作用相同。)
您当前的脚本执行#1 的一部分 - 但它只提取图像,不获取页面的其余部分。如果您的预处理和 image_to_string
函数有效,那么 #2 就完成了。
我是 python 的新手,所以请多多包涵。 我想创建 python 在线解决验证码的代码。我在 windows 而不是 linux 开发,现在虽然我有很多问题。
1st 我不明白我的 python 文件 运行 会如何出现在实时网站上。解决网站显示的验证码。
2nd 我已经设法开发并获得了一些代码,但我认为它不能正常工作,或者至少不能按照我想要的方式工作。当我 运行 它与 cmd 没有任何反应。
这是我的代码:
from PIL import Image
import ImageEnhance
from pytesser import *
from urllib import urlretrieve
def get(link):
urlretrieve(link,'temp.png')
get('http://www.example.com/');
im = Image.open("temp.png")
nx, ny = im.size
im2 = im.resize((int(nx*5), int(ny*5)), Image.BICUBIC)
im2.save("temp2.png")
enh = ImageEnhance.Contrast(im)
enh.enhance(1.3).show("30% more contrast")
imgx = Image.open('temp2.png')
imgx = imgx.convert("RGBA")
pix = imgx.load()
for y in xrange(imgx.size[1]):
for x in xrange(imgx.size[0]):
if pix[x, y] != (0, 0, 0, 255):
pix[x, y] = (255, 255, 255, 255)
imgx.save("bw.gif", "GIF")
original = Image.open('bw.gif')
bg = original.resize((116, 56), Image.NEAREST)
ext = ".tif"
bg.save("input-NEAREST" + ext)
image = Image.open('input-NEAREST.tif')
print image_to_string(image)
有人可以帮我修复这段代码并向我解释如何在网站上使用它 e.x
I cannot understand how will my python file run on a live website.
我想我可以帮助你理解。您不会 运行 您的 python 脚本“在实时网站上”。你想要的是 运行 你 python 在你的机器上本地编写脚本,但是作为一个更大的程序的一部分,这个程序充当一个自动客户端,旨在与你正在破解其验证码的服务器进行交互。
比较这两个程序:
- Google Chrome 是一个 人工引导的网络客户端 ,它可以 与任何网络服务器交互.
- 您的脚本是一个自动客户端,并且它只能与您设计的网络服务器交互.
这就是我所说的特定设计:您将客户端设计为从特定 URL 的网络服务器获取验证码图像,并以特定于网络服务器的格式提交数据。像这样,例如:
- 加载您想要的网站,例如
httplib
执行HTTP GET
。 - 提取验证码图像并解决
- 提交带有已解决的验证码字符串和所需数据的其余部分的表单,再次使用像
httplib
这样的 http 客户端执行HTTP POST
。 (当您在网站上填写表格时,POST
与 "submit" 按钮的作用相同。)
您当前的脚本执行#1 的一部分 - 但它只提取图像,不获取页面的其余部分。如果您的预处理和 image_to_string
函数有效,那么 #2 就完成了。