Python ngscope=“web scraping类上的Python div”

2024-04-20 12:55:34 发布

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

我是python新手,我正在尝试从我最喜欢的电台网站上获取一些歌曲名称,但无论我做什么,我都无法进入div ui-view="main.header" class="ng-scope"来获取歌曲名称。在

使用我的代码,我可以从txt读取第一级div,但不能读取更深层:

<div id="audio-player" style="width: 0px; height: 0px"></div>
<div id="fb-root"></div>
<div ui-view="main.header"></div>
<div ui-view="main.content"></div>
<div ui-view="main.footer"></div>

歌曲列表的刷新率是10秒,是不是因为这个而阻止了该区域的刮擦? 我也尝试过div1 = soup.findAll(div),但没有成功。在

您可以在www.rockfm.ro网站在

解析代码:

^{pr2}$

这是我的代码:

import urllib
from BeautifulSoup import *

url = "www.rockfm.ro"
html = urllib.urlopen('http://www.rockfm.ro').read()

soup = BeautifulSoup(html)

div1 = soup.findAll(True)

#code to get into divs` classes

for div2 in div1:
    print("Level 1: "+ str(div2))
    with open('rock.txt', 'a') as file:
        file.write("Level 1: " + str(div2) + "\n")
    div3 = div2.findAll(True)

    for div4 in div3:
        print ("Level 2: "+ str(div4))
        with open('rock.txt', 'a') as file:
            file.write("Level 2: " + str(div4) + "\n")
        div5 = div4.findAll(True)

        for div6 in div5:
            print ("Level 3:" + str(div6))
            with open('rock.txt', 'a') as file:
                file.write("Level 3: " + str(div6) + "\n")
            div7 = div6.findAll(True)

            for div8 in div7:
                print ("Level 4:" + str(div8))
                with open('rock.txt', 'a') as file:
                    file.write("Level 3: " + str(div8) + "\n")
                div9 = div8.findAll(True)

                for div10 in div9:
                    print ("Level 4:" + str(div10))
                    with open('rock.txt', 'a') as file:
                        file.write("Level 4: " + str(div10) + "\n")

Tags: indivtxttrueforaswithopen
1条回答
网友
1楼 · 发布于 2024-04-20 12:55:34

试试这个:

songs = []
soup = BeautifulSoup(html.text, 'html.parser')
li_list = soup.find.all('li')
for li in li_list:
    if li['ng-repeat'] == "track in trackList.lista":
    songs.append(li.text)

相关问题 更多 >