aiaiask.com©

开花即结果 提问即答案

用python爬取豆瓣前50数据
原始问题:

用python爬取豆瓣前50数据

import requests
from bs4 import BeautifulSoup
 
url = 'https://movie.douban.com/chart'   #豆瓣电影排行榜页面地址 
res = requests.get(url)    #发送请求,获取响应数据 
html = res.text           #将响应数据转为文本格式 
soup = BeautifulSoup(html,'html.parser')     #使用BeautifulSoup创建对象,并使用html格式进行解析 

 # 找到所有class为item的div   其中包含了前50部电影信息 
items = soup.find_all('div',class_='item')  

 # 遍历items,依此处理前50部电影信息:   各字段信息的定位方法如下图所示:    https://img-blog.csdnimg.cn/20200804171914407.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM1Njg5MjUw,size_16,color_FFFFFF,t_70#pic_center   

for item in items:     #遍历items     

    title = item.find('span',class_='title').text     # 电影名称      

    score = item.find('span',class_='rating_num').text     # 电影评分      

    directorAndActor = item['data-staff']     # 导演、主要演员      

    releaseDateAndCountry = item['data-region'] + ' | ' + item['data-release']        #上映日期、国家/地区     

    categoryAndDurationTime=item['data-category']+' | '+item['data-duration']        #类型、片长     

 print('片名: ',title)      print('评分: ',score)      print('导演&主要演员: ',directorAndActor)      print('上映日期&国家/地区: ',releaseDateAndCountry)      print('类型&片长: ',categoryAndDurationTime,'  
用python爬取豆瓣前50数据
https://www.aiaiask.com/ask/12535.html
AI / 模型DeepSeek
时间 / DATEApril 17, 2023, 8:52 PM
语言zh-CN
IP / 区域重庆市重庆市