python爬虫豆瓣电影并且做创意展示
时间: 2023-07-11 08:58:42 浏览: 164
好的,下面是一个创意展示的示例代码,可以爬取豆瓣电影top250的电影名称、评分和封面图片,并将数据可视化为一个瀑布流的图片墙:
```python
import requests
from bs4 import BeautifulSoup
from PIL import Image
from io import BytesIO
import random
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import webbrowser
url = 'https://movie.douban.com/top250'
def get_movies(url):
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 = soup.find('ol', attrs={'class': 'grid_view'})
movie_list = []
for movie_li in movie_list_soup.find_all('li'):
detail = movie_li.find('div', attrs={'class': 'hd'})
movie_name = detail.find('span', attrs={'class': 'title'}).getText()
rating = movie_li.find('span', attrs={'class': 'rating_num'}).getText()
img_src = movie_li.find('div', attrs={'class': 'pic'}).find('img').get('src')
response = requests.get(img_src)
img = Image.open(BytesIO(response.content))
movie_list.append({'name': movie_name, 'rating': float(rating), 'img': img})
return movie_list
movies = get_movies(url)
root = Tk()
root.geometry('800x600')
root.title('豆瓣电影Top250瀑布流')
def open_link(event):
webbrowser.open_new(event.widget.data['link'])
def show_movies(movies):
columns = 4
rows = len(movies) // columns + 1
for i in range(rows):
for j in range(columns):
index = i * columns + j
if index < len(movies):
movie = movies[index]
x = j * 200 + 100
y = i * 300 + 150
rating = movie['rating']
img = movie['img']
img.thumbnail((200, 200))
img_label = Label(root, image=img)
img_label.place(x=x, y=y)
img_label.data = {'link': 'https://movie.douban.com/top250'}
img_label.bind('<Button-1>', open_link)
name_label = Label(root, text=movie['name'], font=('Arial', 12))
name_label.place(x=x, y=y+200)
rating_label = Label(root, text='%.1f' % rating, font=('Arial', 12), bg='yellow')
rating_label.place(x=x+150, y=y+250)
root.mainloop()
show_movies(movies)
```
这段代码使用了requests库和BeautifulSoup库来获取豆瓣电影top250的电影名称、评分和封面图片信息,并使用PIL库和Tkinter库将数据可视化为一个瀑布流的图片墙。您可以根据您的需求进行修改和扩展。请注意,爬取网站的时候应该遵守网站的爬取规则和法律法规。
阅读全文