Powershell get the code for specific Web page information _powershell

Source: Internet
Author: User
Tags json

PowerShell can easily get information on the Web page and read the corresponding content. If the object's format is XML or JSON, it's easier to handle, often using the Invoke-restmethod and Invoke-webrequest commands. The first is to get the content of the JSON format, which can get the contents of the entire Web page.

For example, I would like to inquire about the weather in Sydney tomorrow. A Web site that provides apis is randomly searched.
Http://openweathermap.org/current#name

I'm going to search for Sydney, so the corresponding format is
Http://api.openweathermap.org/data/2.5/weather?q=sydney,au He will automatically generate a JSON-formatted result.

We can use Invoke-restmethod to get this result directly, for example

$b =invoke-restmethod "Http://api.openweathermap.org/data/2.5/weather?q=sydney,au"
  $c =[pscustomobject]@{   
  ' Description ' = $b. Weather.description 
  ' name ' = $b. Name 
  ' windspeed ' = $b. Wind.speed   
  

I can also use Invoke-webrequest to crawl the content of the entire page, and then convert it from JSON to the same way.

$a = Invoke-webrequest-uri "Http://api.openweathermap.org/data/2.5/weather?q=sydney,au" $b = $a. Content | Convertfrom-json

Similarly, if I want to get the latest content of RSS for a blog. You can use Invoke-webrequest to crawl the corresponding XML file, such as

[XML] $a = Invoke-webrequest-uri "http://blogs.msdn.com/b/powershell/rss.aspx" $a. Rss.channel.Item | Select Title,pubdate

The function is very powerful, the use is very simple.

This article comes from the "Mapo Tofu" blog

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.