第十一章 获取验证码提交答案

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
#此代码是书上151页获取验证码提交答案
#需要安装pillow库进行图片的转换和像素的变化
#需要安装tesseract进行图片文字的识别

#这段代码的思路是:先打开需要验证码填写的网页,获取必要的信息,将验证码转换为字符串,
#查看当前网页的提交的路径,然后将这些参数传递过去,判断是否成功即可
from urllib.request import urlopen, urlretrieve

import requests
from PIL import Image
from PIL import ImageOps

#利用pillow库对图片进行像素和尺寸的变化
from bs4 import BeautifulSoup

#用于创建子进程
import subprocess
def cleanImage(imagePath):
    image=Image.open(imagePath)
    image=image.point(lambda x:0 if x<143 else 255)
    borderImage=ImageOps.expand(image,border=20,fill='white')
    borderImage.save(imagePath)
html=urlopen("http://www.pythonscraping.com/humans-only")
bsObj=BeautifulSoup(html)
#收集需要处理的表单数据(包括验证码和输入字段)
imageLocation=bsObj.find("img",{"title":"Image CAPTCHA"})["src"]
formBuildId=bsObj.find("input",{"name":"form_build_id"})["value"]
captchaSid=bsObj.find("input",{"name":"captcha_sid"})["value"]
captchaToken=bsObj.find("input",{"name":"captcha_token"})["value"]

captchaUrl="http://pythonscraping.com"+imageLocation
urlretrieve(captchaUrl,"captcha.jpg")
cleanImage("captcha.jpg")
#系统调用tesseract,由于没有安装,因此下面这行代码有错就先注释,在运行中需要安装加解注释
#p=subprocess.Popen(["tesseract","page.jpg","page"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
#p.wait()
f=open("captcha.txt","r")

#清理识别结果中的空格和换行符
captchaResponse=f.read().replace(" ","").replace("\n","")
print("captcha solution attempt: "+captchaResponse)

if len(captchaResponse)==5:
    params={
        "captcha_token":captchaToken,
        "captcha_sid":captchaSid,
        "form_id":"comment_node_page_form",
        "form_build_id":formBuildId,
        "captcha_reponse":captchaResponse,
        "name":"Ryan Mitchell",
        "subject":"I come to check the Grail",
        "comment_body[und][0][value]":"... and I am definitely not a bot"
    }
    r= requests.post("http://www.pythonscraping.com/comment/reply/10",data=params)
    reponseObj=BeautifulSoup(r.text)
    if reponseObj.find("div",{"class":"messages"}) is not None:
        print(reponseObj.find("div",{"class":"messages"}).get_text())
    else:
        print("There is a problem reading the CHPTCHA correctly!")

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值