-1

为了,我必须:
1)从 txt 文件中获取所有链接

http://example1.htm
http://example2.htm
http://example3.htm
...

2) 从每个链接获取源代码
3) 从源代码获取我的字符串
4) 将字符串导出到 csv

它适用于一个链接。例子:

$topic1 = "kh_header.><b>((?<=)[^<]+(?=</b>))"
$topic2 = "<b>Numer ogłoszenia:\s([^;]+(?=;))"
 Select-String -Path strona1.htm -pattern $topic1 | foreach-object {
 $_.line -match $topic1 > $nul
 $out1 = $matches[1]
 }
 Select-String -Path strona1.htm -pattern $topic2 | foreach-object {
 $_.line -match $topic2 > $nul
 $out2 = $matches[1]
 }
echo $out1';'$out2';' | Set-content out.csv -force

, 但我无法通过 txt 文件中的许多链接获得它。我试试看:

$topic = "kh_header.><b>((?<=)[^<]+(?=</b>))"
$topic2 = "<b>Numer ogłoszenia:\s([^;]+(?=;))"
 $folder = Get-ChildItem e:\sk\html
  ForEach ($htmfile in $folder){
   If ($_.extension -eq ".htm"){
    $htmfile = ForEach-Object  {
            $WC = New-Object net.webclient
            $HTMLCode = $WC.Downloadstring($_.fullname)
            }
       Select-String -Path $HTMLCode -pattern $topic | foreach-object {
       $_.line -match $topic > $nul
       $out1 = $matches[1]
       }    
       Select-String -Path $HTMLCode -pattern $topic2 | foreach-object {
       $_.line -match $topic2 > $nul
       $out2 = $matches[1]
       }      
       echo $out1';'$out2';' | Set-content out.csv -force     
    }
}

我怎么才能得到它?

4

1 回答 1

1

默认情况Select-String下,它只会在任何特定行上找到第一个匹配项。您可以使用该AllMatches参数来修复它,例如:

foo.txt contains: "static void Main(string[] args)"

Select-String foo.txt -pattern '\W([sS]..)' -AllMatches | 
    Foreach {$_.Matches} |
    Foreach {$_.Groups[1].Value}

此外,Select-String 是面向行的,因此它不会跨行找到模式匹配。为了找到这些,您需要将文件作为字符串读取,例如:

$text = [io.file]::readalltext("$pwd\foo.txt")

然后使用一些特殊的正则表达式指令,例如:

$text | Select-String -pattern '(?si)\W([sS]..)' -AllMatches |
        Foreach {$_.Matches} |
        Foreach {$_.Groups[1].Value}
于 2012-12-15T19:55:19.050 回答