Python 如何在<;之后获取文本;br/>;在div中标记?
我写了一个脚本,在一秒钟内从网上下载歌词。以div格式显示的歌词文本,行末带有Python 如何在<;之后获取文本;br/>;在div中标记?,python,python-3.x,beautifulsoup,formatting,Python,Python 3.x,Beautifulsoup,Formatting,我写了一个脚本,在一秒钟内从网上下载歌词。以div格式显示的歌词文本,行末带有。当我试图通过BeautifulSoup获取文本时。我得到了这个错误: 回溯(最近一次呼叫最后一次): 文件“/home/rohit/Desktop/lystis_finder.py”,第27行,在 app=epicryricfinderapp() 文件“/home/rohit/Desktop/lystis_finder.py”,第10行,在init self.app() 文件“/home/rohit/Desktop/
。当我试图通过BeautifulSoup获取文本时。我得到了这个错误:
回溯(最近一次呼叫最后一次):文件“/home/rohit/Desktop/lystis_finder.py”,第27行,在
app=epicryricfinderapp()
文件“/home/rohit/Desktop/lystis_finder.py”,第10行,在init
self.app()
文件“/home/rohit/Desktop/lystis_finder.py”,第21行,在应用程序中
对于容器中的i.get_text():
AttributeError:“列表”对象没有属性“获取文本” 我会尝试许多不同的方法,但我会找到这个问题的解决方案 我的代码:
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()
我期望:
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中跳过
以获取文本。容器是列表对象而不是元素。这就是为什么会出现此错误
AttributeError:“列表”对象没有属性“获取文本”
您需要在迭代中获取文本
for i in container:
print(i.get_text())
谢谢。我忘了!