python爬虫qq音乐排行榜
时间: 2023-11-01 09:58:46 浏览: 220
python爬虫可以用来爬取QQ音乐排行榜中的信息。你可以使用selenium库来模拟浏览器的操作,通过控制滚轮来加载数据。然后,你可以使用xpath来解析页面,获取每个用户的头像链接、昵称、评论时间和评论内容。将这些信息存储在一个列表中即可。以下是一个示例代码:
```python
from selenium import webdriver
import time
url = 'https://y.qq.com/n/ryqq/songDetail/0006wgUu1hHP0N'
driver = webdriver.Chrome()
driver.get(url)
time.sleep(1)
driver.maximize_window()
info_list = []
_single = True
while _single:
items = driver.find_elements_by_xpath("//li[@class='comment__list_item c_b_normal']")
print(len(items))
if len(items) < num:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
else:
_single = False
for index, item in enumerate(items):
dic = {}
try:
headPortraits = item.find_element_by_xpath("./div[1]/a/img").get_attribute('src')
name = item.find_element_by_xpath("./div[1]/h4/a").text
time = item.find_element_by_xpath("./div[1]/div[1]").text
content = item.find_element_by_xpath("./div[1]/p/span").text
content = content.replace('\n','')
dic['headPor'] = headPortraits
dic['name'] = name
dic['time'] = time
dic['cont'] = content
info_list.append(dic)
except Exception as e:
print(e)
print(info_list)
```
阅读全文
相关推荐

















