用python写一段代码,爬取酷狗TOP500数据爬取
时间: 2023-10-06 16:07:52 浏览: 45
好的,以下是用 Python 爬取酷狗 TOP500 数据的示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.kugou.com/yy/rank/home/1-8888.html?from=rank'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
song_list = soup.select('.pc_temp_songlist > ul > li')
for song in song_list:
rank = song.select_one('.pc_temp_num').text.strip()
name = song.select_one('.pc_temp_songname').text.strip()
singer = song.select_one('.pc_temp_artist').text.strip()
print(f'{rank}. {name} - {singer}')
```
这段代码通过 requests 库向酷狗 TOP500 首页发送 HTTP 请求,得到响应后用 BeautifulSoup 库解析 HTML 文档。然后,我们通过 CSS 选择器选出歌曲列表中的每一首歌曲,并从中提取出歌曲名和歌手名,最终输出歌曲名单。
相关问题
请用Python写出一段代码爬取豆瓣电影TOP30数据
好的,以下是用Python爬取豆瓣电影TOP30数据的代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://movie.douban.com/top250'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
movies = soup.select('.info')
for movie in movies:
title = movie.select_one('.title').text.strip()
rating = movie.select_one('.rating_num').text.strip()
quote = movie.select_one('.quote').text.strip()
print(f'Title: {title}\nRating: {rating}\nQuote: {quote}\n')
```
运行代码后,会输出豆瓣电影TOP30中每一部电影的标题、评分和经典台词。
用python写一段代码爬取某网站图片
抱歉,我是语言模型AI,无法执行代码。以下是一个简单的Python爬取网站图片的代码示例:
```python
import requests
from bs4 import BeautifulSoup
import os
# 设置图片保存路径
save_folder = './images'
if not os.path.exists(save_folder):
os.makedirs(save_folder)
# 网站url
url = 'http://www.example.com'
# 发送get请求获取网页内容
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 找到所有的img标签
img_tags = soup.find_all('img')
# 遍历所有的img标签,下载图片
for img in img_tags:
img_url = img.get('src')
if img_url.startswith('http'):
img_name = img_url.split('/')[-1]
img_path = os.path.join(save_folder, img_name)
with open(img_path, 'wb') as f:
f.write(requests.get(img_url).content)
print(f'{img_name} 下载完成')
```
这段代码会爬取指定的网站上所有的图片并保存到指定的文件夹中。需要注意的是,不要滥用爬虫,尊重网站的内容版权。