IDF實驗室:初探乾坤--簡單編程-字元統計,idf初探
地址:
ctf.idf.cn/index.php?g=game&m=article&a=index&id=37
題目:
這裡這裡 → http://ctf.idf.cn/game/pro/37
Writeup:
(第二份代碼引用他人新浪部落格:blog.sina.com.cn/s/blog_e53f38130102vjlz.html )
很明顯,編寫代碼分別統計woldy五個字母的數量,並提交。但是注意需要在2秒內提交,所以需要寫爬蟲,With Python !!!
第一次:自己用Python3.4寫的:( 原始碼如下 )
我連Cookies ,和 Headers 都全部偽裝了。。。
但是他總是返回給我說,“你數學是小學體育老師教的嗎?”
我就無語了!!!!
import urllib.request
import urllib.parse
import re
url = "http://ctf.idf.cn/game/pro/37/index.php"
req = urllib.request.Request(url)
response = urllib.request.urlopen(url)
html = response.read().decode('utf-8')
A = html.find('<hr />') + 6
B = html.find('<hr />', A)
f = html[A:B]
w = f.count('w')
o = f.count('o')
l = f.count('l')
d = f.count('d')
y = f.count('y')
ans1 = '%d'%w+'%d'%o+'%d'%l+'%d'%d+'%d'%y
length = str(len(ans1))
head = {}
head['Host'] = "ctf.idf.cn"
head['User-Agent'] = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0"
head['Accept-Language'] = "zh-CN,en-US;q=0.7,en;q=0.3"
head['Accept-Encoding'] = "gzip, deflate"
head['Referer'] = "http://ctf.idf.cn/game/pro/37/"
head['Cookie'] = "Hm_lvt_184d7dcce9f76d1f5ab23d66e447d9a8=1432209840,1432307014,1433080747,1433157423; PHPSESSID=23ro01bddb6a7604ovumie8nr7; Hm_lpvt_184d7dcce9f76d1f5ab23d66e447d9a8=1433157799"
head['Connection'] = "keep-alive"
head['Cache-Control'] ="max-age=0"
xdata['Content-Type'] = "application/x-www-form-urlencoded"
xdata['Content-Length'] = length
xdata = {'anwser':ans1}
xdata = urllib.parse.urlencode(xdata).encode('utf-8')
req = urllib.request.Request(url, data = xdata)
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')
A = html.find('<body>') + 6
B = html.find('<hr />', A )
f = html[A:B]
print(f)
第二次:用 Python2.7 編寫:(原代碼如下)
首先安裝BeautifulSoup
代碼參考一個新浪部落格:blog.sina.com.cn/s/blog_e53f38130102vjlz.html
#! /usr/python
#coding:utf-8
import sys, urllib,urllib2
import requests
#from BeautifulSoup import BeautifulSoup
from bs4 import BeautifulSoup
url = "http://ctf.idf.cn/game/pro/37/" #網頁地址
s = requests.session()
content = s.get("http://ctf.idf.cn/game/pro/37/").text #擷取頁面內容
test=content.split('<hr />') #把字串用分成3部分
print test[1]
w=0
o=0
l=0
d=0
y=0
for i, ch in enumerate(test[1]): #遍曆分割後的第二部分字串
if ch=="w":
w=w+1
elif ch=="o":
o=o+1
elif ch=="l":
l=l+1
elif ch=="d":
d=d+1
elif ch=="y":
y=y+1
tem='%d' %w +'%d' %o +'%d' %l +'%d' %d +'%d' %y #把數字拼成字串
print tem
values = {'anwser':tem} #填寫表單
result = s.post('http://ctf.idf.cn/game/pro/37/', data=values) #提交表單
print(result.text)
第二次可以跑出結果。
在這裡,我想請教一下,我自己寫的東西哪裡出了問題呢???
本人python 新手一隻,求高手解答,十分感激。