python请求从其他网址获取响应

时间:2018-08-07 20:59:46

标签: python python-requests response captcha

是否可以从其他文件获取发布请求响应? 这是我的代码

from PIL import Image
from PIL import ImageEnhance
import PIL.ImageOps
import pytesseract
import numpy
from bs4 import BeautifulSoup
import urllib.request
import requests
import io


pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 '
                         '(KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}

register_url = 'https://register.example.com/index.php'

soup = BeautifulSoup(urllib.request.urlopen(register_url).read(), features="lxml")
url = soup.img['src']


imageResponse = requests.get(url)
captcha_image = Image.open(io.BytesIO(imageResponse.content)).convert('RGB')
image = PIL.ImageOps.grayscale(captcha_image)
image = ImageEnhance.Brightness(image).enhance(3)
image = PIL.ImageOps.colorize(image, (255, 255, 255), (0, 0, 0))
imageArray = numpy.array(image)

text = pytesseract.image_to_string(imageArray, config="-c tessedit_char_whitelist=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")

payload = {
    "username": "teststuff1",
    "pass": "123456",
    "pass2": "123456",
    "email": "userfd@hotmail.com",
    "captcha": text
}

session = requests.Session()
post = session.post(register_url, data =payload, headers=headers)

data = session.get("https://register.example.com/response.php")
print(data.content)

如果不是index.php上的验证码,我可以直接发布到response.php文件中。因此,很明显,如果我将一个后发请求发送到response.php文件,我将得到不正确的验证码错误。我已经尝试了2天,所以我想我会来这里寻求帮助的。

0 个答案:

没有答案