使用 Powershell 下载文本文件
Downloading Text Files Using Powershell
我正在尝试使用 Powershell 下载并保存文本文件 http://www.gutenberg.org/cache/epub/164/pg164.txt。我尝试使用代码:
$curl http://www.gutenberg.org/cache/epub/164/pg164.txt -OutFile verne.txt
但它没有保存文本文件,而是保存了 http://www.gutenberg.org/ebooks/164?msg=welcome_stranger 页面源的文本文件。我想知道我的代码是否有问题,或者我是否需要使用其他代码。
这是一个重定向。如果您在浏览器中输入 url,您将获得相同的欢迎陌生人页面。我的猜测是他们不希望您以这种方式访问此内容。他们可能需要登录,或者至少需要一个有效的会话 cookie。
您的 link 是重定向,试试这个:
$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
}
我正在尝试使用 Powershell 下载并保存文本文件 http://www.gutenberg.org/cache/epub/164/pg164.txt。我尝试使用代码:
$curl http://www.gutenberg.org/cache/epub/164/pg164.txt -OutFile verne.txt
但它没有保存文本文件,而是保存了 http://www.gutenberg.org/ebooks/164?msg=welcome_stranger 页面源的文本文件。我想知道我的代码是否有问题,或者我是否需要使用其他代码。
这是一个重定向。如果您在浏览器中输入 url,您将获得相同的欢迎陌生人页面。我的猜测是他们不希望您以这种方式访问此内容。他们可能需要登录,或者至少需要一个有效的会话 cookie。
您的 link 是重定向,试试这个:
$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
}