2016-08-12 2 views
0

Я пытаюсь создать макрос, который очищает и импортирует таблицы из web page, . Более конкретно, я хочу получить две таблицы tables pointed by arrows, please ignore the text in the table if it doesn't make sense, I translated using google. Эти таблицы обновляются автоматически, поэтому я использовал подход IE (by @ron), не очищал данные. Я измучен, может кто-нибудь мне помочь? Я новичок vba, ценю любую помощь.vba scrape нестатические данные веб-таблицы

Sub test() 
    ' open IE, navigate to the website of interest and loop until fully loaded 
     Set IE = CreateObject("InternetExplorer.Application") 
     my_url = "http://www.neeq.com.cn/static/statisticdata.html" 

    With IE 
     .Visible = False 
     .navigate my_url 
     .Top = 50 
     .Left = 530 
     .Height = 400 
     .Width = 400 

    Do Until Not IE.busy And IE.ReadyState = 4 
     DoEvents 
    Loop 

    End With 

    Set tbl = IE.Document.getElementsByTagName("table") 
    For Each itm In tbl 
     i = 1 
     For Each itm2 In itm.Rows 
      For Each cell In itm2.Cells 
       ActiveSheet.Cells(i, 2) = cell.innerText 
       i = i + 1 
      Next 
     Next 
    Next 
    end sub() 
+0

End Sub() неправильна – cutzero

+0

s orry, это опечатка. – Michael

ответ

0

Вы должны выбрать "TR" из вашего объекта таблицы для каждого Ряды Как вы используете IE, не Statix ​​должен быть возврат (JavaScript выполняется)

i = 1 
For Each itm2 In tbl.getElementsByTagName("tr") 

тест с Debug.Print

вы выбираете HTML затем таблицы -> TR -> TD

Sub test() 
    ' open IE, navigate to the website of interest and loop until fully loaded 
     Set IE = CreateObject("InternetExplorer.Application") 
     my_url = "http://www.neeq.com.cn/static/statisticdata.html" 

    With IE 
     .Visible = False 
     .navigate my_url 
     .Top = 50 
     .Left = 530 
     .Height = 400 
     .Width = 400 

    Do Until Not IE.busy And IE.ReadyState = 4 
     DoEvents 
    Loop 

    End With 

    Set tbl = IE.Document.getElementsByTagName("table") 
    For Each itm In tbl 
     i = 1 
     For Each itm2 In itm.getElementsByTagName('tr') 
      For Each cell In itm2.getElementsByTagName('td') 
       ActiveSheet.Cells(i, 2) = cell.innerText 
       i = i + 1 
      Next 
     Next 
    Next 
    end sub 
Смежные вопросы