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

面对一道要求2秒内完成字符统计且页面内容动态变化的编程题,通过学习POST表单提交并利用Python编写自动化程序解决。关键在于保存首次请求的cookie并在后续请求中使用,以避免超时问题。可以使用第三方库mechanize协助实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一眼看这道题很简单,不就是字符统计么,可是题目要求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()



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值