Python 爬虫实战:豆瓣电影 Top250 数据抓取
以下是一个简单的爬虫案例,演示如何使用 Python 爬取豆瓣电影 Top250 的电影名称和评分:
- 导入所需模块
import requests
from bs4 import BeautifulSoup
- 发送请求获取网页源代码
url = 'https://movie.douban.com/top250'
response = requests.get(url)
html = response.text
- 解析网页源代码,获取电影名称和评分
soup = BeautifulSoup(html, 'html.parser')
movie_list = soup.find_all('div', class_='info')
for movie in movie_list:
name = movie.find('span', class_='title').text
rating = movie.find('span', class_='rating_num').text
print(name, rating)
- 运行程序,输出结果
肖申克的救赎 9.7
霸王别姬 9.6
这个杀手不太冷 9.4
...
原文地址: https://www.cveoy.top/t/topic/nRSx 著作权归作者所有。请勿转载和采集!