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

从谷歌浏览器的开发工具进入
选择图片右键点击检查Python爬虫:一个爬取豆瓣电影人像的小案例_第1张图片

![在这里插入图片描述](https://img-blog.csdnimg.cn/1b38c2a942c441fb8cb545a28bb35015.pngPython爬虫:一个爬取豆瓣电影人像的小案例_第2张图片

翻页之后发现网址变化的只有start数值,每次变化值为30
Python爬虫:一个爬取豆瓣电影人像的小案例_第3张图片
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()

Python爬虫:一个爬取豆瓣电影人像的小案例_第4张图片

把爬取的图片全部放到新建的文件夹中存放
Python爬虫:一个爬取豆瓣电影人像的小案例_第5张图片

你可能感兴趣的:(爬虫,爬虫,python,开发语言)