• 使用PowerShell下载必应图片


    今天想聊聊POWERSHELL对于WEB页面的一些应用,本人也是最近才发觉其实PS也是可以做爬虫的。。。所以想抛砖引玉给大家一个思路。

    这次要用到的主要命令是 invoke-webrequest 

    先来看看官方对于这个命令的介绍

    The Invoke-WebRequest cmdlet sends HTTP, HTTPS, FTP, and FILE requests to a web page or web service. It parses the response and returns collections of forms, links, images, and other significant HTML elements.

    https://docs.microsoft.com/zh-cn/powershell/module/Microsoft.PowerShell.Utility/Invoke-WebRequest?view=powershell-5.1

    其实很好理解,这条PS命令可以让你模拟浏览器发送请求给网站,并且得到你要的信息。

    所以今天我们就从简单的入手,用POWERSHELL下载每日必应的美图


    #bing每日图片 完整代码

    $picurl = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=10"

    $data = invoke-webrequest $picurl

    $decode = convertfrom-json -inputobject $data.content

    $images = $decode.images

    foreach ($image in $images)

    {

    $imageurl = $image.url

    $fullurl = "http://www.bing.com" + $imageurl

    $name = $image.hsh

    invoke-webrequest $fullurl -outfile ($name + ".jgp")

    }


    其中最关键的点是如何将乱码一样的content转换为Json, 这里要用到 convertfrom-json,由于powershell 是无法从下图中得到的网页代码读取任何有用信息所以必须要转换。

    image.png

    在成功转换之后存储在$decode里的变量变成PS易懂的格式,其中包含了该图片的URL和名称以及哪位大神的作品等等信息。再接下去就很好处理了。

    image.png

    foreach ($image in $images)

    {

    $imageurl = $image.url

    #获取图片URL

    $fullurl = "http://www.bing.com" + $imageurl

    #补全URL

    $name = $image.hsh

    #获取图片名称

    invoke-webrequest $fullurl -outfile ($name + ".jgp")

    #下载到PS运行目录

    }

    脚本虽然简单但是给我的启发很大,让我看到了PS的无限可能。

    END

  • 相关阅读:
    Vue-如何实现响应式
    Docker中mysql容器时区问题
    Django格式化日期时,抛出异常ValueError: embedded null byte
    前端报被CORS策略阻止,Django开启跨域解决
    DRF框架之认证、授权和登录
    Django之ALLOWED_HOSTS、LOGGING和多个子应用管理
    DRF框架生成接口文档
    DRF框架之自定义action
    DRF框架之视图集、Routers路由
    DRF框架之Concrete Generic Views
  • 原文地址:https://www.cnblogs.com/oneasdf/p/9366604.html
Copyright © 2020-2023  润新知