在网页上保存文字(1000页)

时间:2017-10-06 17:52:40

标签: batch-file web text web-scraping mining

我有1000个网址。我需要一个工具来获取我的URL并导出这些页面上显示的所有文本。 我需要在网页上显示a的文本,而不是背景HTML代码。

你知道任何软件或方法吗?

1 个答案:

答案 0 :(得分:2)

将其保存为bat文件(即innerTextGet.bat):

@if (@X)==(@Y) @end /* JScript comment 
        @echo off 

        cscript //E:JScript //nologo "%~f0" %* 
        ::pause
        exit /b %errorlevel% 

@if (@X)==(@Y) @end JScript comment */ 


var link=WScript.Arguments.Item(0);
var saveTo=WScript.Arguments.Item(1);


var IE = new ActiveXObject("InternetExplorer.Application"); 
IE.Visible=false;
IE.Navigate2(link);

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

var counter=0;
while (IE.Busy && counter<60*60*10) {
    //WScript.Echo(IE.Busy);
    sleep(1000);
    counter++;
}

if(IE.Busy){
    WScript.Echo("Cant wait 4ever");
    WScript.Quit(10);
}

function writeContent(file,content) {
        var ado = WScript.CreateObject("ADODB.Stream");
        ado.Type = 2;  // adTypeText = 2
        ado.CharSet = "iso-8859-1";  // right code page for output (no adjustments)
        //ado.Mode=2;
        ado.Open();

        ado.WriteText(content);
        ado.SaveToFile(file, 2);
        ado.Close();    
}

var innerText=IE.document.body.innerText;
IE.Quit();
writeContent(saveTo,innerText);

并使用它:

call innerTextGet.bat "https://stackoverflow.com/questions/46611374/save-texts-on-webpages-1000-pages"  result.txt

它不是故障安全 - 如果参数被核心传递等,则不检查结果文件是否已存在,但它至少起作用。它再次使用{strong> @omegastripes 提出的innerText对象的InternetExplorer.Application属性,虽然我提供了jscript,因为它更容易插入批处理文件。

由于您没有提供有关链接存储位置的信息,我假设您知道如何阅读和迭代它们。