Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to download Bing Daily Pictures using PowerShell

Shulou Source: shulou.com Published: 2022-06-03 01:33:11 09月25日 Update

I haven't written a technical article for a long time, and I have experienced a lot of great events in recent years. Recently, I can finally calm down and write something.

Today, I want to talk about some applications of POWERSHELL for WEB pages. I only recently found that PS can also do crawlers. So I want to throw a brick to attract jade to give you an idea.

The main command to be used this time is invoke-webrequest

First, let's take a look at the official introduction to this order.

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

It's easy to understand that this PS command allows you to simulate a browser to send a request to a website and get the information you want.

So today we will simply start by downloading Bing's Meitu every day with POWERSHELL.

# bing Daily Picture complete Code

$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")

}

One of the most critical points is how to convert garbled content into Json. Convertfrom-json is used here, which must be converted because powershell cannot read any useful information from the web page code shown in the figure below.

After successful conversion, the variables stored in $decode become PS-understandable format, which contains the URL and name of the image and which god's work, and so on. And then it'll be easy to deal with.

Foreach ($image in $images)

{

$imageurl = $image.url

# get picture URL

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

# complete URL

$name = $image.hsh

# get the picture name

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

# download to the PS running directory

}

Although the script is simple, it enlightens me a lot and makes me see the infinite possibilities of PS.

END

Tags: Picture information command code name big successful throw a brick to attract jade the following picture thing garbled work key variable big event great god official train of thought technology technical category Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Huawei Xiaomi vpn Shulou Technology