页面是这样的:http://poj.org/problem?id=3334

要从这样的页面里面提取题目标题,时间限制,内存限制,题目描述,输入,输出,示例输入,示例输出,提示,来源等信息,获取必要的题目中的图片。

#!/usr/bin/env python
#coding=utf-8
from BeautifulSoup import BeautifulSoup
import urllib
import re
def getpojhtml(pid):
    url = "http://poj.org/problem?id="+str(pid)
    html = urllib.urlopen(url)    
    soup = BeautifulSoup(html)
    title = soup.title.string[7:]
    time_limit = soup.findAll(text = re.compile("Time Limit"))[0].next
    mem_limit = soup.findAll(text = re.compile("Memory Limit"))[0].next
    description = soup.findAll(text = re.compile("Description"))[0].next.contents
    input = soup.findAll(text = re.compile("Input"))[0].next.contents
    output = soup.findAll(text = re.compile("Output"))[0].next.contents
    sim_input = soup.findAll(text = re.compile("Sample Input"))[0].next.contents
    sim_output = soup.findAll(text = re.compile("Sample Output"))[0].next.contents
    try:
        hint = soup.findAll(text = re.compile("Hint"))[0].next.contents
    except:
        hint = []
    try:
        source = soup.findAll(text = re.compile("Source"))[0].next.contents
    except :
        source = []
    pattern = re.compile('images/\d{4}[.\w]*')
    pic =  pattern.findall(html)
    pic_url=[]
    for item in pic:
        pic_url.append( 'http://poj.org/'+str(item))

return title,time_limit,mem_limit,description,input,output,sim_input,sim_output,hint,source,pic_url

if __name__=='__main__':
    ret = getpojhtml(3344)
    for item in ret:

print item

实现方案

首先用urllib模块获取整个页面,然后用beautifulsoup来解析,由于个别页面没有hint或者source,所以用try避免出错退出

图片可以选择用beautifulsoup来解析,但是我还是选择了用正则表达式来解析,因为用正则表达式可以准确地定位到题目描述中的图片,而beautifulsoup把整个页面中的所有图片都找出来了,有些并不是我需要的。

运行结果

Chessboard Dance
 2000MS
 65536K
[<div><p>Another boring Friday afternoon, Betty the Beetle thinks how to amuse herself. She goes out of her hiding place to take a walk around the living room in Bennett's house. Mr. and Mrs. Bennett are out to the theatre and there is a chessboard on the table! &quot;The best time to practice my chessboard dance,&quot; Betty thinks! She gets so excited that she does not note that there are some pieces left on the board and starts the practice session! She has a script showing her how to move on the chessboard. The script is a sequence like the following example:</p><p><center><img src="data:images/3344_1.GIF" /></center></p><p>At each instant of time Betty, stands on a square of the chessboard, facing one of the four directions (up, down, left, right) when the board is viewed from the above. Performing a &quot;move <i>n</i>&quot; instruction, she moves <i>n</i> squares forward in her current direction. If moving <i>n</i> squares goes outside the board, she stays at the last square on the board and does not go out. There are three types of turns: turn right, turn left, and turn back, which change the direction of Betty. Note that turning does not change the position of Betty.</p><p>If Betty faces a chess piece when moving, she pushes that piece, together with all other pieces behind (a tough beetle she is!). This may cause some pieces fall of the edge of the chessboard, but she doesn't care! For example, in the following figure, the left board shows the initial state and the right board shows the state after performing the script in the above example. Upper-case and lower-case letters indicate the white and black pieces respectively. The arrow shows the position of Betty along with her direction. Note that during the first move, the black king (r) falls off the right edge of the board!</p><p><center><img src="data:images/3344_2.GIF" /></center></p><p>You are to write a program that reads the initial state of the board as well as the practice dance script, and writes the final state of the board after the practice.</p></div>]
[<div><p>There are multiple test cases in the input. Each test case has two parts: the initial state of the board and the script. The board comes in eight lines of eight characters. The letters r, d, t, a, c, p indicate black pieces, R, D, T, A, C, P indicate the white pieces and the period (dot) character indicates an empty square. The square from which Betty starts dancing is specified by one of the four characters &lt;, &gt;, ^, and v which also indicates her initial direction (left, right, up, and down respectively). Note that the input is not necessarily a valid chess game status.</p><p>The script comes immediately after the board. It consists of several lines (between 0 and 1000). In each line, there is one instruction in one of the following formats (<i>n</i> is a non-negative integer number):</p><p>move <i>n</i><br />turn left<br />turn right<br />turn back</p><p>At the end of each test case, there is a line containing a single # character. The last line of the input contains two dash characters.</p></div>]
[<p>The output for each test case should show the state of the board in the same format as the input. Write an empty line in the output after each board.</p>]
[u'.....c..\r\n.p..A..t\r\nD..>T.Pr\r\n....aP.P\r\np.d.C...\r\n.....p.R\r\n........\r\n........\r\nmove 2\r\nturn right\r\nmove 3\r\nturn left\r\nturn left\r\nmove 1\r\n#\r\n--\r\n']
[u'.....c..\r\n.p..A..t\r\nD.....TP\r\n....a..P\r\np.d.C^..\r\n.......R\r\n.....P..\r\n.....p..\r\n']
[]
[<a href="searchproblem?field=source&amp;key=Tehran+2006">Tehran 2006</a>]

['http://poj.org/images/3344_1.GIF', 'http://poj.org/images/3344_2.GIF']


博主ma6174对本博客文章(除转载的)享有版权,未经许可不得用于商业用途。转载请注明出处http://www.cnblogs.com/ma6174/

对文章有啥看法或建议,可以评论或发电子邮件到ma6174@163.com


本文转自ma6174博客园博客,原文链接:http://www.cnblogs.com/ma6174/archive/2012/08/04/2623159.html,如需转载请自行联系原作者

解析poj页面获取题目相关推荐

  1. Java解析html页面,获取想要的元素

    背景:通过接口访问数据,获取的内容是个标准的html格式,使用jsoup的方式获取页面元素值 先推荐比较好的博客:http://www.open-open.com/jsoup/. 单个案例比较不错 h ...

  2. Android开发系列十:使用Jsoup解析HTML页面

    在写Android程序时,有时需要解析HTML页面,特别是那类通过爬网站抓取数据的应用,比如:天气预报等应用.如果是桌面应用可以使用htmlparser这个强大的工具,但是在Android平台上使用会 ...

  3. python教程app 小米应用商店_Python爬虫过程解析之多线程获取小米应用商店数据...

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理. 以下文章来源于IT共享之家 ,作者IT共享者 前言 小米应用商店给用户发现最好的安卓应用和游戏,安 ...

  4. 爬虫数据存储到数据库/增量爬虫+多级页面获取=====安居客信息爬取

    文章目录 前言 一.增量爬虫是什么? 二.python数据存储到数据库 三.多级页面的跳转获取 四:遇到的问题以及解决 五:代码 总结 前言:这次的爬取内容是安居客网页里面的信息,首先是我爬取的页面是 ...

  5. 纯前端JS实现文件上传解析渲染页面

    AI真的能代替前端吗? 回答:不会完全代替 能用吗?复制到项目中只会报错 爆红 --他完全不能理解你需要什么 JavaScript(简称JS)是一种轻量级的脚本语言,主要用于在Web页面上添加交互行为 ...

  6. html的子页面获取自己url,如何从html页面获取url参数并将其显示在textarea中?

    我用下面的在我的HTML页面的JavaScript功能,我想没有textarea的任何连接操作正常显示参数....如何从html页面获取url参数并将其显示在textarea中? function g ...

  7. bos 获取数据库连接_java解析数据接口获取json对象

    最近小编在做项目的时候,需要解析一个url以获取其数据,开始我为简便,使用了Postman这个工具来解析,也获取了json对象. 但后也发现,它没法直接连接数据库,也就是说这些数据不能直接存入数据库, ...

  8. iframe父页面获取iframe子页面的元素 与 iframe子页面获取父页面元素

    一.在iframe子页面获取父页面元素代码如下:$('#objld', parent.document); 二.在父页面获取iframe子页面的元素代码如下:$("#objid", ...

  9. 如何获取html页面上的按钮列表,如何从一个html页面获取单选按钮的值到另一个?...

    我尝试获取位于第一个html页面上的底部粘贴html代码的值.我有另一个html页面,应该用"onclick"按钮生成.更准确地说,代码应该在下一页中创建按钮.如何从一个html页 ...

最新文章

  1. 更简单的调试Release版本Optimize code的.NET程序集
  2. 简单实现x的n次方pta_学会这四招,原来平均值计算也可以这么简单
  3. 使用junit测试用例
  4. [SDOI2014]旅行
  5. 漫谈 Linux,Windows 和 Mac
  6. 使用注解匹配Spring Aop切点表达式
  7. 程序员,你会说话吗?
  8. node 学习 ——模块导出
  9. 图新地球点云大师:点云数据三维可视化管理及等高线提取
  10. 戴尔(DELL)成就Vostro15-7580 15.6英寸八代混合独显便携商务笔记本 5699元
  11. iOS----------Apple id如何关闭双重认证?
  12. 【OpenCV C++】照片修改像素(尺寸大小)
  13. CentOS 8 Error: Failed to download metadata for repo ‘appstream‘: Cannot prepare internal mirrorlist
  14. Linksys玩多了,来看看真正的Cisco~技术帖
  15. 移动端实时视频直播技术实践:如何做到实时秒开、流畅不卡
  16. 第二型曲面积分的定义
  17. MATLAB2021b详细安装教程
  18. C++Primer笔记——拷贝控制
  19. MacPro终端出现bash: touch: command not found
  20. 常指针和指向常量的指针

热门文章

  1. [转]如何设置win7一直以管理员身份运行
  2. 世界顶尖品牌的经典广告词欣赏
  3. CSS 图片上下部与边框有间隙
  4. Postgre合并多行数据为一行
  5. 怎么查询表中BLOB字段的大小
  6. 关于idea的git账号与电脑的git账号不一致的问题。已解决!
  7. python glob函数_python glob 模块 map函数
  8. JAVA 代码交互率低的原因分析,深入剖析Java编程中的中文问题及建议最优解决方法...
  9. 在php中创建三个表格,创建新工作表PHPExcel
  10. 计算机考研8,计算机考研每日一练:第八天