powershell - 如何构建一个可以定期从 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)
请指导我!
谢谢!!
解决方案
推荐阅读
- celery - 气流网络服务器没有名为“airflow.www.fab_security”的模块
- tensorflow - tensorflow - 无效参数:输入大小应该匹配,但它们相差 2
- javascript - 为什么我的网站子网址在提交表单时不起作用,但在我手动输入时起作用
- python-3.x - 第一次尝试在folium中使用等值线图,索引错误
- node.js - 从 github 获取此代码,尝试运行它并抛出此错误,据我所知,错误在 index.js 中,我在此处提供了 index.js 代码
- c++ - Cmake编译器未知
- javascript - Discord.js 机器人在尝试发送命令后崩溃
- database - 功能依赖,这是正确的问题吗
- php - 打开购物车一个博客页面“模板”自动出现在网页视图中(主题广场)
- python - 烧瓶闪烁的消息未显示