Python爬虫:一个爬取豆瓣电影人像的小案例

从谷歌浏览器的开发工具进入

选择图片右键点击检查

![在这里插入图片描述](https://img-blog.csdnimg.cn/1b38c2a942c441fb8cb545a28bb35015.png![在这里插入图片描述](https://file.jishuzhan.net/article/1697232022344306690/cf13e06e42f046429fc1e19155468d0a.png)

翻页之后发现网址变化的只有start数值,每次变化值为30

Python代码

python 复制代码
import requests
from bs4 import BeautifulSoup
import time
import os


# 豆瓣影人图片
url = 'https://movie.douban.com/celebrity/1011562/photos/'
res = requests.get(url=url, headers="").text
content = BeautifulSoup(res, "html.parser")
data = content.find_all('div', attrs={'class': 'cover'})
picture_list = []
for d in data:
    plist = d.find('img')['src']
    picture_list.append(plist)
print(picture_list)


# https://movie.douban.com/celebrity/1011562/photos/?type=C&start=30&sortby=like&size=a&subtype=a
def get_poster_url(res):
    content = BeautifulSoup(res, "html.parser")
    data = content.find_all('div', attrs={'class': 'cover'})
    picture_list = []
    for d in data:
        plist = d.find('img')['src']
        picture_list.append(plist)
    return picture_list

# XPath://*[@id="content"]/div/div[1]/ul/li[1]/div[1]/a/img
def download_picture(pic_l):
    if not os.path.exists(r'picture'):
        os.mkdir(r'picture')
    for i in pic_l:
        pic = requests.get(i)
        p_name = i.split('/')[7]
        with open('picture\\' + p_name, 'wb') as f:
            f.write(pic.content)

def fire():
    page = 0
    for i in range(0, 450, 30):
        print("开始爬取第 %s 页" % page)
        url = 'https://movie.douban.com/celebrity/1011562/photos/?type=C&start={}&sortby=like&size=a&subtype=a'.format(i)
        res = requests.get(url=url, headers="").text
        data = get_poster_url(res)
        download_picture(data)
        page += 1
        time.sleep(1)


fire()

把爬取的图片全部放到新建的文件夹中存放

相关推荐
w-w0w-w21 小时前
C++模板参数与特化全解析
开发语言·c++
诗词在线21 小时前
中国古代诗词名句按主题分类有哪些?(爱国 / 思乡 / 送别)
人工智能·python·分类·数据挖掘
不绝19121 小时前
C#核心:继承
开发语言·c#
高锰酸钾_21 小时前
机器学习-L1正则化和L2正则化解决过拟合问题
人工智能·python·机器学习
天天睡大觉21 小时前
Python学习11
网络·python·学习
智航GIS21 小时前
11.11 Pandas性能革命:向量化操作与内存优化实战指南
python·pandas
AI即插即用1 天前
即插即用系列(代码实践)专栏介绍
开发语言·人工智能·深度学习·计算机视觉
码农水水1 天前
蚂蚁Java面试被问:混沌工程在分布式系统中的应用
java·linux·开发语言·面试·职场和发展·php
喵了meme1 天前
c语言经验分享
c语言·开发语言
写代码的【黑咖啡】1 天前
Python中的Selenium:强大的浏览器自动化工具
python·selenium·自动化