基本Python/Beauty Soup解析

基本Python/Beauty Soup解析,python,html,beautifulsoup,Python,Html,Beautifulsoup,说我用过 date = r.find('abbr') 得到 <abbr class="dtstart" title="2012-11-16T00:00:00-05:00">November 16, 2012</abbr> 我明白了 我做错了什么 回答:这是我最后的学习代码: soup = BeautifulSoup(page) calendar = soup.find('table',{"class" : "vcalendar ical"}) dates = cal

说我用过

date = r.find('abbr')
得到

<abbr class="dtstart" title="2012-11-16T00:00:00-05:00">November 16, 2012</abbr>
我明白了

我做错了什么

回答:这是我最后的学习代码:

soup = BeautifulSoup(page)
calendar = soup.find('table',{"class" : "vcalendar ical"})

dates = calendar.findAll('abbr', {"class" : "dtstart"})
events = calendar.findAll('strong')

for i in range(1,len(dates)-1):
    print dates[i].string + ': ' + events[i].string
November 16, 2012
错误消息表示
date
None
。您没有显示足够的代码来说明原因。事实上,以最直接的方式使用您发布的代码应该是可行的:

import BeautifulSoup

content='<abbr class="dtstart" title="2012-11-16T00:00:00-05:00">November 16, 2012</abbr>'
r=BeautifulSoup.BeautifulSoup(content)
date=r.find('abbr')
print(date.string)
# November 16, 2012
导入美化组
content='2012年11月16日'
r=美化组。美化组(内容)
日期=r.find('abbr')
打印(date.string)
#2012年11月16日
soup.find('abbr')。string
应该可以正常工作。
日期一定有问题

from BeautifulSoup import BeautifulSoup

doc = '<abbr class="dtstart" title="2012-11-16T00:00:00-05:00">November 16, 2012</abbr>'

soup = BeautifulSoup(doc)

for abbr in soup.findAll('abbr'):
    print abbr.string
从美化组导入美化组
博士

text是一个参数,用于搜索NavigableString对象 而不是标签

或者您正在查找文本节点,或者您正在查找标记。文本节点不能有标记名


也许你想要
'.join([el.string代表r.findAll('strong'))中的el)

在搜索元素时不要使用
text=True
。找到所需的元素,然后使用这些元素的
.string
属性。
from BeautifulSoup import BeautifulSoup

doc = '<abbr class="dtstart" title="2012-11-16T00:00:00-05:00">November 16, 2012</abbr>'

soup = BeautifulSoup(doc)

for abbr in soup.findAll('abbr'):
    print abbr.string
November 16, 2012