標題:
請問如何抓取javascript的*.csv檔案?
[打印本頁]
作者:
torrent
時間:
2013-12-27 11:01
標題:
請問如何抓取javascript的*.csv檔案?
想請教一下,我要用批次抓取網頁中的*csv檔,然後把裡面的資料放入excel的表格中,但網頁中的檔案連結是用javascript藏起來,案例如下:
http://prtr.epa.gov.tw/resultEMS.aspx?emsno=A36A0770&tab=Panel5
我打算存放的excel檔已經有管制編號列表,然後就根據這個列表去抓取需要的資料,不過在抓*csv這個地方就卡住了。
謝謝
[attach]17130[/attach]
作者:
stillfish00
時間:
2013-12-27 20:23
回復
1#
torrent
系統忙碌中,請稍後再次查詢。
系統壞了嗎,手動都不能下載。。。
作者:
torrent
時間:
2013-12-28 01:51
好像是耶,我前幾天試還正常。
作者:
GBKEE
時間:
2014-1-1 13:38
回復
3#
torrent
[attach]17176[/attach]
試試看
Option Explicit
Sub Ex()
Dim Sh As Worksheet, Rng As Range
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set Rng = Sheets("Sheet1").Range("A2")
On Error Resume Next
Set Sh = Sheets.Add
With Sh.QueryTables.Add("URL;http://prtr.epa.gov.tw/resultEMS.aspx?emsno=" & Rng & "&tab=Panel5", Sh.[A1])
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = """GridView5"""
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Do While Rng <> ""
With Sh.QueryTables(1)
Rng.Offset(, 1).Range("A1:I1").Value = IIf(Err.Number = 0, Sh.QueryTables(1).ResultRange.Rows(2).Value, "")
Err.Clear
Set Rng = Rng.Offset(1)
.Connection = "URL;http://prtr.epa.gov.tw/resultEMS.aspx?emsno=" & Rng & "&tab=Panel5"
.Refresh BackgroundQuery:=False
End With
Loop
Sh.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
複製代碼
作者:
torrent
時間:
2014-1-2 12:24
太感謝了!
我發現有些表格是多於一筆資料的,所以一個管制編號的資料有可能會有一筆、兩筆甚至10筆,例如這裡:
http://prtr.epa.gov.tw/resultEMS.aspx?emsno=E4901607&tab=Panel5
我爬了一下文並google,本來想用ResultRange.Rows.Count這個指令來算table的列數後,先以Range().EntireRow.insert插入所需要的列數,然後再以ResultRange.Rows(i)加入數據,但怎麼試都是空白,不知道大大有沒有好的辦法?
謝謝
作者:
GBKEE
時間:
2014-1-2 14:08
本帖最後由 GBKEE 於 2014-1-5 06:52 編輯
回復
5#
torrent
試試看
Option Explicit
Sub Ex()
Dim Sh As Worksheet, Rng As Range, Q As Variant
Application.ScreenUpdating = False
Set Rng = Sheets("Sheet1").Range("A2") '管制編號
On Error GoTo ER
With Sheets("管制內容")
Set Sh = Sheets(.Name)
.UsedRange = ""
End With
On Error Resume Next
With Sh.QueryTables.Add("URL;http://prtr.epa.gov.tw/resultEMS.aspx?emsno=" & Rng & "&tab=Panel5", Sh.[AA1])
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = """GridView5"""
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Set Q = Sh.QueryTables(1)
Do While Rng <> ""
If Err = 0 And Application.Count(Q.ResultRange) > 0 Then
With Sh.Cells(Sh.Rows.Count, 2).End(xlUp)
.Offset(1, -1) = Rng
If .Row = 1 Then
.Offset(, -1) = "管制編號"
Q.ResultRange.Copy .Cells
Else
Q.ResultRange.Rows("2:" & Q.ResultRange.Rows.Count).Copy .Offset(1)
End If
End With
End If
Err.Clear
Set Rng = Rng.Offset(1)
Q.Connection = "URL;http://prtr.epa.gov.tw/resultEMS.aspx?emsno=" & Rng & "&tab=Panel5"
Q.Refresh BackgroundQuery:=False
Loop
Q.ResultRange = ""
With Sh
.Columns.AutoFit
For Each Q In .Names
Q.Delete
Next
For Each Q In .QueryTables
Q.Delete
Next
End With
Application.ScreenUpdating = True
Exit Sub
ER:
If Err.Number = 9 Then
Sheets.Add.Name = "管制內容"
Resume
End If
End Sub
複製代碼
作者:
torrent
時間:
2014-1-4 22:47
太感激了,我這幾天修正了一些裡面的程式碼,讓它也可以抓別的資源。非常感謝!
順便問一下,這裡不抓csv而是抓網頁的table,是因為csv中文進來是亂碼而又無解的原因嗎?
作者:
GBKEE
時間:
2014-1-5 06:54
回復
7#
torrent
這網頁csv不是無法下載嗎!,
作者:
torrent
時間:
2014-1-5 13:28
不好意思,我是因為後來要到環保署另一個opendata網站抓csv的時候發現抓進工作表都會變成亂碼,所以才聯想到。
http://opendata.epa.gov.tw/Data/Contents/EMS/
這個網站和之前那個第一樓的網站應該是通的,但這裡csv就直接提供所有單位的管制編號,但一次提供1000筆,所以總共7萬多筆要下載71次csv檔案。
雖然我只是要最重要的管制編號,但其它都亂碼還是覺得很怪,以下是我的code,我還是初學者,用最簡單的do/loop來處理迴圈,跑到一半就卡住了,不知道出了什麼事情,多謝!
Sub csv()
Dim i As Integer, k As Integer, emsUrl As String
Set i = 0
Set k = 1000
emsUrl = "http://opendata.epa.gov.tw/ws/Data/EMS/?$orderby=RegistrationNo&$skip=" & i & "&$top=" & k & "&format=csv"
With ActiveSheet.QueryTables.Add(Connection:="URL;" & emsUrl, Destination:=Range("A2"))
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.RefreshPeriod = 0
.AdjustColumnWidth = False
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
End With
End Sub
複製代碼
作者:
torrent
時間:
2014-1-5 13:36
抱歉,剛剛貼錯code了,但已經不能編輯:
Sub csv()
Dim i As Integer, k As Integer, emsUrl As String, Rng As Range
i = 1
k = 1000
Do Until k = 71000
Set Rng = Sheets("Sheet1").Range("A" & i & "")
emsUrl = "http://opendata.epa.gov.tw/ws/Data/EMS/?$orderby=RegistrationNo&$skip=" & i & "&$top=" & k & "&format=csv"
With ActiveSheet.QueryTables.Add(Connection:="URL;" & emsUrl, Destination:=Rng)
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.RefreshPeriod = 0
.AdjustColumnWidth = False
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
End With
i = i + 1000
k = k + 1000
Loop
End Sub
複製代碼
作者:
GBKEE
時間:
2014-1-5 17:02
回復
10#
torrent
試試看
Option Explicit
Sub Ex() '全部資料超過 65536筆 2003版不適用
Dim Sh As Worksheet, wb As Workbook, i As Long
Set Sh = ActiveWorkbook.Sheets(1)
Sh.UsedRange = ""
i = 0
Do
Workbooks.OpenText Filename:="http://opendata.epa.gov.tw/ws/Data/EMS/?$orderby=RegistrationNo&$skip=" & i & "&$top=1000&format=csv" _
, Origin:=-535, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), _
Array(9, 1), Array(10, 1), Array(11, 1)), TrailingMinusNumbers:=True
With ActiveWorkbook.Sheets(1).UsedRange
If .Cells(1) = "" Then Exit Do
If i = 0 Then
.Copy Sh.[a1]
Else
.Offset(1).Copy Sh.[a1].End(xlDown).Offset(1)
End If
ActiveWorkbook.Close False
End With
i = i + 1000
Loop
ActiveWorkbook.Close False
End Sub
複製代碼
作者:
torrent
時間:
2014-1-6 05:18
回復
11#
GBKEE
受教了,原來要用Workbooks。
另外,我在GBKEE大大幫我修正的第二個code中做了一些修正,目的是把A欄的管制編號填滿,我在第31列加了這一行:
.Resize(Q.ResultRange.Rows.Count, 1).Offset(2, -1).Value = Rng
看起來除了最後一個管制編號會多兩行尾巴之外,好像沒有其它的問題,不知道各位有沒有更好的意見或看出這樣搞會有bug?
謝謝
[attach]17199[/attach]
Sub punish()
Dim Sh As Worksheet, Rng As Range, Q As Variant
Application.ScreenUpdating = False
Set Rng = Sheets("Sheet1").Range("A2") '管制編號
On Error GoTo ER
With Sheets("管制內容")
Set Sh = Sheets(.Name)
.UsedRange = ""
End With
On Error Resume Next
With Sh.QueryTables.Add("URL;http://prtr.epa.gov.tw/resultEMS.aspx?emsno=" & Rng & "&tab=Panel5", Sh.[AA1])
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = """GridView5"""
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Set Q = Sh.QueryTables(1)
Do While Rng <> ""
If Err = 0 And Application.Count(Q.ResultRange) > 0 Then
With Sh.Cells(Sh.Rows.Count, 2).End(xlUp)
.Offset(1, -1) = Rng
If .Row = 1 Then
.Offset(, -1) = "管制編號"
Q.ResultRange.Copy .Cells
Else
.Resize(Q.ResultRange.Rows.Count, 1).Offset(2, -1).Value = Rng
Q.ResultRange.Rows("2:" & Q.ResultRange.Rows.Count).Copy .Offset(1)
End If
End With
End If
Err.Clear
Set Rng = Rng.Offset(1)
Q.Connection = "URL;http://prtr.epa.gov.tw/resultEMS.aspx?emsno=" & Rng & "&tab=Panel5"
Q.Refresh BackgroundQuery:=False
Loop
Q.ResultRange = ""
With Sh
.Columns.AutoFit
For Each Q In .Names
Q.Delete
Next
For Each Q In .QueryTables
Q.Delete
Next
End With
Application.ScreenUpdating = True
Exit Sub
ER:
If Err.Number = 9 Then
Sheets.Add.Name = "管制內容"
Resume
End If
End Sub
複製代碼
作者:
GBKEE
時間:
2014-1-6 07:56
回復
12#
torrent
'.Resize(Q.ResultRange.Rows.Count, 1).Offset(2, -1).Value = Rng
.Resize(Q.ResultRange.Rows.Count - 1, 1).Offset(1, -1).Value = Rng
'Q.ResultRange.Rows.Count - 1 ->不包含表頭的列數
複製代碼
作者:
torrent
時間:
2014-1-6 10:13
回復
13#
GBKEE
多謝,這樣跑出來的結果沒有問題了。
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)