【Python】------ Python批量爬取某网站图片代码展示

一, Python批量爬取某网站图片代码展示 简洁清晰:

1.使用技术包:requests,urllib,BeautifulSoup等;

1.效果展示:

【Python】------ Python批量爬取某网站图片代码展示_第1张图片

import os
import re

import requests
import json
from lxml import etree
import urllib
from bs4 import BeautifulSoup

#下载图片
def getSogouImagDowld(htmlsrc,filename,path):
    imgHtml = requests.get(htmlsrc)
    soup = BeautifulSoup(imgHtml.text, 'html.parser')
    imgSrcHtml = soup.select('.photo-information-content img')
    m=0
    for i in imgSrcHtml:
       selector = etree.HTML(str(i))
       imgsrc = selector.xpath('//img/@src')[0]
       print('***** '+imgsrc+' *****'+'   Downloading...')
       try:
           urllib.request.urlretrieve(imgsrc, path + filename + '/' + str(m) + '.jpg')
           m = m + 1
           print('Download complete!')
       except IOError:
           print ("没有找到文件")


#获取图片

def getSogouImag(page,limit,path):

    vpage = page
    vlimit= limit
    # imgs = requests.get('https://www.zcool.com.cn/discover.json?cate=1&subCate=7&hasVideo=0&city=0&college=0&recommendLevel=2&sort=9&limit=200&page=1')
    imgs = requests.get('https://www.zcool.com.cn/search/content.json?word=nutdream&cate=0&type=0&recommendLevel=0&time=0&hasVideo=0&city=0&college=0&sort=5&limit=25&column=5&page=1')
    jd = json.loads(imgs.text)
    jdData = jd['data']["data"]
    # jdData = jd['data']
    html_url = []
    file_name = []

    #保留网址
    for jd_json in jdData:
        if 'pageUrl' in jd_json['object']:
          pageUrlstr=jd_json['object']['pageUrl']
          fileName=jd_json['object']['title'].strip()
          fileName=re.sub('[a-zA-Z0-9"#$%&\'()*+,-./:;<=>?@,。?★、…【】《》?“”‘[\\]^_`{|}~\s]+','', fileName)
          html_url.append(pageUrlstr)
          file_name.append(fileName)

    #创建文件夹
    for fileValue in file_name:
        print('***** ' + str(fileValue) + ' 创建文件夹 *****' + '   Createloading...')
        fileDY=path+fileValue
        isExists = os.path.exists(fileDY)
        # 判断结果 不存在就创建
        if not isExists:
            os.makedirs(fileDY)

    #下载图片
    fileNum=0
    for url in html_url:
        print('***** ' + str(url) + ' 下载图片中 *****' + '   downloadloading...')
        filename=file_name[fileNum]
        getSogouImagDowld(url,filename,path)
        fileNum=fileNum+1


getSogouImag(200,1,'D:/nutdream/')

效果图:

【Python】------ Python批量爬取某网站图片代码展示_第2张图片

 推荐其他文章:

【Python】-------通过Python爬取某乎动态图片实例代码_皮皮冰要做大神-CSDN博客一、找到需要爬取的用户主页:例如如下:https://www.zhihu.com/people/chen-ge-monica想要爬取其中的动态图片进行保存下来。可以通过python实现。二、通过python技术实现。1.先找[动态信息]是如何渲染的,既然前端显示出来,说明肯定有调取后台数据的url,怎么查看呢 看如下图: 按【F12】打开【开发者工具】找到【Network】找到如下url地...https://blog.csdn.net/qq_38366657/article/details/122878530【Python】------- Python 12个常用内置函数的使用方式_皮皮冰要做大神-CSDN博客一,Python 12个常用内置函数的使用方式 :1.max - 获取最大值;2.min - 获取最小值;3.list - 转成列表;4.append - 追加一个元素;5.count - 某个元素出现的次数;6.extend - 已存在的列表中添加新的列表内容;7.index - 找到索引的位置;8.insert - 插入索引任意位置上;9.pop - 移除指定索引位置;10.remove -移除匹配项;11.reverse - 列表元素进行排序;12...https://blog.csdn.net/qq_38366657/article/details/114534914

你可能感兴趣的:(#,Python,python,数据挖掘,爬虫,requests,urllib)