Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to download Bing Daily Pictures using PowerShell

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report