zoukankan      html  css  js  c++  java
  • python 爬虫 下载图片

    import os#导入操作系统模块
    from urllib.request import urlretrieve#下载url对应的文件
    from urllib.request import urlopen #打开url,得到网页源代码
    from bs4 import BeautifulSoup #bs库,对源代码进行各种操作

    downloadDirectory = "downloaded" #下载至名为“download”的文件夹
    baseUrl = "http://pythonscraping.com"#########################################

    #将任意链接转换成absolute URL——清理和标准化
    def getAbsoluteURL(baseUrl, source):
    if source.startswith("http://www."):
    url = "http://"+source[11:]
    elif source.startswith("http://"):
    url = source
    elif source.startswith("www."):
    url = source[4:]
    url = "http://"+url
    else:
    url = baseUrl+"/"+source
    if baseUrl not in url:
    return None
    return url

    #新建一个文件夹,存放下载文件
    def getDownloadPath(baseUrl, absoluteUrl, downloadDirectory):
    path = absoluteUrl.replace("www.", "")
    path = path.replace(baseUrl, "")
    path = downloadDirectory+path
    directory = os.path.dirname(path)
    if not os.path.exists(directory):
    os.makedirs(directory)
    return path

    html = urlopen("http://www.pythonscraping.com")##############################
    bsObj = BeautifulSoup(html)
    downloadList = bsObj.findAll(src=True)#获取src对应的链接list
    #print(downloadList)

    #将链接list中每一个链接转换成absoluteURL
    for download in downloadList:
    fileUrl = getAbsoluteURL(baseUrl, download["src"])
    if fileUrl is not None:
    print(fileUrl)

    urlretrieve(fileUrl, getDownloadPath(baseUrl, fileUrl, downloadDirectory))
  • 相关阅读:
    监听事件 队列 邮件发送
    elasticsearch 天气
    elasticsearch
    event 监听事件
    observer 监听的实现 laravel 框架
    中间件
    git 代码 上传到码云
    laravel 省略入口文件 index.php
    limit offset 和limit
    CSS变形和动画
  • 原文地址:https://www.cnblogs.com/yrm1160029237/p/6295990.html
Copyright © 2011-2022 走看看