import requests
from bs4 import BeautifulSoup
# 豆瓣电影页面链接
url = 'https://movie.douban.com/subject/1292052/'
# 设置请求头信息,模拟浏览器请求
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'
}
# 发送HTTP请求,获取HTML源代码
response = requests.get(url, headers=headers)
# 使用Beautiful Soup解析HTML
soup = BeautifulSoup(response.content, 'html.parser')
# 获取主演列表
cast_list = soup.select('#info span.actor span')
# 遍历主演列表,输出演员名字
for cast in cast_list:
actor_name = cast.get_text()
print(actor_name)
测试截图