如何用inet下载网页源代码

本文展示了如何在VBA中利用Microsoft Internet Transfer Control 6.0(Inet)下载网页源代码并将其保存为文件,同时演示了下载图片到本地并显示在Picture1控件中的步骤。在`Command1_Click`事件中,首先通过`Inet1.OpenURL`获取网页源代码,然后写入到本地文件。对于下载图片,同样使用`Inet1.OpenURL`,但接收返回的二进制数据,并保存为本地图片文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

'部件里添加 Microsoft Internet Transfer Control 6.0

 

'1.下载网页

 Private Sub Command1_Click()
    Dim s As String
    Inet1.Cancel
    Inet1.protocol = icHTTP
    Inet1.url = "http://www.baidu.com"
    s = Inet1.OpenURL
    If Len(s) = 0 Then Exit Sub
    Open "d:/aaa.htm" For Output As #1
    Print #1, s
    Close #1
    MsgBox "ok"
End Sub

 

'2.下载图片

Private Sub Command1_Click()
    Dim myData() As Byte, i&, myURL$, RealFile$
    myURL = "http://www.baidu.com/img/baidu_logo.gif"
    If Inet1.StillExecuting = True Then Exit Sub
    myData() = Inet1.OpenURL(myURL, icByteArray)
    For i = Len(myURL) To 1 Step -1
        If Left$(Right$(myURL, i), 1) = "/" Then RealFile$ = Right$(myURL, i - 1)
    Next
   
    Open "d:/1.gif" For Binary Access Write As #1
    Put #1, , myData()
    Close #1
    Picture1.Picture = LoadPicture("d:/1.gif")
End Sub

Private Sub Form_Load()
    Picture1.AutoSize = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If Dir("d:/1.gif") <> "" Then Kill "d:/1.gif"
End Sub

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值