如何使用Beautifulsoup将不同的文本集合存储在单个变量中

2024-04-25 06:06:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我在MacOS上用Python3构建一个简单的程序,将一个艺术家的所有歌词都放到一个变量中。虽然我能够正确地迭代不同的URL(每个URL都是来自这个艺术家的一首歌)并得到我想要的输出,但是我很难将所有不同的歌曲存储在一个变量中。在

我尝试过不同的方法,试图将其存储在列表、字典、列表中的字典等中,但都没有成功。我也读过beauthulsoup文档和几个论坛,但都没有成功。在

我相信这应该是很简单的事情。这是我正在运行的代码:

import requests
import re
from bs4 import BeautifulSoup

r = requests.get("http://www.metrolyrics.com/notorious-big-albums-list.html")
c = r.content
soup = BeautifulSoup(c, "html.parser")

albums = soup.find("div", {'class' : 'grid_8'})


for page in albums.find_all('a', href=True, alt=True):
    d = {}
    r = requests.get(a['href'])
    c = r.content
    soup = BeautifulSoup(c, "html.parser")
    song = soup.find_all('p', {'class':'verse'})
    title = soup.find_all('h1')

    for item in title:
        title = item.text.replace('Lyrics','')
        print("\n",title.upper(),"\n")

    for item in song:
        song = item.text
        print(song)

当运行这段代码时,您将得到我希望存储在单个变量中的精确输出。在

我已经挣扎了好几天了,所以我真的很感激你的帮助。在

谢谢


Tags: inimporturlforsongtitlehtmlall
2条回答

我成功了!!在

我无法将输出存储在变量中,但我可以编写一个txt文件来存储所有内容,这一点更好。这是我使用的代码:

import requests
import re
from bs4 import BeautifulSoup

with open('nBIGsongs.txt', 'a') as f:

    r = requests.get("http://www.metrolyrics.com/notorious-big-albums-list.html")
    c = r.content
    soup = BeautifulSoup(c, "html.parser")

    albums = soup.find("div", {'class' : 'grid_8'})


    for a in albums.find_all('a', href=True, alt=True):
        r = requests.get(a['href'])
        c = r.content
        soup = BeautifulSoup(c, "html.parser")
        song = soup.find_all('p', {'class':'verse'})
        title = soup.find_all('h1')

        for item in title:
            title = item.text.replace('Lyrics','')
            f.write("\n" + title.upper() + "\n")

        for item in song:
            f.write(item.text)

f.close()

我还是很想知道有没有其他更好的方法。在

谢谢!在

下面是一个如何在一个变量中存储数据的示例。 这可以是JSON或类似的,使用python字典。在

a = dict()
#We create a instance of a dict. Same as a = {}.

a[1] = 'one' 
#This is how a basic dictionary works. There is a key and a value.

a[2] = {'two':'the number 2'}

#Now our Key is normal, however, our value is another dictionary. 

print(a)
#This is how we access the dict inside the dict.
print(a[2]['two'])
# first key [2] (gives us {'two':'the number 2'} we access value inside it [2]['two']")

你将能够将这些知识应用到你的算法中。在

使用相册作为第一个键all['Stay strong'] = {'some-song':'text_heavy'}

我还建议您创建一个函数,因为您正在使用代码。在

例如,请求,然后使用bs4进行解析

^{pr2}$

软件开发的一个好的实践被称为DRY(不要重复自己),因为可读性很重要,与WET相反(浪费每个人的时间,把所有东西写两次)。在

只是要记住一些事情。

相关问题 更多 >