How to download Bing daily pictures using PowerShell

Source: Internet
Author: User

For a long time did not write technical articles, I have been in the past few years have been a big big volt experience a lot of things. Recently, I can finally write something down.


Today I want to talk about PowerShell for the Web page of some applications, I also recently found that in fact PS can also do crawler ... So I want to give you a thought.


The main command to use this time is invoke-webrequest .


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


In fact, it is very good to understand that this PS command allows you to simulate the browser to send a request to the website, and get the information you want.

So today we're going to start with the simple, download the daily Bing maps with PowerShell



#bing每日图片 Full 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 the garbled content into JSON, where Convertfrom-json is used, because PowerShell is unable to read any useful information from the page code it is getting, so it has to be converted.


The variable stored in the $decode after a successful conversion becomes a PS-understandable format that contains the URL and name of the picture, and the work of the Great God, among other information. And then it's good to deal with it.


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运行目录

}


Although the script is simple but gives me a lot of inspiration, let me see the infinite possibilities of PS.

END


How to download Bing daily pictures using PowerShell

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.