1

Often Invoke-WebRequest is preferable in usage with the -UseBasicParsing parameter, if not using Invoke-RestMethod command for both performance and network savings.

But the results from these don't have the good ol' PARSEHTML method.

How can we parse html using the stated command setups?

  1. Invoke-Webrequest $site -UseBasicParsing
  2. Invoke-RestMethod $site

1 Answer 1

3

The scenario can be solved by creating a new HTML object and writing to its IHTMLDocument2 section

NOTE: THIS IS ONLY VALID IN WINDOWS POWERSHELL 5.0 and 5.1

You can deal with the listed scenarios as follows:

  1. For Invoke-Webrequest $site -UseBasicParsing

    $html = new-object -ComObject "HTMLFile; $html.IHTMLDocument2_write($site.rawcontent)

  2. For Invoke-RestMethod $site

    $html = new-object -ComObject "HTMLFile; $html.IHTMLDocument2_write($site)

Now you can parse like normal for example getting an element by id

$button = $html.getElementById('button')

Sign up to request clarification or add additional context in comments.

2 Comments

how do you do this in powershell 7.1?
Can you give a complete example, that defines elements like $site? Is site just $site="google.com" or is there more to it? Your example isn't clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.