VBA выскабливание - PullRequest
       8

VBA выскабливание

0 голосов
/ 28 января 2019

Ниже приведен html / javascript код: я хочу очистить идентификатор в теге tr по клику.Как я могу очистить тег onClick и просто получить идентификатор в конце?Код ниже предназначен для динамической защищенной таблицы веб-сайта, в которой все строки имеют гиперссылки и отсортированы по нечетному и четному классу, как вы увидите ниже.Я признателен за любую помощь, спасибо!

<tr class='odd' onmouseover='mov(this)' onMouseOut="mou(this, 'odd')" onClick='location.href="custlist.php?rptname=Report&custm_id=**13857**"'> 
<td align='right'>&nbsp;1.&nbsp;</td>
<td nowrap style='text-align:;'>&nbsp;Company&nbsp;</td>
<td nowrap style='text-align:;'>&nbsp;blah blah&nbsp;</td>
<td nowrap style='text-align:;'>&nbsp; bbb&nbsp;</td>
<td nowrap style='text-align:right;'>&nbsp;1,084,771.10&nbsp;</td>
<td nowrap style='text-align:right;'>&nbsp;1,060,787.10&nbsp;</td>
<td nowrap style='text-align:right;'>&nbsp;1,203,000.00&nbsp;</td>
<td nowrap style='text-align:right;'>&nbsp;30,233.90&nbsp;</td>
<td nowrap style='text-align:left;'>&nbsp;&nbsp;</td>
</tr>
<tr  class='even'  onmouseover='mov(this)' onMouseOut="mou(this, 'even')" onClick='location.href="custlist.php?rptname=report&custm_id=22012"'>
<td align='right'>&nbsp;2.&nbsp;</td>
<td nowrap style='text-align:;'>&nbsp;T3 bbhj &nbsp;</td>
<td nowrap style='text-align:;'>&nbsp;hhht &nbsp;</td>
<td nowrap style='text-align:right;'>&nbsp;720,260.00&nbsp;</td>


<tr>

* После 25 строк в нем указан код переноса:

            <tr class='header'>

  <th width='20px'>&nbsp;</th>


      <th nowrap>&nbsp;



          <a href='/fats/custlist.php?srid=4969&sort=1&desc=ASC&perpage=ALL' class='sorter'>
              CUSTOMER</a>
          &nbsp;


      </th>


      <th nowrap>&nbsp;



          <a href='/fats/custlist.php?srid=4969&sort=2&desc=ASC&perpage=ALL' class='sorter'>
              CUSTOMER AGENT</a>
          &nbsp;


      </th>


      <th nowrap>&nbsp;



          <a href='/fats/custlist.php?srid=4969&sort=3&desc=ASC&perpage=ALL' class='sorter'>
              ACCT MANAGERS</a>
          &nbsp;


      </th>


      <th nowrap>&nbsp;



          <a href='/fats/custlist.php?srid=4969&sort=4&desc=ASC&perpage=ALL' class='sorter'>
              TOTAL</a>
          &nbsp;<img src='images/up_arrow.gif'>&nbsp;


      </th>


      <th nowrap>&nbsp;



          <a href='/fats/custlist.php?srid=4969&sort=5&desc=ASC&perpage=ALL' class='sorter'>
              BALANCE</a>
          &nbsp;


      </th>


      <th nowrap>&nbsp;



          <a href='/fats/custlist.php?srid=4969&sort=6&desc=ASC&perpage=ALL' class='sorter'>
              CREDIT LIMIT</a>
          &nbsp;


      </th>


      <th nowrap>&nbsp;



          <a href='/fats/custlist.php?srid=4969&sort=7&desc=ASC&perpage=ALL' class='sorter'>
              CREDIT AVAILABLE</a>
          &nbsp;


      </th>


      <th nowrap>&nbsp;



          <a href='/fats/custlist.php?srid=4969&sort=8&desc=ASC&perpage=ALL' class='sorter'>
              NOTES</a>
          &nbsp;


      </th>

Мой VBA-код на данный момент:

Sub GetCreditLimit()



Dim ieApp As Object
Dim ieDoc As Object
Dim ieTable As Object
Dim clip As DataObject


Dim td As Object
Dim tr As Object

Dim objTbl As Object
Dim htmlTR As MSHTML.IHTMLElementCollection
Dim htmlTD As MSHTML.IHTMLElementCollection

Dim i As Integer
Dim Links As Object
Dim objElement As HTMLObjectElement
Dim n As Integer
Dim elems As Object
Dim e As Object




'create a new instance of ie
Set ieApp = New InternetExplorer

'you don’t need this, but it’s good for debugging
ieApp.Visible = True



'assume we’re not logged in and just go directly to the login page
ieApp.Navigate "https://brokerage.suntecktts.com/agents/login"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

Set ieDoc = ieApp.Document

'fill in the login form – View Source from your browser to get the control names
With ieDoc.forms(0)
.agent_login.Value = "username"
.agent_password.Value = "password"
.submit
End With
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

'now that we’re in, go to the page we want
ieApp.Navigate "https://brokerage.suntecktts.com/fats/custlist.php?srid=8897&page=1&perpage=ALL"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

'get the table based on the table's id
Set ieDoc = ieApp.Document
Set ieTable = ieDoc.all.Item("general-report-wrapper")



'copy the tables html to the clipboard and paste to teh sheet
If Not ieTable Is Nothing Then
Set clip = New DataObject
clip.SetText "<html>" & ieTable.outerHTML & "</html>"
clip.PutInClipboard

ActiveSheet.Range("A1").Select
ActiveSheet.PasteSpecial "Unicode Text"
Rows("1:1").Select

End If

With ieApp
Set ieDoc = ieApp.Document
End With
Do While ieApp.Busy Or Not ieApp.ReadyState = READYSTATE_COMPLETE
        DoEvents
    Loop

Set objTbl = ieDoc.getElementById("report name")
Set htmlTR = objTbl.getElementsByTagName("tr")
Set htmlTD = htmlTR.getElementsByTagName("td")
Set td = ieDoc.getElementByTagName("td")

    For Each tr In htmlTD
      ActiveSheet.Range("J2" & Rows.count).Value = tr.onclick
            i = i + 1


    Next tr






ieApp.Quit
Set ieApp = Nothing
'close 'er up

End Sub

Ответы [ 2 ]

0 голосов
/ 31 января 2019

Я понял это ... см. Код ниже:

Sub GetCredit()

Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim ieTable As Object
Dim clip As DataObject
Dim internetdata As Object
Dim ieResult As Object
Dim header_links As Object
Dim link As Object
Dim ieTag As Object
Dim b As Object
Dim td As Object
Dim tr As Object
Dim NewURL As String
Dim iRow As Integer


    Set ieApp = New InternetExplorer
    ieApp.Visible = True
    ieApp.Navigate "https://brokerage.suntecktts.com/agents/login"
    Do While ieApp.Busy: DoEvents: Loop
    Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

    Set ieDoc = ieApp.Document

    'fill in the login form – View Source from your browser to get the control names
    With ieDoc.forms(0)
        .agent_login.Value = "username" 'id
        .agent_password.Value = "password" 'password
        .submit
    End With
    Do While ieApp.Busy: DoEvents: Loop
    Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

    ieApp.Navigate "https://brokerage.suntecktts.com/fats/custlist.php?srid=3691&page=1&perpage=ALL"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

'get the table based on the table's id
Set ieDoc = ieApp.Document
Set ieTable = ieDoc.all.Item("general-report-wrapper")



'copy the tables html to the clipboard and paste to teh sheet
If Not ieTable Is Nothing Then
Set clip = New DataObject
clip.SetText "<html>" & ieTable.outerHTML & "</html>"
clip.PutInClipboard

ActiveSheet.Range("A1").Select
ActiveSheet.PasteSpecial "Unicode Text"
Rows("1:1").Select

End If

Dim y As Integer



Dim LR As Long
LR = ActiveSheet.UsedRange.Rows.count
Range("J2").AutoFill Destination:=Range("J2:J" & LR)



With ieApp
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

Set ieDoc = ieApp.Document

Dim ids As Object, i As Long, val As String
Set ids = ieDoc.querySelectorAll("tr[onClick*='custm_id']")
Debug.Print ids



For i = 0 To ids.Length - 1
val = ids.Item(i).getAttribute("onclick")
Debug.Print val
Debug.Print Split(Split(val, "custm_id=")(1), Chr$(34))(0)
ActiveSheet.Range("J2").Offset(i, y).Value = val
    y = y
Next i
    i = i + 1







'close 'er up
    ieApp.Quit
    Set ieApp = Nothing








End With


End Sub
0 голосов
/ 28 января 2019

Ваш HTML не выглядит правильно сформированным.При правильно сформированном HTML вы должны иметь возможность использовать комбинацию селекторов css для нацеливания на элемент, комбинируя селектор класса, нацеливаясь на класс odd tr, с селектором attribute = value, используя модификатор contains (*)для указания значения атрибута onclick должна содержать подстрока custm_id.Затем вы извлекаете строку значения атрибута onclick и используете Split для доступа к идентификатору.Предполагая Internet Explorer в качестве метода доступа:

Dim val As String
val = ie.document.querySelector("tr.odd[onClick*='custm_id']").getAttribute("onclick")
Debug.Print Split(Split(val, "custm_id=")(1), Chr$(34))(0)

Все идентификаторы

Dim ids As Object, i As Long, val As String
Set ids = ie.document.querySelectorAll("tr[onClick*='custm_id']")

For i = 0 To ids.Length - 1
    val = ids.Item(i).getAttribute("onclick")
    Debug.Print Split(Split(val, "custm_id=")(1), Chr$(34))(0)
Next
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...