<p>试试<a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">Beautiful Soup</a>。这是一个非常容易使用的用于解析HTML文档和片段的库。在</p>
<pre><code>import urllib2
from BeautifulSoup import BeautifulSoup
for opp in range(opp1,oppn+1):
oppurl = (www.myhomepage.com)
response = urllib2.urlopen(oppurl)
html = response.read()
soup = BeautifulSoup(html)
if soup.head.title == "Record doesn't exist":
continue
else:
oppfilename = 'work/opptest'+str(opp)+'.htm'
oppfile = open(oppfilename,'w')
opp.write(opphtml)
print 'Wrote ',oppfile
votefile.close()
</code></pre>
<p>编辑</p>
<p>如果不能选择靓汤,我个人会使用正则表达式。然而,我拒绝在公共场合承认这一点,因为我不会让别人知道我会屈尊于简单的解决方案。让我们看看“电池包”里有什么。在</p>
<p><a href="http://docs.python.org/library/htmlparser.html" rel="nofollow">^{<cd1>}</a>看起来很有前途,让我们看看能否按我们的意愿来做。在</p>
^{pr2}$
<p>那真是太痛苦了。几乎和Java一样冗长。(开玩笑)</p>
<p>还有什么?有一个<a href="http://docs.python.org/library/xml.dom.minidom.html" rel="nofollow">^{<cd2>}</a>一个“轻量级DOM实现”。我喜欢“轻量级”的声音,意思是我们可以用一行代码来完成,对吗?在</p>
<pre><code>import xml.dom.minidom
html = '<html><head><title>Test</title></head><body><h1>Parse me!</h1></body></html>'
title = ''.join(node.data for node in xml.dom.minidom.parseString(html).getElementsByTagName("title")[0].childNodes if node.nodeType == node.TEXT_NODE)
>>> print title
Test
</code></pre>
<h2>我们只有一条线!在</h2>
<hr/>
<p>所以我听说这些正则表达式在从HTML中提取文本时非常有效。我想你应该用那些。在</p>