python电影票房排行榜数据爬取
时间: 2023-10-04 22:09:21 浏览: 130
Python爬虫爬取电影票房数据及图表展示操作示例
5星 · 资源好评率100%
好的,以下是一个简单的 Python 代码示例,使用 requests 和 BeautifulSoup 库从猫眼电影网站上爬取电影票房排行榜数据:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://maoyan.com/board/4'
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'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
movie_list = soup.find_all('div', {'class': 'movie-item-info'})
for movie in movie_list:
title = movie.find('a').text
actor = movie.find_all('p')[0].text.strip().replace('\n', '').split(':')[1]
time = movie.find_all('p')[2].text.strip().replace('\n', '').split(':')[1]
score = movie.find('p', {'class': 'score'}).text
print(title, actor, time, score)
```
运行代码后,将会在控制台输出电影名称、演员、上映时间和评分等信息。你可以根据需要对代码进行修改和扩展。
阅读全文