环境准备
路径准备
浏览器F12获取到加载电影信息u
self.base_url = 'https://movie.douban.com/j/chart/top_list?type={}&interval_id=100:90&action=&start={}&limit={}'
代码实现
import urllib.requestimport json# 某瓣电影排行信息抓取class DouBan(object):# 构造器# 参数1:页码,参数2:每页显示条数, 参数2:电影类型: 6 为情色def __init__(self, page_no, page_size, type):self.base_url = 'https://movie.douban.com/j/chart/top_list?type={}&interval_id=100:90&action=&start={}&limit={}'self.headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36'}self.urls = [self.base_url.format(type, i * page_size, page_size ) for i in range(page_no)]# 获取内容def get_content(self, url):request = urllib.request.Request(url=url, headers=self.headers)response = urllib.request.urlopen(request)content = response.read.decode('utf-8')return content# 下载影片信息def download(self):for url in self.urls:content = self.get_content(url)self.show_movie_info(content)pass# 展示影片信息def show_movie_info(self, content):content = json.loads(content)for info in content:print(f"电影名:【{info['title']}】,上映时间:{info['release_date']},豆瓣评分:{info['score']},类型:{info['types']},主演:{info['actors']} ")# 启动def run(self):self.downloadpass# 入口if __name__ == '__main__':page_no = 5page_size = 20type = 6 # 类别-6为情色DouBan(page_no, page_size, type).run
运行结果
www.tstingmi.com 提供内容。