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

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

相关推荐
不绝1915 分钟前
Lua基础语法入门:注释、变量类型与字符串操作
开发语言·lua
the局外人30 分钟前
Python 实战检索优化:BM25、向量与重排
数据库·后端·python
__underline__34 分钟前
冷门Python知识点006篇:Python 类体里的名字,能被"凭空造出来"
后端·python
Hhy_110737 分钟前
《C++深度解构07》手撕 vector —— 从底层三指针架构到迭代器失效彻底吃透
c语言·开发语言·c++·学习·visual studio
风落青芜1 小时前
链式二叉树的实现与基本操作
c语言·开发语言·数据结构·算法
艾莉丝努力练剑1 小时前
【QT】系统相关:TCP的API介绍
开发语言·前端·c++·人工智能·qt·学习·tcp/ip
用户0332126663671 小时前
使用 Python 轻松移除 Excel 文档中的重复行
python·excel
LiaoWL1231 小时前
从零搭一个 RAG 知识库(二):换上语义 Embedding——BGE 实测与阈值重校准
python·大模型·numpy·embedding·向量检索·rag
Dmz丶2 小时前
前端转 AI 100 天 Day 48:向量数据库——100 万向量的毫秒级检索
python
青少儿编程课堂2 小时前
蓝桥杯·全国青少年信息素养大赛·电子学会Python/C++考级:中国剩余定理解析
c++·python·算法·bfs·信息学竞赛