如何使用批处理脚本读取网站内容? [英] How to read the content of a website using batch script?

查看:289
本文介绍了如何使用批处理脚本读取网站内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需求,比如需要从网站上获取很少的内容,我必须使用批处理脚本将其保存在.txt文件中.

I got a requirement like, need to get few content from a website and i have to save that in a .txt file using batch scripting.

@echo off
  echo.>"D:\Jai\dblank.txt"

通过使用上面的批处理代码,我只能在此位置创建文件,而我想在该.txt文件中提供浏览器内容.我对批处理脚本非常陌生.

by using above batch code i can only create a file in a location along with this i want to give the browser content in that .txt file. I am very new to batch scripting.

推荐答案

Set Arg = WScript.Arguments
set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout

if LCase(Arg(0)) = "web" or LCase(Arg(0)) = "http" then
    HttpGet
Elseif LCase(Arg(0)) = "remhtml" or LCase(Arg(0)) = "tags" then
    RemoveHTMLTags
End If


Sub HttpGet
On Error Resume Next
    Set File = WScript.CreateObject("Microsoft.XMLHTTP")
    File.Open "GET", Arg(1), False
    File.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 1.1.4322; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C; .NET4.0E; BCD2000; BCD2000)"
    File.Send
    txt=File.ResponseText
    'Putting in line endings
    Outp.write txt
    If err.number <> 0 then 
        Outp.writeline "" 
        Outp.writeline "Error getting file" 
        Outp.writeline "==================" 
        Outp.writeline "" 
        Outp.writeline "Error " & err.number & "(0x" & hex(err.number) & ") " & err.description 
        Outp.writeline "Source " & err.source 
        Outp.writeline "" 
        Outp.writeline "HTTP Error " & File.Status & " " & File.StatusText
        Outp.writeline  File.getAllResponseHeaders
        Outp.writeline LCase(Arg(1))
    End If
End Sub

Sub RemoveHTMLTags
    Set ie = CreateObject("InternetExplorer.Application") 
    ie.Visible = 0
    ie.Silent = 1 
    ie.Navigate2 "file://" & FilterPath & "Filter.html"
    Do 
        wscript.sleep 50            
    Loop Until ie.document.readystate = "complete"
    ie.document.body.innerhtml = Inp.readall
    Outp.write ie.document.body.innertext
'   ie.quit
End Sub

要使用

常规用途

过滤器用于命令提示符. Filter.vbs必须与cscript.exe一起运行.如果您只键入filter,它将运行一个批处理文件,该文件将自动执行此操作.

Filter is for use in a command prompt. Filter.vbs must be run with cscript.exe. If you just type filter it will run a batch file that will do this automatically.

filter subcommand [parameters]

过滤器只能读写标准输入和标准输出.这些仅在命令提示符下可用.

Filter reads and writes standard in and standard out only. These are only available in a command prompt.

filter <inputfile >outputfile
filter <inputfile | other_command
other_command | filter >outputfile
other_command | filter | other_command

网络

filter web webaddress
filter ip webaddress

从网络上检索文件并将其写到标准输出中.

Retrieves a file from the web and writes it to standard out.

webaddress - a web address fully specified including http://

示例

获取Microsoft主页

Gets Microsoft's home page

cscript //nologo filter.vbs web http://www.microsoft.com

标签

filter tags

从文本中删除HTML标签.

Removes HTML tags from text.

示例

cscript //nologo filter.vbs web http://www.microsoft.com | cscript //nologo filter.vbs tags

这篇关于如何使用批处理脚本读取网站内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆