如何用Python提取web页面的某些部分

2024-04-29 05:41:46 发布

您现在位置:Python中文网/ 问答频道 /正文

目标网页: http://www.immi.gov.au/skilled/general-skilled-migration/estimated-allocation-times.htm

我要提取的部分:

  <tr>
  <td>Skilled &ndash; Independent (Residence) subclass 885<br />online</td>
  <td>N/A</td>
  <td>N/A</td>
  <td>N/A</td>
  <td>15 May 2011</td>
  <td>N/A</td>
  </tr>

代码通过在线搜索关键字“子类885
”找到此部分后,应打印第5个标记(即“2011年5月15日”)内的日期,如上图所示。

这只是我自己的一个监视器,用来监视我的移民申请进度。


Tags: http网页目标wwwmigrationtrtdgeneral
3条回答

"Beau--ootiful Soo--oop!

Beau--ootiful Soo--oop!

Soo--oop of the e--e--evening,

Beautiful, beauti--FUL SOUP!"

——刘易斯·卡罗尔,Alice's Adventures in Wonderland

我想这正是他想要的!

素甲鱼可能会这样做:

>>> from BeautifulSoup import BeautifulSoup
>>> import urllib2
>>> url = 'http://www.immi.gov.au/skilled/general-skilled-migration/estimated-allocation-times.htm'
>>> page = urllib2.urlopen(url)
>>> soup = BeautifulSoup(page)
>>> for row in soup.html.body.findAll('tr'):
...     data = row.findAll('td')
...     if data and 'subclass 885online' in data[0].text:
...         print data[4].text
... 
15 May 2011

但我不确定这会有什么帮助,因为那个日期已经过去了!

祝你申请顺利!

有一个图书馆叫美丽汤,它做你要求的工作。http://www.crummy.com/software/BeautifulSoup/

您可能希望以此为起点:

Python 2.6.7 (r267:88850, Jun 13 2011, 22:03:32) 
[GCC 4.6.1 20110608 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2, re
>>> from BeautifulSoup import BeautifulSoup
>>> urllib2.urlopen('http://www.immi.gov.au/skilled/general-skilled-migration/estimated-allocation-times.htm')
<addinfourl at 139158380 whose fp = <socket._fileobject object at 0x84aa2ac>>
>>> html = _.read()
>>> soup = BeautifulSoup(html)
>>> soup.find(text = re.compile('\\bsubclass 885\\b')).parent.parent.find('td', text = re.compile(' [0-9]{4}$'))
u'15 May 2011'

相关问题 更多 >