您的位置:首页 > 编程语言

idf实验室--简单编程字符统计

2015-05-16 10:14 211 查看

第一眼看这道题很简单,不就是字符统计么,可是题目要求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] += 1
elif txt == 'o':
char_count[1] += 1
elif txt == 'l':
char_count[2] += 1
elif txt == 'd':
char_count[3] += 1
elif 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-8

import re
import urllib2
import mechanize

TARGET_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 cotent
page_content = br_controller.response().read()

# Get target text
check_text = get_text(page_content)

# Calculate
for txt in check_text:
if txt == 'w':
char_count[0] += 1
elif txt == 'o':
char_count[1] += 1
elif txt == 'l':
char_count[2] += 1
elif txt == 'd':
char_count[3] += 1
elif 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'] = result
br_controller.submit()

print br_controller.response().read()

if __name__ == '__main__':
submit()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: