怎样用powershell获取一个网页的ClassName为'comment-content'的全部内容
时间: 2024-01-22 07:18:46 浏览: 25
你可以使用 `Invoke-WebRequest` cmdlet 来获取网页内容,然后使用 `Select-Object` cmdlet 和 `-ExpandProperty` 参数来获取 ClassName 为 'comment-content' 的所有内容。
以下是示例代码:
```powershell
$url = "网页的URL"
$response = Invoke-WebRequest -Uri $url
$content = $response.AllElements | Where-Object { $_.ClassName -eq "comment-content" } | Select-Object -ExpandProperty InnerText
```
其中,`$url` 是要获取内容的网页 URL,`$response` 是 `Invoke-WebRequest` 返回的响应对象,`$content` 是获取到的 ClassName 为 'comment-content' 的全部内容。
相关问题
$url = 'https://book.douban.com/subject/1023045/comments/' $ie = Invoke-WebRequest -URI $url $data = $ie.ParsedHtml.getElementsByClassName('comment-content') $data|%{$_.outerText} 上面的可以获取到ClassName -eq "comment-content"内容 为什么下面的获取不到ClassName -eq "comment-content"内容 $response = Invoke-WebRequest -Uri $url $content = $response.AllElements | Where-Object { $_.ClassName -eq "comment-content" } | Select-Object -ExpandProperty InnerText
可能是因为 `$response.AllElements` 返回的是页面上所有 HTML 元素,包括内嵌的、脚本生成的、隐藏的等等,而不仅仅是评论内容。尝试使用以下代码来筛选评论内容:
```powershell
$response = Invoke-WebRequest -Uri $url
$data = $response.ParsedHtml.getElementsByClassName('comment-content') | ForEach-Object { $_.outerText }
```
这样应该可以获取到所有类名为 `comment-content` 的元素的文本内容了。如果还有问题,请告诉我。
powershell 使用add-content 添加新内容的时候如何让他换行,新家到新的一行
在使用 `Add-Content` 命令添加新内容时,默认情况下会在原有内容的末尾添加新内容,不会换行。如果你想要在新内容的前面添加一个空行或者换行符,可以在 `<NewLine>` 参数中使用特殊字符 `n` 来表示换行。例如,以下命令将在文件 `example.txt` 中添加一个新行并换行:
```
Add-Content -Path example.txt -Value "`nNew content on a new line"
```
在这个命令中,`"`n"` 表示换行符,`New content on a new line` 将会在新的一行中添加到文件中。注意特殊字符 `n` 必须使用双引号括起来,否则会被当作普通字符处理。