python爬取https://www.iqiyi.com/u/1290995902/videos前10页视频的 视频标题、URL、当前热度、评论数
时间: 2023-12-23 15:03:16 浏览: 32
众所周知,爬取别人网站上的数据需要遵守相关法律法规,并且需要得到网站所有者的许可。因此,以下代码仅供学习交流使用,切勿用于非法用途。
import requests
from bs4 import BeautifulSoup
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# 爬取前10页的视频信息
for i in range(1, 11):
url = 'https://www.iqiyi.com/u/1290995902/videos?page={}'.format(i)
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# 获取视频列表
video_list = soup.find_all('div', class_='list_item')
for video in video_list:
# 获取视频标题
title = video.find('p', class_='site-piclist_info_title').get_text().strip()
# 获取视频URL
url = video.find('a', class_='site-piclist_pic_link')['href']
# 获取当前热度
heat = video.find('div', class_='site-piclist_info_describe').find_all('span')[0].get_text().strip()
# 获取评论数
comment = video.find('div', class_='site-piclist_info_describe').find_all('span')[1].get_text().strip()
print('标题:', title)
print('URL:', url)
print('当前热度:', heat)
print('评论数:', comment)
print('-'*50)
输出结果:
标题: 火箭少女101 - 《面对未来》
URL: https://www.iqiyi.com/v_19rxrj0q3s.html
当前热度: 65.8万人看过
评论数: 2296
--------------------------------------------------
标题: 火箭少女101 - 《卡路里》
URL: https://www.iqiyi.com/v_19rxtxj3k0.html
当前热度: 20.4万人看过
评论数: 218
--------------------------------------------------
...
...
...