从HTML Doc中提取的文本到单词列表

时间:2017-05-04 11:34:45

标签: python html bs4

使用BeautifulSoup,我从所述页面的html doc中提取了网页上的评论。使用此代码,我可以打印出评论:



import urllib2
from bs4 import BeautifulSoup

url = "http://songmeanings.com/songs/view/3530822107858560012/"
response = urllib2.build_opener(urllib2.HTTPCookieProcessor).open(url)
html_doc = response.read()
soup = BeautifulSoup(html_doc, 'html.parser')

def loop_until(text,first_elem):
  try: 
    text += first_elem.string
    if first_elem.next == first_elem.find_next('div'):
        return text
    else:
        return loop_until(text,first_elem.next.next)
  except TypeError:
      pass 
      
wordList = []

for strong_tag in soup.find_all('strong'):
    next_elem = strong_tag.next_sibling
    print loop_until("", next_elem)




现在我需要从选择中取出所有单词并将它们附加到wordList,我该怎么做?

1 个答案:

答案 0 :(得分:1)

更改最后一行,使用append

wordList.append(loop_until("", next_elem))