最近在学习C++,爬下一些题来,用于平时练手。
原理其实也很简单,就是生成页面链接,下载网页,提取内容,保存为xml。
用到正则表达式来匹配,minidom来生成xml文件。
(相关URL已更改,题库将上传到sae的网页)
#coding:utf8 # c++ exercises crawler by bosshida,2014.1.2 import urllib2 import re import string from xml.dom import minidom # regex pattern for problem page pNamePattern = re.compile("<h2.*?>(.*?)</h2>",re.S) # regex pattern for problem description pDescPattern = re.compile('<td.*?background="srcs/bg_mid.gif".*?>(.*?)</td>',re.S) # regex pattern for input, output, sampleInput, sampleOutput pPrePattern = re.compile("<pre.*?>(.*?)</pre>",re.S) # generate problem set page url def genProblemSetUrl(count): pSetUrl = "http://xxx.edu.cn:8080/JudgeOnline/problemset.jsp?vol=%d" % count #pSetUrl = "/Users/apple/Dropbox/pysrc/problem%d" % count #pSetUrl = pSetUrl + ".htm" return pSetUrl # get next problem url by regex def getAllProblemUrl(page): pUrls = re.findall('<a href="(problem.jsp\?id=\d+)">',page, re.S) return pUrls # download page content def loadPage(url): response = urllib2.urlopen(url) content = response.read() return content # get problem name def getProblemName(page): pNameMatch = pNamePattern.search(page) return pNameMatch.group(1) if pNameMatch!=None else None # get problem Description def getProblemDesc(page): pDescMatch = pDescPattern.search(page) return pDescMatch.group(1) if pDescMatch!=None else None # get input content def getInputContent(page): pInput = pPrePattern.findall(page)[0] return pInput # get output content def getOutputContent(page): pOutput = pPrePattern.findall(page)[1] return pOutput # get sample input def getSampleInput(page): pSampleInput = pPrePattern.findall(page)[2] return pSampleInput # get sample output def getSampleOutput(page): pSampleOutput = pPrePattern.findall(page)[3] return pSampleOutput # class for xml class XmlGenerator: def __init__(self, xmlName): self.doc = minidom.Document() self.xmlName = xmlName def createNode(self, nodeName): return self.doc.createElement(nodeName) def addNode(self, node, prevNode = None): curNode = node if prevNode is not None: prevNode.appendChild(curNode) else: self.doc.appendChild(curNode) return curNode def setNodeAttr(self, node, attName, value): curNode = node curNoee.setAttribute(attName, value) def setNodeValue(self, curNode, value): nodeData = self.doc.createTextNode(value) curNode.appendChild(nodeData) def genXml(self): f = open(self.xmlName, "w") f.write(self.doc.toprettyxml(indent="\t", newl="\n", encoding="utf8")) f.close() path = "D://test.xml" xmlGen = XmlGenerator(path) rootNode = xmlGen.createNode("root") xmlGen.addNode(node=rootNode) count = 0 while count < 6: pSetUrl = genProblemSetUrl(count+1) print pSetUrl html = loadPage(pSetUrl) for pageUrl in getAllProblemUrl(html): pageUrl = "http://xxx.edu.cn:8080/JudgeOnline/" + pageUrl print pageUrl pPage = loadPage(pageUrl) pPage_gbk = unicode(pPage, "gbk") pName = getProblemName(pPage_gbk) pDesc = getProblemDesc(pPage_gbk) pInput = getInputContent(pPage_gbk) pOutput = getOutputContent(pPage_gbk) pSampleInput = getSampleInput(pPage_gbk) pSampleOutput = getSampleOutput(pPage_gbk) problemNode = xmlGen.createNode("problem") pUrlNode = xmlGen.createNode("url") xmlGen.setNodeValue(pUrlNode, pageUrl) xmlGen.addNode(pUrlNode, problemNode) pNameNode = xmlGen.createNode("name") xmlGen.setNodeValue(pNameNode, pName) xmlGen.addNode(pNameNode, problemNode) pDescNode = xmlGen.createNode("desc") xmlGen.setNodeValue(pDescNode, pDesc) xmlGen.addNode(pDescNode, problemNode) pInputNode = xmlGen.createNode("input") xmlGen.setNodeValue(pInputNode, pInput) xmlGen.addNode(pInputNode, problemNode) pOutputNode = xmlGen.createNode("output") xmlGen.setNodeValue(pOutputNode, pOutput) xmlGen.addNode(pOutputNode, problemNode) pSInputNode = xmlGen.createNode("sampleInput") xmlGen.setNodeValue(pSInputNode, pSampleInput) xmlGen.addNode(pSInputNode, problemNode) pSOutputNode = xmlGen.createNode("sampleOutput") xmlGen.setNodeValue(pSOutputNode, pSampleOutput) xmlGen.addNode(pSOutputNode, problemNode) xmlGen.addNode(problemNode, rootNode) xmlGen.genXml() count += 1 print "finish"
相关推荐
python读程序写结果题库.docx
使用Python爬去网页中的C语言题库,并解决Python爬去乱码的问题
python计算机二级题库(附带答案)python计算机二级题库(附带答案)python计算机二级题库(附带答案)python计算机二级题库(附带答案)python计算机二级题库(附带答案)python计算机二级题库(附带答案)python...
《Python程序设计》练习题库
81个Python爬虫源代码,内容包含新闻、视频、中介、招聘、图片资源等网站的爬虫资源
资源名称:python把Word题库转成Excle题库.py 内容概要: python把Word题库转成Excle题库.py是一个Python脚本,旨在将Word格式的题库转换为Excel格式的题库。该脚本使用Python的docx和openpyxl库来读取和写入Word...
C#调用C++,C++调用Python并传参,包括C++向python传递图片数据,C++向python传递组数据,C++向python传递字符串数据,C++向python传递结构体数据,C++初始化python类数据并调用方法,实际案例,能直接运行,需要安装...
使用Windows共享内存技术 实现Python与C_C++程序间的数据传递.pdf
python学习资料共享。
Python2理论题库题库(363道) Python2理论题库题库(363道)全文共64页,当前为第1页。Python2理论题库题库(363道)全文共64页,当前为第1页。Python2理论题库 Python2理论题库题库(363道)全文共64页,当前为第1页。 ...
讲诉python爬虫的20个案例 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
Python期末试题题库完整
Python数据分析与应用题库 Python数据分析与应用题库 下列nltk模块中,可以对句子实现分词操作的是()。 [单选题] * A、nltk.corpus B、nltk.tokenize(正确答案) C、nltk.stem D、nltk.tag 答案解析:暂无解析 下列...
基于python的蓝桥杯练习系统题库 基于python的蓝桥杯练习系统题库 基于python的蓝桥杯练习系统题库 基于python的蓝桥杯练习系统题库 基于python的蓝桥杯练习系统题库 基于python的蓝桥杯练习系统题库 基于...
python爬去网页图片小程序+源码(福利)
c语言 c++ java .net 前端 python c# 微信小程序 软测 Linux嵌入式开发+驱动开发 PHP 面试就业指导资料 最全资源
WORD 格式可编辑 《Python 程序设计》题库 一、填空题 第一章 基础知识 1、 Python 安装扩展库常用的是 _______工具。( pip) 2、 Python 标准库 math 中用来计算平方根的函数是 __________。(sqrt) 3、 Python ...
这个例子示范了如何从C++中导出一个类到Python。轻松的扩展Python模块!
Python中调用C++dll例子,使用python中的ctypes。