有没有办法循环遍历Powershell中的publicsuffix列表?

问题描述 投票:0回答:2

我正在尝试测试一个Web过滤解决方案,所以我有一个powershell循环遍历URL列表并返回webresponse。问题是,您经常会遇到可能未经授权的403或404的cdns或其他网站,您需要找到根域。

我发现的唯一合理的解决方案是将其与publicsuffix列表交叉引用。从我见过的唯一一种语言不能很好地运行PowerShell。我想知道是否有人遇到过此问题或有解决方案。

powershell public-suffix-list
2个回答
1
投票

虽然your solution有效,但还有一种更简洁,更快速的替代方案:

$url = 'https://publicsuffix.org/list/public_suffix_list.dat'
(Invoke-RestMethod $url) -split "`n" -match '^[^/\s]' |
  Set-Content .\public_suffix_list.dat
  • Invoke-RestMethod $url将指定URL处的文本文件作为单个字符串返回。
  • -split "`n"将字符串拆分为一系列行
  • -match '^[^/\s]'匹配那些以(^)字符开头的行(来自[...]中包含的集合)不是(^)文字/而不是空白字符(/s),它有效地过滤掉评论/(假设的)非数据线。

上面将数据行专用数组保存到文件中,就像在解决方案中一样。

请注意,确定给定的URL是否具有公共后缀不仅仅涉及与数据行的后缀匹配,因为后者具有通配符标签(*)并涉及异常(以!开头的行) - 请参阅https://publicsuffix.org/list/


0
投票
# You can use whatever directory
$workingdirectory = "C:\"

# Downloads the public suffix list
Invoke-WebRequest -Uri "https://publicsuffix.org/list/public_suffix_list.dat" -OutFile "$workingdirectory\public_suffix_list.dat"

# Gets the content of the file, removes the empty spaces, removes all the
# comments that has // and outputs it to a file
(gc $workingdirectory\public_suffix_list.dat) |
    ? { $_.Trim() -ne "" } |
    Select-String -Pattern "//" -NotMatch |
    Set-Content "$workingdirectory\public_suffix_list.dat"
© www.soinside.com 2019 - 2024. All rights reserved.