简单编程-字符统计
题目
solve
# -*- coding: utf-8 -*-
import urllib.request
import urllib.parse
import http.cookiejar
import string
import re
# url
url = "http://ctf.idf.cn/game/pro/37/"
# set cookie
req = urllib.request.Request( url )
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener( urllib.request.HTTPCookieProcessor( cj ) )
res = opener.open( req )
#正则表达式
content = res.read()
check_text = re.findall( r'<hr />(.*)<hr />',str( content ) )[0]
check_text = check_text[ 10:len( check_text ) - 6 ]
count = [ 0,0,0,0,0 ]#0w 1o 2l 3d 4y
for i in check_text :
if i == 'w' :
count[0] += 1
elif i == 'o' :
count[1] += 1
elif i == 'l' :
count[2] += 1
elif i == 'd' :
count[3] += 1
elif i == 'y' :
count[4] += 1
string = ""
for each in count :
string += str( each )
print( string )
# 接下来就是提交了
value = { 'anwser':string }
data = urllib.parse.urlencode( value ) #先编码成网络字节序?题解只有这一句话,但是我自己用了报错,所以下面加了一句
data = urllib.parse.unquote_to_bytes( data ) #再转换成二进制?
request = urllib.request.Request( url,data )
response = opener.open( request )
html = response.read().decode( 'utf-8' )
print( html )write up1
write up2
write up3
Last updated