简体   繁体   English

之后如何获取文本<br> div中的标签?

[英]How to get text after <br/> tag in div?

I wrote a script to download lyrics from the internet in just a second.我写了一个脚本,可以在一秒钟内从互联网上下载歌词。 The text of lyrics in div with <br> at the end of line. div 中的歌词文本,行尾带有<br> when I trying to get text through BeautifulSoup.当我试图通过 BeautifulSoup 获取文本时。 I'm getting this error:我收到此错误:

Traceback (most recent call last):回溯(最近一次通话最后):
File "/home/rohit/Desktop/lyrics_finder.py", line 27, in文件“/home/rohit/Desktop/lyrics_finder.py”,第 27 行,在
app = EpicLyricFinderApp() app = EpicLyricFinderApp()
File "/home/rohit/Desktop/lyrics_finder.py", line 10, in init文件“/home/rohit/Desktop/lyrics_finder.py”,第 10 行,在init
self.app() self.app()
File "/home/rohit/Desktop/lyrics_finder.py", line 21, in app应用程序中的文件“/home/rohit/Desktop/lyrics_finder.py”,第 21 行
for i in container.get_text():对于我在 container.get_text() 中:
AttributeError: 'list' object has no attribute 'get_text' AttributeError: 'list' object 没有属性 'get_text'

I'd try many differnt way but I'd get solution of this problem我会尝试很多不同的方式,但我会得到这个问题的解决方案

My Code:我的代码:

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()

I expected:我期望:

How can I skip <br/> in Beautifulsoup in order to get text.如何在 Beautifulsoup 中跳过<br/>以获取文本。

The container is list object not element.That is why you are getting this error.容器是列表 object 而不是元素。这就是您收到此错误的原因。

AttributeError: 'list' object has no attribute 'get_text' AttributeError: 'list' object 没有属性 'get_text'

You need to get the text in iteration.您需要在迭代中获取文本。

for i in container:
    print(i.get_text())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM