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