python 爬取TOJ 上 读取 提交但未AC 的题目

KS想看自己在TOJ上提交的但是没AC的题目 ,但是又不想一页一页的翻 ; 正好这几天才学了pyhon ,就想写个爬虫程序,才爬取;需要输入toj的账号密码信息,用的还是前面介绍的,TOJ涉及到了登录, 可以通过审查元素来查看,需要什么信息, 还好TOJ的加密没有QQ空间那么高(e)端(xin)=== 直接按正常步骤就可以登录 ,下面就是对页面的解析了;与一般页面不同的是, TOJ把user对题目的state 存在js里面了,因此后面用到的是解析字符串,而非html, 简单一些了

贴图:


代码如下:(PS: 写的不完善,登录失败会没有信息输出)

# -*- coding: cp936 -*-


import urllib2,urllib,cookielib,time,os,sys
from bs4 import BeautifulSoup

  

class Problem:
	def __init__(self,state,id,name):
		self.id=id
		self.state=state
		self.name=name


#cookie
cookiejar= cookielib.CookieJar()
cookieproc= urllib2.HTTPCookieProcessor(cookiejar)
opener= urllib2.build_opener(cookieproc)
urllib2.install_opener(opener)


do_wa=[]#save unac problems
url='http://acm.tju.edu.cn/toj/list.php?vol='

username=raw_input('input your id for TOJ:')
password=raw_input('input your password:')
domain='acm.tju.edu.cn'


##handle
def handle(str):
	index=str.index('}')
	str=str[index+2:-10]
	p_list=str.split('\n')
	for p in p_list:
		list_do=p.split(',')
		if list_do[1]=='2':
			do_wa.append(Problem(list_do[1],list_do[2],list_do[3]))



#headers and postdata for broswers
headers={
	'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0'
}
postdata={
	'user_id':username,\
	'passwd':password,
	'domain':domain,
	'login':'Login'
}
postdata=urllib.urlencode(postdata)

#handle
print username,'do but un_ac:'
for i in range(1,100):
	#time.sleep(0.1)
	toj_url=url+str(i)
	req=urllib2.Request(toj_url,postdata)
	html=urllib2.urlopen(req).read()
	html= BeautifulSoup(html)
	script=html.find('script')
	script=str(script)
	if script=='None':
		break
	handle(script)

for i in do_wa :	
	print i.id,i.name
os.system('pause')



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值