简体   繁体   中英

Downloading Text Files Using Powershell

I'm trying to download and save the text file http://www.gutenberg.org/cache/epub/164/pg164.txt using Powershell. I tried using the code:

$curl http://www.gutenberg.org/cache/epub/164/pg164.txt -OutFile verne.txt

But instead of saving the text file it saved the text file for the page source of http://www.gutenberg.org/ebooks/164?msg=welcome_stranger . I'm wondering if there's something wrong with my code or if I need to use another code.

It's a redirect. If you put the url in the browser you will get the same welcome stranger page. My guess is that they don't want you to access this content in this way. They may require log in, or at the very least, a valid session cookie.

Your link is a redirection, try this :

$uri = 'www.gutenberg.org/ebooks/164.txt.utf-8'
$request = Invoke-WebRequest -Uri $uri -MaximumRedirection 0 -ErrorAction Ignore
 
if($request.StatusDescription -eq 'found')
{
   #redownload the new url (redirection)
   $request=Invoke-WebRequest -Uri $request.Headers.Location
   $request.ParsedHtml.body.outerText
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM