[英]How to get text after <br/> tag in div?
我写了一个脚本,可以在一秒钟内从互联网上下载歌词。 div 中的歌词文本,行尾带有<br>
。 当我试图通过 BeautifulSoup 获取文本时。 我收到此错误:
回溯(最近一次通话最后):
文件“/home/rohit/Desktop/lyrics_finder.py”,第 27 行,在
app = EpicLyricFinderApp()
文件“/home/rohit/Desktop/lyrics_finder.py”,第 10 行,在init
self.app()
应用程序中的文件“/home/rohit/Desktop/lyrics_finder.py”,第 21 行
对于我在 container.get_text() 中:
AttributeError: 'list' object 没有属性 '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/>
以获取文本。
容器是列表 object 而不是元素。这就是您收到此错误的原因。
AttributeError: 'list' object 没有属性 'get_text'
您需要在迭代中获取文本。
for i in container:
print(i.get_text())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.