python爬虫(4)四种方法通过黑板客第二关

来源:互联网 发布:windowsxp系统修复软件 编辑:程序博客网 时间:2024/05/17 22:51

黑板客第二关网址是 http://www.heibanke.com/lesson/crawler_ex01/ 

第二关的页面如下:


即要求用户输入用户名和密码,然后闯关成功

用户名没有规则,可以任意输入,而密码是一个30以内的数字,并不是真正我们注册的用户名和密码组合

因此,这就需要我们去依次尝试输入30以内的数字作为密码


这种方式需要我们向网站做出一个互动:自动提交表单

第一种方法

通过urllib 提交内容

#!/usr/bin/python# coding:utf-8import urllibimport reimport sysreload(sys)sys.setdefaultencoding('utf-8') data={'username':'qiqiyingse'}url='http://www.heibanke.com/lesson/crawler_ex01/' for num in xrange(1,31):data['password']=numpost_data=urllib.urlencode(data)print post_dataresponse=urllib.urlopen(url,post_data)html=response.read()result=re.findall('密码错误',html)if not result:print '闯关成功,下一关网址是:http://www.heibanke.com'+re.findall(r'<a href="(.*?)" class',html)[0]break 

所耗时长:

闯关成功,下一关网址是:http://www.heibanke.com/lesson/crawler_ex02/run time is  0:00:12.546250

第二种方法

urllib2 的方式,用urllib 打包数据

#!/usr/bin/python# coding:utf-8import urllib2import reimport urllibimport datetimebegin_time=datetime.datetime.now()data={'username':'qiqiyingse'}url='http://www.heibanke.com/lesson/crawler_ex01/' for num in range(1,31):data['password']=numpost_data=urllib.urlencode(data)print post_dataresponse=urllib2.urlopen(url,post_data)html=response.read()result=re.findall('密码错误',html)if not result:print '闯关成功,下一关网址是:http://www.heibanke.com'+re.findall(r'<a href="(.*?)" class',html)[0]print 'run time is ',datetime.datetime.now()-begin_timebreak 

所耗时长:

闯关成功,下一关网址是:http://www.heibanke.com/lesson/crawler_ex02/run time is  0:00:10.163122

这种方法的数据打包也可以用下面这两句代码

post_data=urllib.urlencode(data)request=urllib2.Request(url,post_data)response=urllib2.urlopen(request)
只是里面多了一个urllib2里面的Request 方法来进行打包

第三种方法

使用request的post方法来提交数据

#!/usr/bin/python# coding:utf-8import requestsimport reimport datetimeimport sysreload(sys)sys.setdefaultencoding('utf-8')begin_time=datetime.datetime.now()url = 'http://www.heibanke.com/lesson/crawler_ex01/'payload = {'username': 'test', 'password': 0}for n in range(30):payload['password'] = ncontent = requests.post(url, payload).textpattern = r'<h3>(.*)</h3>'result = re.findall(pattern, content)print "try enter ",n,result[0]if u"错误" not in result[0]:print result[0]+'\n下一关网址是:http://www.heibanke.com'+re.findall(r'<a href="(.*?)" class',content)[0]print 'run time is ',datetime.datetime.now()-begin_timebreak
所耗费时长

下一关网址是:http://www.heibanke.com/lesson/crawler_ex02/run time is  0:00:09.232878

第四种方法

使用webdriver的方式,相当于直接通过页面输入,页面点击的方式

#!/usr/bin/python#coding=utf-8 '''通过webdriverde 凡是,利用PhantomJS来登录电脑上需要安装Selenium'''import time,sysfrom selenium import webdriverreload(sys)sys.setdefaultencoding('utf-8')#计算时间函数def print_run_time(func):    def wrapper(self, *args, **kw):        local_time = time.time()        func(self)        print 'run time is {:.2f}:'.format(time.time() - local_time)    return wrapperclass heibanke:@print_run_timedef heibank_ex02(self):testurl="http://www.heibanke.com/lesson/crawler_ex01/"cap = webdriver.DesiredCapabilities.PHANTOMJSdriver = webdriver.PhantomJS()for i in xrange(31):driver.get(testurl)#print driver.current_urldriver.find_element_by_name("username").send_keys("Jimy")driver.find_element_by_name("password").send_keys(i)driver.find_element_by_id("id_submit").click()print "当前输入的密码是:",i,if "错误" not in driver.find_element_by_tag_name('h3').text:print driver.find_element_by_tag_name('h3').textbreakprint driver.find_element_by_tag_name('h3').textprint 'end'driver.quit()returnif __name__ == '__main__':h2=heibanke()h2.heibank_ex02()
运行结果如下:

当前输入的密码是: 0 您输入的密码错误, 请重新输入当前输入的密码是: 1 您输入的密码错误, 请重新输入当前输入的密码是: 2 您输入的密码错误, 请重新输入当前输入的密码是: 3 您输入的密码错误, 请重新输入当前输入的密码是: 4 您输入的密码错误, 请重新输入当前输入的密码是: 5 您输入的密码错误, 请重新输入当前输入的密码是: 6 您输入的密码错误, 请重新输入当前输入的密码是: 7 您输入的密码错误, 请重新输入当前输入的密码是: 8 您输入的密码错误, 请重新输入当前输入的密码是: 9 您输入的密码错误, 请重新输入当前输入的密码是: 10 您输入的密码错误, 请重新输入当前输入的密码是: 11 您输入的密码错误, 请重新输入当前输入的密码是: 12 恭喜! 用户Jimy成功闯关, 继续你的爬虫之旅吧endrun time is 30.44:

小结

其实还有其他多种方法,接触多了自然就知晓了。
我们最终的目的是练习,目的达到就好。
大家还有其他什么方法,欢迎留言




1 0
原创粉丝点击