<p>是否需要使用字典取决于您自己,但是如果您选择使用字典,则最好为每个列表项单独使用一个字典,而不是为所有条目使用一个字典。在</p>
<p>我建议你把所有的条目都存储在一个列表中。下面的代码显示了两个建议,要么使用<code>tuple</code>来存储每个项目的不同信息位,要么使用字典。在</p>
<p>如果您只想显示信息或将其写入文件,<code>tuple</code>解决方案会更快。在</p>
<pre><code># Two possible ways of storing your data: a list of tuples, or a list of dictionaries
entries_tuples = []
entries_dictionary = []
soup = BeautifulSoup(text)
div = soup.find('div', {'id': 'object-a'})
ul = div.find('ul', {'id': 'object-a-1'})
for li in ul.findAll('li'):
title = li.find('a', {'class': 'title'})
url_href = title.get('href')
person = title.text
url_word = li.find('p', {'class': 'url word'}).text
emails = re.findall(r'\s+(\S+@\S+)(?:\s+|\Z)', li.findAll('p')[1].text, re.M) # allow for multiple emails
entries_tuples.append((url_href, person, url_word, emails))
entries_dictionary.append({'url_href' : url_href, 'person' : person, 'url_word' : url_word, 'emails' : emails})
for url_href, person, url_word, emails in entries_tuples:
print '{:25} {:10} {:25} {}'.format(url_href, person, url_word, emails)
print
for entry in entries_dictionary:
print '{:25} {:10} {:25} {}'.format(entry['url_href'], entry['person'], entry['url_word'], entry['emails'])
</code></pre>
<p>对于示例HTML,将显示以下内容:</p>
^{pr2}$
<p>注意,从文本中提取电子邮件地址本身就是一个完整的问题。上面的解决方案可以很容易地匹配那些实际上不是格式良好的电子邮件地址的条目,但在这里就足够了。在</p>