Skip to content

Instantly share code, notes, and snippets.

@no13bus
Last active June 15, 2016 07:41
Show Gist options
  • Save no13bus/ce19d3a08aeae2096d32 to your computer and use it in GitHub Desktop.
Save no13bus/ce19d3a08aeae2096d32 to your computer and use it in GitHub Desktop.
模拟登录带有验证码的登录页面
from bs4 import BeautifulSoup
import requests
from PIL import image
import pytesseract
mysession = requests.Session()
url = 'http://110.249.223.91/hbhb/Default.aspx'
r = mysession.get(url,timeout=60*4)
c = r.content
mysoup = BeautifulSoup(c)
inputstring = mysoup.find_all('input', attrs={"name":"__VIEWSTATE"})
inputstring = inputstring[0]
##获取post需要的请求参数__VIEWSTATE的值 赋值为变量viewstate
viewstate = inputstring['value']
captcha_url = 'http://110.249.223.91/hbhb/CheckCode.aspx'
capr = mysession.get(captcha_url,timeout=60*4)
with open('/home/captcha.jpg', 'wb') as f:
f.write(capr.content)
###获取验证码字串 赋值cap_str
cap_str = pytesseract.image_to_string(Image.open('/home/captcha.jpg'))
payload = {'__EVENTTARGET':'','__EVENTARGUMENT':'','__VIEWSTATE':viewstate,'txtUserName':'guest','txtPWD':'123','txtCheckCode':cap_str,'ibtnLogin.x':'25','ibtnLogin.y':'8'}
headers = {
'Content-Type':'application/x-www-form-urlencoded',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding':'gzip,deflate,sdch',
'Accept-Language':'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4',
'Cache-Control':'max-age=0',
'Connection':'keep-alive',
'Content-Type':'application/x-www-form-urlencoded',
'Host':'110.249.223.91',
'Origin':'http://110.249.223.91',
'Referer':'http://110.249.223.91/hbhb/Default.aspx',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.3'
}
result = mysession.post(url, data=payload, headers=headers,timeout=60*4)
print result.content
@CoterJiesen
Copy link

我的怎么识别不出来 。。。求帮助

@CoterJiesen
Copy link

from PIL import image中image首字母是不是应该大写,我小写运行不通过,请指教

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment