idf实验室--简单编程字符统计,有需要的朋友可以参考下。
第一眼看这道题很简单,不就是字符统计么,可是题目要求2s内回答,而且每次打开的页面需要统计的字符串内容都会变,这就蛋疼了,于是乎上网学习下如何提交post表单,然后用python写个程序自动提交就ok了 ( 题目地址 )
代码如下:
# -*- coding: utf-8 -*- import urllib2 import urllib import cookielib import string import re#需要提交post的url TARGET_URL = "http://ctf.idf.cn/game/pro/37/"# 设置一个cookie处理器 req = urllib2.Request(TARGET_URL) cj = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) res = opener.open(req)# 通过正则匹配抓到需要统计的字符串 content = res.read() check_text = re.findall(r'<hr />(.*)<hr />',content,re.S)[0]# 简单的统计 char_count = [0,0,0,0,0] for txt in check_text:if txt == 'w':char_count[0] += 1elif txt == 'o':char_count[1] += 1elif txt == 'l':char_count[2] += 1elif txt == 'd':char_count[3] += 1elif txt == 'y':char_count[4] += 1#将数字转换成字符串 result = "" for nIndex in char_count:result += str(nIndex) print "Result = ", result# 接下来就是提交了 value = {'anwser': result} data = urllib.urlencode(value) request = urllib2.Request(TARGET_URL,data) response = opener.open(request) html = response.read() print html
需要注意的地方:你需要保存下来第一次正则匹配时打开页面cookie,构造一个opener,在第二次提交时使用之前的cookie即可。。。否则会提示超时
下面是一个大牛给我的代码,用到了第三方库mechanize:
# coding=utf-8import re import urllib2 import mechanizeTARGET_URL = "http://ctf.idf.cn/game/pro/37/" USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36 QQBrowser/3.5.3420.400"# Get target text use regular expression. def get_text(content):return re.findall(r'<hr />(.*)<hr />', content,re.S)[0]def submit():char_count = [0, 0, 0, 0, 0]br_controller = mechanize.Browser()br_controller.set_handle_equiv(True)br_controller.set_handle_redirect(True)br_controller.set_handle_referer(True)br_controller.set_handle_robots(False)br_controller.addheaders = [("User-Agent", USER_AGENT)]br_controller.open(TARGET_URL)# Get web page cotentpage_content = br_controller.response().read()# Get target textcheck_text = get_text(page_content)# Calculatefor txt in check_text:if txt == 'w':char_count[0] += 1elif txt == 'o':char_count[1] += 1elif txt == 'l':char_count[2] += 1elif txt == 'd':char_count[3] += 1elif txt == 'y':char_count[4] += 1# Change value in char_count to string.result = ""for nIndex in char_count:result += str(nIndex)print "Result = ", result# Post form.br_controller.select_form(nr=0)br_controller.form['anwser'] = resultbr_controller.submit()print br_controller.response().read()if __name__ == '__main__':submit()