【发布时间】:2016-01-10 11:12:31
【问题描述】:
我的数据存储在如下列表中:
date_name = [<a href="/president/washington/speeches/speech-3455">Proclamation of Neutrality (April 22, 1793)</a>,
<a class="transcript" href="/president/washington/speeches/speech-3455">Transcript</a>,
<a href="/president/washington/speeches/speech-3456">Fifth Annual Message to Congress (December 3, 1793)</a>,
<a class="transcript" href="/president/washington/speeches/speech-3456">Transcript</a>,
<a href="/president/washington/speeches/speech-3721">Proclamation against Opposition to Execution of Laws and Excise Duties in Western Pennsylvania (August 7, 1794)</a>]
这些不是date_name 中的str 元素。我正在尝试获取Proclamation of Neutrality (April 22, 1793)、Fifth Annual Message to Congress (December 3, 1793) 和Proclamation against Opposition to Execution of Laws and Excise Duties in Western Pennsylvania (August 7, 1794),以便我可以从每个演讲中获取日期。我想为 900 多个演讲做这个。这是我一直在尝试的代码,因为它适用于我在另一个列表理解场景中遇到的类似问题:
url = 'http://www.millercenter.org/president/speeches'
connection = urllib2.urlopen(url)
html = connection.read()
date_soup = BeautifulSoup(html)
date_name = date_soup.find_all('a')
del date_name[:203] # delete extraneous html before first link (for obama 4453)
# do something with the following list comprehensions
dater = [tag.get('<a href=') for tag in date_name if tag.get('<a href=') is not None]
# remove all items in list that don't contain '<a href=', as this string is unique
# to the elements in date_name that I want
speeches_dates = [_ for _ in dater if re.search('<a href=',_)]
但是,我在dater 变量过程中得到一个空集,所以我无法继续构造speeches_dates。
【问题讨论】:
标签: python list python-2.7 web-scraping beautifulsoup