“预期声明”在VB脚本中

发布于 2025-01-07 21:39:35 字数 710 浏览 1 评论 0原文

我是 vbscript 新手。我收到错误

预计在 get_html 处声明

我的代码的底部。我实际上试图为变量 get_html 声明一个值(即一个 url)。我该如何解决这个问题?

Module Module1

Sub Main()

End Sub
Sub get_html(ByVal up_http, ByVal down_http)
    Dim xmlhttp : xmlhttp = CreateObject("msxml2.xmlhttp.3.0")
    xmlhttp.open("get", up_http, False)
    xmlhttp.send()

    Dim fso : fso = CreateObject("scripting.filesystemobject")

    Dim newfile : newfile = fso.createtextfile(down_http, True)
    newfile.write(xmlhttp.responseText)

    newfile.close()

    newfile = Nothing
    xmlhttp = Nothing

End Sub
get_html _"http://www.somwwebsite.com", _"c:\downloads\website.html"

End Module

I'm new to vbscript. I get the error

Declaration expected at get_html

At the bottom part of my code. I am actually trying to declare a value (which is a url) for the variable get_html. How can I resolve this?

Module Module1

Sub Main()

End Sub
Sub get_html(ByVal up_http, ByVal down_http)
    Dim xmlhttp : xmlhttp = CreateObject("msxml2.xmlhttp.3.0")
    xmlhttp.open("get", up_http, False)
    xmlhttp.send()

    Dim fso : fso = CreateObject("scripting.filesystemobject")

    Dim newfile : newfile = fso.createtextfile(down_http, True)
    newfile.write(xmlhttp.responseText)

    newfile.close()

    newfile = Nothing
    xmlhttp = Nothing

End Sub
get_html _"http://www.somwwebsite.com", _"c:\downloads\website.html"

End Module

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺孤伤于心 2025-01-14 21:39:36

您可能希望将对 get_html 的调用移至 Main 子例程 (Sub Main()) 内进行调用。例如:

Sub Main()
  get_html _"http://www.somwwebsite.com", _"c:\downloads\website.html"
End Sub

据我所知,您不能直接从模块内进行函数调用。

You probably want to move your call to get_html to be a call from within your Main subroutine (Sub Main()). For example:

Sub Main()
  get_html _"http://www.somwwebsite.com", _"c:\downloads\website.html"
End Sub

AFAIK, you can't make function calls from directly within a module.

羁拥 2025-01-14 21:39:35

存在一些语法错误。

响应可能会以 utf-8 编码形式返回。但 FSO 与 utf-8 并不一致。另一种选择是将响应写入 unicode(将 True 作为第三个参数传递给 创建文本文件)
但输出大小将大于应有的大小。因此我更愿意使用 < strong>Stream 对象。
我修改了你的代码。请考虑。

'Requeired Constants
Const adSaveCreateNotExist = 1 'only creates if not exists
Const adSaveCreateOverWrite = 2 'overwrites or creates if not exists
Const adTypeBinary = 1

Sub get_html(ByVal up_http, ByVal down_http)
    Dim xmlhttp, varBody
    Set xmlhttp = CreateObject("msxml2.xmlhttp.3.0")
        xmlhttp.open "GET", up_http, False
        xmlhttp.send
        varBody = xmlhttp.responseBody
    Set xmlhttp = Nothing
    Dim str
    Set str = CreateObject("Adodb.Stream")
        str.Type = adTypeBinary
        str.Open
        str.Write varBody
        str.SaveToFile down_http, adSaveCreateOverWrite
        str.Close
    Set str = Nothing
End Sub

get_html "http://stackoverflow.com", "c:\downloads\website.html"

There are some syntax mistakes.

  • Module statement is not part of VBScript.
  • Underscores can lead to unexpected results. See http://technet.microsoft.com/en-us/library/ee198844.aspx (search for the word underscore on page)
  • You can't use parentheses when calling a Sub (e.g. xmlhttp.open is a sub, does not return anything). You have two main alternatives to calling a sub routine. sub_proc param1, param2 or Call sub_proc(param1, param2)
  • The assignment operator '=' is not enough for the objects. You should
    use Set statement. It assigns object references to the
    variables.

The response may be return as utf-8 encoded. But however FSO is not at peace with utf-8. Another option is to write the response as unicode (passing True as third parameter to CreateTextFile)
but the output size will be larger than it should be. Therefore I would prefer to use Stream object.
I've revised your code. Please consider.

'Requeired Constants
Const adSaveCreateNotExist = 1 'only creates if not exists
Const adSaveCreateOverWrite = 2 'overwrites or creates if not exists
Const adTypeBinary = 1

Sub get_html(ByVal up_http, ByVal down_http)
    Dim xmlhttp, varBody
    Set xmlhttp = CreateObject("msxml2.xmlhttp.3.0")
        xmlhttp.open "GET", up_http, False
        xmlhttp.send
        varBody = xmlhttp.responseBody
    Set xmlhttp = Nothing
    Dim str
    Set str = CreateObject("Adodb.Stream")
        str.Type = adTypeBinary
        str.Open
        str.Write varBody
        str.SaveToFile down_http, adSaveCreateOverWrite
        str.Close
    Set str = Nothing
End Sub

get_html "http://stackoverflow.com", "c:\downloads\website.html"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文