|
原来发表在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 & "£º" & Mid(ch, InStr(ch, "¡¶"), InStrRev(ch, ")") - InStr(ch, "¡¶") + 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><!-- ÕýÎÄÒ³»­Öл­ begin --><!-- ÕýÎÄÒ³»­Öл­ begin --><p>", vbCrLf)
ch = Replace(ch, "<!-- ÕýÎÄÒ³»­Öл­ begin --><!-- ÕýÎÄÒ³»­Öл­ 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编辑过]
|
|