【发布时间】:2019-11-19 11:43:11
【问题描述】:
我写了一个脚本,可以在一秒钟内从互联网上下载歌词。 div 中的歌词文本,行尾带有<br>。当我试图通过 BeautifulSoup 获取文本时。我收到此错误:
回溯(最近一次通话最后一次):
文件“/home/rohit/Desktop/lyrics_finder.py”,第 27 行,在
app = EpicLyricFinderApp()
init
中的文件“/home/rohit/Desktop/lyrics_finder.py”,第 10 行 self.app()
应用程序中的文件“/home/rohit/Desktop/lyrics_finder.py”,第 21 行
对于我在 container.get_text():
AttributeError: 'list' 对象没有属性 'get_text'
我会尝试很多不同的方法,但我会得到这个问题的解决方案
我的代码:
from bs4 import BeautifulSoup
import os, requests, re
class EpicLyricFinderApp:
def __init__(self):
self.text = '+'.join(input('Enter song name and also include singer: ').split(' '))
self.url = "https://search.azlyrics.com/search.php?q=let+me+love+you{}".format(self.text)
self.lyrics = ''
self.app()
def app(self):
req = requests.get(self.url).content
soup = BeautifulSoup(req, 'html.parser')
links = [link['href'] for link in soup.select('.text-left a')]
# Open another url
req1 = requests.get(links[0]).content
soup1 = BeautifulSoup(req1, 'html.parser')
container = soup1.select('body > div.container.main-page > div > div.col-xs-12.col-lg-8.text-center > div:nth-child(10)')
for i in container.get_text():
print(i)
if __name__ == '__main__':
app = EpicLyricFinderApp()
我预计:
如何在 Beautifulsoup 中跳过 <br/> 以获取文本。
【问题讨论】:
标签: python python-3.x beautifulsoup formatting