使用Python 和 Selenium 抓取 酷狗 音乐专辑 附源码

在这篇博客中,我将分享如何使用Python和Selenium抓取酷狗音乐网站上的歌曲信息。我们将使用BeautifulSoup解析HTML内容,并提取歌曲和专辑信息。

准备工作

首先,我们需要安装一些必要的库:

pip install requests beautifulsoup4 selenium

代码实现

以下是完整的代码:

import os
import requests
from bs4 import BeautifulSoup as be
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import urllib.parse

# 初始化参数
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

# URL参数编码
keyword = input('请输入歌手:')
search_url = f'https://www.kugou.com/yy/html/search.html#searchType=song&searchKeyWord={urllib.parse.quote(keyword)}'

# 第一步:使用Selenium打开页面
driver = webdriver.Chrome(options
  • 18
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用PythonSelenium抓取网页的所有文本内容是可行的。你可以使用Selenium库和WebDriver来模拟用户与网页的交互并获取完全呈现的内容。下面是一个使用Selenium抓取网页文本内容的示例代码: ```python from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 设置网页URL和浏览器驱动 url = 'https://www.example.com' driver = webdriver.Chrome() # 打开网页 driver.get(url) # 等待网页元素加载完成 wait = WebDriverWait(driver, 10) wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'body'))) # 获取网页的所有文本内容 text = driver.find_element(By.TAG_NAME, 'body').text print(text) # 关闭浏览器驱动 driver.quit() ``` 这段代码使用Chrome浏览器作为驱动,并打开了指定的URL。然后,通过等待页面元素加载完成,使用`driver.find_element()`方法找到页面的`<body>`标签,并获取其文本内容。 请确保在运行代码之前,你已经安装了Selenium库和相应的浏览器驱动。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [使用PythonSelenium抓取网页内容](https://blog.csdn.net/weixin_39915649/article/details/131120944)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Web-page-Scrapping-using-Selenium-Python:SASTRA结果网页使用SeleniumPython进行报废](https://download.csdn.net/download/weixin_42113794/16430337)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值