用Python解析post请求

2024-10-03 09:08:35 发布

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

你好!我在解析无线电台网站上的动态元素时遇到问题。这里您需要当前歌曲的名称

import requests
from bs4 import BeautifulSoup


songsNameList = []

url = f"https://top-radio.ru/web/russkij-xit"
q = requests.get(url)
result = q.content

soup = BeautifulSoup(result, 'lxml')
songs = soup.select("#se_igra")

for song in songs:
    print(song.find('span'))
    songsNameList.append(song.find('span'))

https://top-radio.ru/web/marusya-fm

包含在gif formatfrom POST字符串中的歌曲名称

Output,我有

我如何分析这个案例


Tags: fromhttpsimportweburlsongtopru
1条回答
网友
1楼 · 发布于 2024-10-03 09:08:35

这个网站使用java脚本。漂亮的汤是不够的。使用Selenium完成这样的任务。 你可以在这里读更多。 https://selenium-python.readthedocs.io/ 在这里你可以下载geckodriver for Firefox。 https://github.com/mozilla/geckodriver/releases 然后在代码中添加以下行:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.firefox.options import Options
binary = FirefoxBinary(r'/usr/bin/firefox') #add here in quotes path to your Firefox
caps = DesiredCapabilities.FIREFOX.copy()
caps['marionette'] = True

driver = webdriver.Firefox(firefox_binary=binary, capabilities=caps, firefox_profile=profile,
                               executable_path='/usr/bin/geckodriver')  ##add here in quotes path to downloaded geckodriver
q = driver.get(url)

相关问题 更多 >