python爬取站酷首页推荐图片

话不多说:

import requests
from lxml import etree
import os
import time
import random

if not os.path.exists('站酷'):
    os.mkdir('站酷')

headers = {
    
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.47'}

def get_picture(page):
    url = f"https://www.zcool.com.cn/?p={page}#tab_anchor"
    response = requests.get(url=url,headers=headers)
    html = etree.HTML(response.text)
    div_list = html.xpath('//*[@id="body"]/main/div[3]/div/div')
    for div in div_list:
        img_src = div.xpath('./div[@class="card-img"]/a/img/@src')[0]
        img_data = requests.get(url=img_src,headers=headers).content
        img_title = '站酷/'+ div.xpath('./div[@class="card-img"]/a/img/@title')[0] + '.jpg'
        try:
            with open(img_title,'wb') as fp:
                fp.write(img_data)
            print(img_title+'下载完毕!')
        except:
            print('出现错误!')
        time.sleep(random.randint(3,5))

for i in range(1,20):
    get_picture(i)
    print(f'第{i}页下载完毕!')
    time.sleep(10)

猜你喜欢

转载自blog.csdn.net/hexiechuangxin/article/details/113796461