Python学习—网络爬虫之OJ

最近在学Python的正则表达式,简直就是大神器,爬虫也蛮有意思的。


'''
Created on 2013-9-19
for poj
@author: yzp
'''
import urllib
import re
import os
import sys
def SubTitle(web):
    key = "<title>.+</title>"
    title = re.findall(key,web)
    return title
def get_problem(i):
    
    filename = "problem"+repr(i)
    fl = file(filename,'w')
    url = "http://poj.org/problem?id="+repr(i)
    f = urllib.urlopen(url)
    
    html = f.read();
    title = SubTitle(html)
    problem = re.compile(u'Description.*Source',re.DOTALL)
    style = problem.search(html)
    if style:
        html = style.group(0)
        html = title[0]+html
        tmp = re.sub('<[^>]*>','',html)
        tmp = tmp.replace('Description','Description\n')
        tmp = tmp.replace('Sample Input','\nSample Input\n')
        tmp = tmp.replace('Sample Output','\nSample Output\n')
        tmp = tmp.replace('Source','')
        tmp = re.sub(';','',tmp)   
        fl.write(tmp)
def main():
    
    for i in range(1001,4054):
        get_problem(i)
main()

'''
Created on 2013-9-19
for hdoj
@author: yzp
'''
import urllib
#print(urllib.urlopen('http://news.qq.com/a/20130919/000593.htm').read())
import re
import os
import sys

def get_problem(i):
    filename = "problem"+repr(i)
    fl = file(filename,'w')
    url = "http://acm.hdu.edu.cn/showproblem.php?pid="+repr(i)
    f = urllib.urlopen(url)
    html = f.read();
    problem = re.compile(u'Problem Description.*Author',re.DOTALL)
    style = problem.search(html)
    if style:
        html = style.group(0)
        tmp = re.sub('<[^>]*>','',html)
        tmp = tmp.replace(' ','')
        tmp = tmp.replace('Author','')
        tmp = tmp.replace('Problem Description','Problem Description\n')
        tmp = tmp.replace('Sample Input','\nSample Input\n')
        tmp = tmp.replace('Sample Output','\nSample Output\n')
        tmp = re.sub(';','',tmp)   
        fl.write(tmp)
    #print(tmp)
    
def main():
    
    for i in range(1000,4758):
        get_problem(i)
main()


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值