zoukankan      html  css  js  c++  java
  • 使用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

  • 相关阅读:
    hdu6060[贪心+dfs] 2017多校3
    Codeforces 547B. Mike and Feet[单调栈/队列]
    Codeforces 545E. Paths and Trees[最短路+贪心]
    gitignore使用
    es学习
    google浏览器安装jsonview
    sychronized关键字底层详解加锁升级过程
    idea 中 “XXX has broken path” 错误解决
    kafka高并发读写的原因
    window redis版本 安装
  • 原文地址:https://www.cnblogs.com/oneasdf/p/9366604.html
Copyright © 2011-2022 走看看