r/PowerShell • u/StrangeCultist • Mar 05 '26
Help to partially download a webpage(ranged requests not supported)
This requires some background.
As per the title. I have several scripts for scraping data from pages of a website(with the permission of its moderators), as the de facto owner of the site is absent to a negligent degree, and external tools are needed to keep things user friendly.
The data in question is in predictable places, and sometimes quite early on a given page. Each run could take substantially less time and take up less bandwidth if I could cut the download short once I have the sections I need. Range requests are not enabled, and frankly if the owner was responsive enough to requests to enable them, what I'm doing would not be necessary in the first place.
Is there a solution within PowerShell itself, a command line utility, or anything else, I could use(preferably one I do not have to compile) that is capable of detecting the content of a web request input as it comes in, or just the amount of data that came through so I could approximate a range request?
7
u/raip Mar 05 '26
Most HTML is perfectly valid XHTML as well - if that's the case for you, the native solution is Select-XML with an XPath filter for the node/data in question. Easiest way to find the correct XPath is with Chrome/Edge/Firefox dev tools.
If it's not parsable with Select-XML, there's a module out there called PSParseHTML: https://github.com/EvotecIT/HtmlTinkerX
This can do all sorts of nifty stuff.
Now for the streaming capability - you're probably going to need to go lower level and implement something directly with HttpClient. This is where stuff can be tricky and it'd be data specific.
4
u/purplemonkeymad Mar 05 '26
I think you would have to do it with the dot net classes directly. HttpClient has a GetStreamAsync which should allow you to read the partial stream for the data. Then you can interrupt the task. Keep in mind it will be a raw byte stream and interrupting the download will mean you are unlikely to be able to use normal html parsing tools.
0
u/Nilxa Mar 05 '26
Easiest way, open your browser dev tools switch to the network tab, reload the page, on the network tab you will see all the network calls the browser is making, find the one that had the info you are after, right click on it select copy powershell, paste that in your ide and test. You will usually find you can drop some of the headers and tidy it all up a bit. But you can capture the output from Invoke-Webrequest E.g.
$response = Invoke-Webrequest....Then check that for the info you are after1
u/[deleted] Mar 06 '26
[removed] — view removed comment