如何在Python中提取网页的某些部分
目标网页: 我想摘录的部分:如何在Python中提取网页的某些部分,python,html,string,Python,Html,String,目标网页: 我想摘录的部分: <tr> <td>Skilled – Independent (Residence) subclass 885<br />online</td> <td>N/A</td> <td>N/A</td> <td>N/A</td> <td>15 May 2011</td> <t
<tr>
<td>Skilled – 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>
熟练&ndash;独立(居住)子类885
在线
不适用
不适用
不适用
2011年5月15日
不适用
一旦代码通过搜索关键字“子类885online”找到此部分,它应打印第5个标记内的日期,即“2011年5月15日”,如上所示
这只是我自己的一个监视器,用来监视我移民申请的进展。有一个叫做Beautiful Soup的图书馆,它完成了你要求的工作 您可能希望以此为起点:
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'
Python 2.6.7(r267:88850,2011年6月13日,22:03:32)
linux2上的[GCC 4.6.1 20110608(预发布)]
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>导入urllib2,re
>>>从BeautifulSoup导入BeautifulSoup
>>>urllib2.urlopen('http://www.immi.gov.au/skilled/general-skilled-migration/estimated-allocation-times.htm')
>>>html=uux.read()
>>>soup=BeautifulSoup(html)
>>>soup.find(text=re.compile('\\bsubclass 885\\b')).parent.parent.find('td',text=re.compile('[0-9]{4}$'))
u'2011年5月15日'
"
哦,哦,哦,哦,哦,晚上
"
--刘易斯·卡罗尔
我想这正是他的想法
素甲鱼可能会这样做:
>>> 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
但我不确定这是否会有帮助,因为那个日期已经过去了
祝你申请成功 这里是初学者,但从我所读到的内容来看,lxml是屏幕抓取的首选模块。这仅仅是一个偏好的问题,还是提供了任何显著的优势?还有,你的比例是30:1。嗨,皮特,我还没有用过lxml,我得研究一下。你是对的,我走了,但方向是对的:^)啊,我以为你想走另一个方向。问问题有什么不对?D:提问没什么错,但是当我做了所有必要的“尽职调查”来恰当地提问时,我通常已经找到了答案。