为什么?
只需使用内置的 PowerShell cmdlet 即可。
Invoke-WebRequest、Start-BitTransfer 或 .Net 命名空间。
您无需浏览器即可抓取网站或下载文件。
注意事项:有些网站会阻止任何自动化工作,无论您尝试使用哪种工具。
3 ways to download files with PowerShell
# 1. Invoke-WebRequest
$url = "http://mirror.internode.on.net/pub/test/10meg.test"
$output = "$PSScriptRoot\10meg.test"
$start_time = Get-Date
Invoke-WebRequest -Uri $url -OutFile $output
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
# 2. System.Net.WebClient
$url = "http://mirror.internode.on.net/pub/test/10meg.test"
$output = "$PSScriptRoot\10meg.test"
$start_time = Get-Date
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)
#OR
(New-Object System.Net.WebClient).DownloadFile($url, $output)
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
# 3. Start-BitsTransfer
$url = "http://mirror.internode.on.net/pub/test/10meg.test"
$output = "$PSScriptRoot\10meg.test"
$start_time = Get-Date
Import-Module BitsTransfer
Start-BitsTransfer -Source $url -Destination $output
#OR
Start-BitsTransfer -Source $url -Destination $output -Asynchronous
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
# Get specifics for a module, cmdlet, or function
(Get-Command -Name Invoke-WebRequest).Parameters
(Get-Command -Name Invoke-WebRequest).Parameters.Keys
Get-help -Name Invoke-WebRequest -Examples
<#
# Built-In Examples
$R = Invoke-WebRequest -URI http://www.bing.com?q=how+many+feet+in+a+mile
$R.AllElements | where {$_.innerhtml -like "*=*"} | Sort { $_.InnerHtml.Length } | Select InnerText -First 5
shortest HTML value often helps you find the most specific element that matches that text.
$R=Invoke-WebRequest http://www.facebook.com/login.php -SessionVariable fb
$FB
$Form = $R.Forms[0]
$Form | Format-List
$Form.fields
$Form.Fields["email"]="User01@Fabrikam.com"
$R=Invoke-WebRequest -Uri ("https://www.facebook.com" + $Form.Action) -WebSession $FB -Method POST -Body $Form.Fields
# Sends a sign-in request by running the Invoke-WebRequest cmdlet. The command specifies a value of "fb" for the SessionVariable parameter, and saves the
$R.StatusDescription
(Invoke-WebRequest -Uri "http://msdn.microsoft.com/en-us/library/aa973757(v=vs.85).aspx").Links.Href
#>
Get-help -Name Invoke-WebRequest -Full
Get-help -Name Invoke-WebRequest -Online
(Get-Command -Name Start-BitsTransfer).Parameters
(Get-Command -Name Start-BitsTransfer).Parameters.Keys
Get-help -Name Start-BitsTransfer -Examples
<#
# Built-In Examples
Start-BitsTransfer -Source "http://server01/servertestdir/testfile1.txt" -Destination "c:\clienttestdir\testfile1.txt"
Import-CSV filelist.txt | Start-BitsTransfer
Start-BitsTransfer -Source "c:\clienttestdir\testfile1.txt" -Destination "http://server01/servertestdir/testfile1.txt" -TransferType Upload
Start-BitsTransfer -Source "http://server01/servertestdir/testfile1.txt", "http://server01/servertestdir/testfile2.txt" -Destination
$Cred = Get-Credential
Start-BitsTransfer -DisplayName MyJob -Credential $Cred -Source "http://server01/servertestdir/testfile1.txt" -Destination "c:\clienttestdir\testfile1.txt"
Import-CSV filelist.txt | Start-BitsTransfer -Asynchronous -Priority Normal
Start-BitsTransfer -Source "http://server01/servertestdir/*.*" -Destination "c:\clienttestdir\"
Import-CSV filelist.txt | Start-BitsTransfer -TransferType Upload
Start-BitsTransfer -Source .\Patch0416.msu -Destination $env:temp\Patch0416.msu -ProxyUsage Override -ProxyList BitsProxy:8080 -ProxyCredential
#>
Get-help -Name Start-BitsTransfer -Full
Get-help -Name Start-BitsTransfer -Online
<#
WebClient Class
Definition
Namespace: System.Net
Assembly: System.Net.WebClient.dll
Provides common methods for sending data to and receiving data from a resource identified by a URI.
https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient?view=netcore-3.1
WebClient.DownloadFile Method
Namespace: System.Net
Assembly: System.Net.WebClient.dll
Downloads the resource with the specified URI to a local file.
https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadfile?view=netcore-3.1
#>
如果您真的想通过 Chrome 使用 Selenium 来执行此操作,那么这个类似的 SO 线程应该会有所帮助。
How to control the download of files with Selenium + Python bindings in Chrome
以及这篇文章和示例:
PowerShell & Selenium: Automate Web Browser Interactions – Part
II
至于这个警告……
>“这种类型的文件会损害您的计算机。您仍然要保留该文件吗?”。
...这不是 PowerShell 或 PowerShell 警告,这是 Windows 和浏览器(IE、Edge、Chrome 等)提醒您注意潜在威胁。
Working around the Google Chrome "This type of file can harm your
computer" problem
Google 最近宣布,它决定改进
防止在 Chrome 浏览器中下载不需要的软件
和谷歌搜索。
在这个主题上也可以查看这些 SO 主题。
How to disable 'This type of file can harm your computer' pop up
This type of file can harm your computer, trying to download an .ini file in Chrome using c# and selenium