在beautifulsoup Python中提取锚标签之间的文本?

时间:2014-05-19 04:16:44

标签: python beautifulsoup

我正在尝试提取此fandango页面上列出的电影名称。

names_tag = soup.findAll('a', {'class': 'dark showtimes-movie-title'})

这是名称隐藏的锚类。问题是,当我运行代码时,输​​出是:

<a class="dark showtimes-movie-title" href="http://www.fandango.com/godzilla3d_170083/movieoverview">Godzilla 3D</a>

当我想要在哥斯拉3D中。如何成功解析此数据?

#anchor element containing the names of each movie
names_tag = soup.findAll('a', {'class': 'dark showtimes-movie-title'})
names_tag = str(names_tag)

movie_name = names_tag.split(',')

for each_line in movie_name:
    movie_names.append(each_line)

i = 0
while (i < len(movie_names)):

    print 'The length of %s is %s' %(movie_names[i], movie_times[i])

    i+=1

1 个答案:

答案 0 :(得分:0)

使用text属性:

names_tag = soup.findAll('a', {'class': 'dark showtimes-movie-title'})
names = [name_tag.text for name_tag in names_tag]