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()

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

相关推荐
lly20240610 分钟前
SQL ROUND() 函数详解
开发语言
烛阴17 分钟前
用 Python 揭秘 IP 地址背后的地理位置和信息
前端·python
大宝剑17020 分钟前
python环境安装
开发语言·python
lly20240633 分钟前
CSS3 多媒体查询
开发语言
Element_南笙37 分钟前
吴恩达新课程:Agentic AI(笔记2)
数据库·人工智能·笔记·python·深度学习·ui·自然语言处理
倔强青铜三1 小时前
苦练Python第69天:subprocess模块从入门到上瘾,手把手教你驯服系统命令!
人工智能·python·面试
倔强青铜三1 小时前
苦练 Python 第 68 天:并发狂飙!concurrent 模块让你 CPU 原地起飞
人工智能·python·面试
星期天要睡觉1 小时前
深度学习——循环神经网络(RNN)实战项目:基于PyTorch的文本情感分析
人工智能·python·rnn·深度学习·神经网络
天***88962 小时前
js封装一个双精度算法实现
开发语言·前端·javascript
.小小陈.2 小时前
数据结构2:单链表
c语言·开发语言·数据结构·笔记·学习方法