设计思路:requests发送请求返回HTML→正则匹配电影名称+图片地址→电影名称+图片地址下载保存至本地
from loguru import loggerimport requestsimport redef douban_top250():# return html 页面def html_page():url = '/top250?start=0&filter='headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'}res = requests.get(url, headers=headers)res.encoding = "utf-8"html = res.textreturn htmli = 1number = 0paeg = int(re.findall(r'&filter=" >(.*)</a>', html_page())[-2]) + 1 # 获取页面的返回值while i < paeg:url = f'/top250?start={number}&filter='headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'}res = requests.get(url, headers=headers)res.encoding = "utf-8"html = res.textnumber += 25i += 1v = 0while v < 25:jpg_name = re.findall(' alt="(.*)" src="', html)[v] # douban图片名称jpg_url = re.findall('" src="(.*)" class="">', html)[v] # douban图片地址file_path = fr'D:\jpg\{jpg_name}.png' # 本地图片地址v += 1try:myfile = requests.get(jpg_url)file_path = fr'D:\jpg\{jpg_name}.png'open(file_path, 'wb').write(myfile.content)except:logger.error(f'图片保存失败,请检查路径{file_path}')logger.info(f"{jpg_name} {jpg_url}")douban_top250()
如果觉得《requests+re模块爬取豆瓣排行版电影和图片保存至本地》对你有帮助,请点赞、收藏,并留下你的观点哦!