首页 > 解决方案 > 如何构建一个可以定期从 Internet 获取内容并使用 PowerShell 将其保存到每个新的 csv 文件的脚本?

问题描述

我是 PowerShell 的初学者,正在考虑使用 PowerShell 为动态网页开发一个抓取工具。

主意:

我尝试使用“Invoke-WebRequest”来抓取免费的抓取网站“https://quotes.toscrape.com/” 抓取后,('For Loop,(Get-content | Format-Table)和 Export-CSV' 命令将是used),它将在新文件夹中创建一个新的 .csv 文件并将内容保存到该文件中。

难度 1:尝试使用“Invoke-WebRequest”抓取“https://quotes.toscrape.com/”,但我没有找到任何引号字符串。对我来说,它看起来只是返回了格式化代码

输出 :

 StatusCode        : 200
StatusDescription : OK
Content           : <!DOCTYPE html>
                    <html lang="en">
                    <head>
                        <meta charset="UTF-8">
                        <title>Quotes to Scrape</title>
                        <link rel="stylesheet" href="/static/bootstrap.min.css">
                        <link rel="stylesheet" href="/static/m...
RawContent        : HTTP/1.1 200 OK
                    Connection: keep-alive
                    Vary: Accept-Encoding
                    Strict-Transport-Security: max-age=15724800; includeSubDomains
                    Content-Length: 11053
                    Content-Type: text/html; charset=utf-8
                    Date: Sat...
Forms             : {}
Headers           : {[Connection, keep-alive], [Vary, Accept-Encoding], [Strict-Transport-Security, max-age=15724800; includeSubDomains], [Content-Length,
                    11053]...}
Images            : {}
InputFields       : {}
Links             : {@{innerHTML=Quotes to Scrape; innerText=Quotes to Scrape; outerHTML=<A style="TEXT-DECORATION: none" href="/">Quotes to Scrape</A>;
                    outerText=Quotes to Scrape; tagName=A; style=TEXT-DECORATION: none; href=/}, @{innerHTML=Login; innerText=Login; outerHTML=<A
                    href="/login">Login</A>; outerText=Login; tagName=A; href=/login}, @{innerHTML=(about); innerText=(about); outerHTML=<A
                    href="/author/Albert-Einstein">(about)</A>; outerText=(about); tagName=A; href=/author/Albert-Einstein}, @{innerHTML=change;
                    innerText=change; outerHTML=<A class=tag href="/tag/change/page/1/">change</A>; outerText=change; tagName=A; class=tag;
                    href=/tag/change/page/1/}...}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 11053

难度2:

完整代码工作:

#1. Creating an array of Folders using For Loop 
for ($i = 1; $i -lt 6; $i++){
    $folders = New-Item -Path "C:\Users\Henry\PowerShellscripts\Project8\May 1$i" -ItemType Directory
    #2. Using For Each Loop to create QuotestoStore.csv inside them 
    foreach($sample in $folders) {
        Add-Content -Path "$sample\QuotestoStore.csv" -Value Invoke-WebRequest 'https://quotes.toscrape.com/'
        Write-Host "$sample saved."
    }
}

错误信息:

Add-Content : A positional parameter cannot be found that accepts argument 
'https://quotes.toscrape.com/'.
At line:6 char:9
+         Add-Content -Path "$sample\QuotestoStore.csv" -Value Invoke-W ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Add-Content], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.AddConten 
   tCommand

**疑问 1:正如我们在(难度 1)中看到的那样,返回 Web 格式代码而不是网页中的值或文本,那么我应该使用哪种语法从网页“https://quotes. toscrape.com/' ??

疑问2:(Add-Content -Path "$sample\QuotestoStore.csv" -Value Invoke-WebRequest 'https://quotes.toscrape.com/')我可以包括(Invoke-WebRequest 'https://quotes.toscrape .com/) 命令在 -Value Parameter??... 如果不是如何创建一个函数并在此处插入,将内容导出到 csv.??

疑问3:我们可以添加一个时间变量($stop = '12:52:30')并创建以下代码:??**

    [datetime] $stop = '12:52:30'

Do{
    for ($i = 1; $i -lt 6; $i++){
        $folders = New-Item -Path "C:\Users\Henry\PowerShellscripts\Project8\May 1$i" -ItemType Directory
        #2. Using For Each Loop to create QuotestoStore.csv inside them 
        foreach($sample in $folders) {
            Add-Content -Path "$sample\QuotestoStore.csv" -Value Invoke-WebRequest 'https://quotes.toscrape.com/'
            Write-Host "$sample saved."
        }
    }
}Until((Get-Date) -ge $stop)

请指导我!

谢谢!!

标签: powershellweb-scraping

解决方案


您是否试图获得如下所示的结果,

您对 csv 格式的 Web 请求的响应

用户下面的代码

$url = "https://quotes.toscrape.com/"

$webrequest = Invoke-WebRequest -Uri $url -SessionVariable websession  -UseBasicParsing


$webrequest | Export-Csv "C:\Test.csv"

如果你想安排这个过程,那么你可以在 powershell 中使用 windows 调度程序(Register-ScheduledTask)。


推荐阅读