设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 4214|回复: 5
打印 上一主题 下一主题

[原创]关于上网“摘书”的问题

[复制链接]
跳转到指定楼层
1#
发表于 2005-6-20 19:26:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
原来发表在EXCELHOME上,但近来访问不到,到这里来与大家分享。

Private Declare Function URLDownloadToFile Lib "urlmon" Alias _

                                           "URLDownloadToFileA" ( _

                                           ByVal pCaller As Long, ByVal szURL As String, _

                                           ByVal szFileName As String, _

                                           ByVal dwReserved As Long, _

                                           ByVal lpfnCB As Long) As Long

Sub DownloadFileFromWeb()

Dim strSavePath As String

Dim returnValue As Long

Dim mm As Integer

Dim dd As Integer

Dim mmmm As String

Dim dddd As String

Dim Fn As String

Dim strUrl As String

For mm = 1 To 179

   

    Fn = mm

    strUrl = "http://book.sina.com.cn/longbook/tech/1100774586_zengguofanshengqian/" & mm & ".shtml"

    strSavePath = ThisWorkbook.Path & "\" & Fn & ".shtml"

        If Dir(strSavePath) = "" Then

        returnValue = URLDownloadToFile(0, strUrl, strSavePath, 0, 0)

        If returnValue = 0 Then Cells(mm, 1) = Fn

        End If

Next mm

MsgBox "Sccess!"

End Sub



Sub ReadTextFile()

    Dim Ffm, ffM2, RDLine, ch, fnj

    Dim fs, fs2, f, f2, i, j

    Const ForReading = 1, ForWriting = 2, ForAppending = 3

   

    j = 1

   

    ffM2 = ThisWorkbook.Path & "/Ôø¹ú·ªµÄÉýǨ֮·.txt"

    'ffM2 = ThisWorkbook.Path & "Ðþ»ú£º¹Ù³¡ºìÈËÃØóÅ.txt"

    Set fs2 = CreateObject("Scripting.FileSystemObject")

    Set f2 = fs2.OpenTextFile(ffM2, ForWriting, True)

   

    For fnj = 1 To 179

     

    Ffm = ThisWorkbook.Path & "/" & fnj & ".shtml"

   

    i = 1

    Set fs = CreateObject("Scripting.FileSystemObject")

    Set f = fs.OpenTextFile(Ffm, ForReading, 0)

   

   

   

    Do While f.AtEndOfStream <> True

    ch = f.readline

    i = i + 1

         If i = 379 Then

         Cells(j, 1).Value = fnj & "&pound;&ordm;" & Mid(ch, InStr(ch, "&iexcl;&para;"), InStrRev(ch, ")") - InStr(ch, "&iexcl;&para;") + 1)

         f2.writeline Cells(j, 1).Value

         

         End If

         

         If i = 390 Then

         Cells(j, 2).Value = ch

         f2.writeline "-----------------------------------------"

         End If

        

        If i = 414 Then

        ch = Cells(j, 2).Value & ch

        ch = Mid(ch, InStr(ch, "<p>") + 3, Len(ch) - InStr(ch, "<p>"))

        ch = Replace(ch, "</p><p>", vbCrLf)

        ch = Replace(ch, "</p><!-- &Otilde;&yacute;&Icirc;&Auml;&Ograve;&sup3;&raquo;&shy;&Ouml;&ETH;&raquo;&shy; begin --><!-- &Otilde;&yacute;&Icirc;&Auml;&Ograve;&sup3;&raquo;&shy;&Ouml;&ETH;&raquo;&shy; begin --><p>", vbCrLf)

        ch = Replace(ch, "<!-- &Otilde;&yacute;&Icirc;&Auml;&Ograve;&sup3;&raquo;&shy;&Ouml;&ETH;&raquo;&shy; begin --><!-- &Otilde;&yacute;&Icirc;&Auml;&Ograve;&sup3;&raquo;&shy;&Ouml;&ETH;&raquo;&shy; begin -->", "")

        ch = Left(ch, InStr(ch, "<a href") - 3) & vbCrLf & vbCrLf & vbCrLf

        Cells(j, 2) = ch

        

        f2.writeline ch

        End If

       Loop

    f.Close

    'f2.Close

    Debug.Print j

   j = j + 1

Next fnj

f2.Close

End Sub





代码主要是下载网页,鉴别网页内的正文内容,取出真正的所需要的TXT文件即可。

[此贴子已经被作者于2005-6-20 11:30:20编辑过]

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
 楼主| 发表于 2005-6-20 19:29:00 | 只看该作者
后来,察看IE.document,发现更简单的办法:其innerText就直接有TXT文本,截取之,即可。

    Const sURI As String = "http://book.sina.com.cn/longbook/tech/1100774586_zengguofanshengqian/88.shtml"

    Dim ie As Object

    Dim strDoc As Object

   Set ie = CreateObject("internetexplorer.application")

    ie.Navigate sURI

    Set strDoc = ie.Document

        Debug.Print strDoc.body.innerText

        Debug.Print strDoc.body.innerHTML

3#
 楼主| 发表于 2005-6-20 19:32:00 | 只看该作者
这样,并不用下载网页,直接处理提取出的文本,剔出无用的广告类文本,更简单了。
4#
发表于 2005-6-21 16:35:00 | 只看该作者
不错,不错,收藏起来研究一下
5#
发表于 2005-6-21 16:45:00 | 只看该作者
以下是引用chenwintek在2005-6-20 11:29:00的发言:



后来,察看IE.document,发现更简单的办法:其innerText就直接有TXT文本,截取之,即可。



    Const sURI As String = "http://book.sina.com.cn/longbook/tech/1100774586_zengguofanshengqian/88.shtml"

    Dim ie As Object

    Dim strDoc As Object

   Set ie = CreateObject("internetexplorer.application")

    ie.Navigate sURI

    Set strDoc = ie.Document

        Debug.Print strDoc.body.innerText

        Debug.Print strDoc.body.innerHTML

用此方法提示:方法‘document’作用于对象‘IWebBrowser2’时失败



  
6#
发表于 2005-6-24 00:49:00 | 只看该作者
这么好的代码我该怎么用?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-14 17:39 , Processed in 0.090059 second(s), 29 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表