帮我用python写一个脚本用来爬取豆瓣top250电影
好的,以下是Python代码,用于爬取豆瓣top250电影:
import requests
from bs4 import BeautifulSoup
url = 'https://movie.douban.com/top250'
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.36'}
movie_list = []
for i in range(0, 250, 25):
params = {'start': str(i), 'filter': ''}
html = requests.get(url, params=params, headers=headers).text
soup = BeautifulSoup(html, 'html.parser')
movies = soup.find_all('div', class_='hd')
for movie in movies:
title = movie.a.span.text.strip()
link = movie.a['href']
movie_list.append({'title': title, 'link': link})
for movie in movie_list:
print(movie['title'], movie['link'])
这个脚本首先定义了一个URL和请求头。然后它遍历每个网页,使用请求参数来获取数据。接下来,它从HTML中提取电影名称和链接,并将它们添加到一个列表中。最后,它遍历列表并打印每个电影的名称和链接。
原文地址: https://www.cveoy.top/t/topic/B9i 著作权归作者所有。请勿转载和采集!