爬虫爬取豆瓣top250的上映日期 上映地区 电影类型 的代码例子
时间: 2023-12-18 07:04:58 浏览: 30
以下是一个使用Python和BeautifulSoup库爬取豆瓣电影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.3'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, "html.parser")
movies = soup.find_all("div", class_="item")
for movie in movies:
title = movie.find("div", class_="title").get_text().strip()
info = movie.find("div", class_="bd").get_text().strip()
date = info.split("/")[0].strip()
region = info.split("/")[1].strip()
genre = info.split("/")[2].strip()
print(title, date, region, genre)
```
此代码将豆瓣电影Top250网页存为HTML文件,然后使用BeautifulSoup库将其解析为Python可读的对象,并提取电影的名称、上映日期、上映地区和电影类型信息。您可以将此代码保存为.py文件并运行它来获取所需的信息。