【发布时间】:2019-06-21 00:14:22
【问题描述】:
以下是 html/javascript 代码:我想在 tr 标签 onclick 中抓取 id。如何抓取 onClick 标签并在最后获取 id?下面的代码用于动态安全网站表,所有行都超链接并按奇偶类排序,如下所示。感谢您的帮助,谢谢!
<tr class='odd' onmouseover='mov(this)' onMouseOut="mou(this, 'odd')" onClick='location.href="custlist.php?rptname=Report&custm_id=**13857**"'>
<td align='right'> 1. </td>
<td nowrap style='text-align:;'> Company </td>
<td nowrap style='text-align:;'> blah blah </td>
<td nowrap style='text-align:;'> bbb </td>
<td nowrap style='text-align:right;'> 1,084,771.10 </td>
<td nowrap style='text-align:right;'> 1,060,787.10 </td>
<td nowrap style='text-align:right;'> 1,203,000.00 </td>
<td nowrap style='text-align:right;'> 30,233.90 </td>
<td nowrap style='text-align:left;'> </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'> 2. </td>
<td nowrap style='text-align:;'> T3 bbhj </td>
<td nowrap style='text-align:;'> hhht </td>
<td nowrap style='text-align:right;'> 720,260.00 </td>
<tr>
*在 25 行之后,它声明了换行代码:
<tr class='header'>
<th width='20px'> </th>
<th nowrap>
<a href='/fats/custlist.php?srid=4969&sort=1&desc=ASC&perpage=ALL' class='sorter'>
CUSTOMER</a>
</th>
<th nowrap>
<a href='/fats/custlist.php?srid=4969&sort=2&desc=ASC&perpage=ALL' class='sorter'>
CUSTOMER AGENT</a>
</th>
<th nowrap>
<a href='/fats/custlist.php?srid=4969&sort=3&desc=ASC&perpage=ALL' class='sorter'>
ACCT MANAGERS</a>
</th>
<th nowrap>
<a href='/fats/custlist.php?srid=4969&sort=4&desc=ASC&perpage=ALL' class='sorter'>
TOTAL</a>
<img src='images/up_arrow.gif'>
</th>
<th nowrap>
<a href='/fats/custlist.php?srid=4969&sort=5&desc=ASC&perpage=ALL' class='sorter'>
BALANCE</a>
</th>
<th nowrap>
<a href='/fats/custlist.php?srid=4969&sort=6&desc=ASC&perpage=ALL' class='sorter'>
CREDIT LIMIT</a>
</th>
<th nowrap>
<a href='/fats/custlist.php?srid=4969&sort=7&desc=ASC&perpage=ALL' class='sorter'>
CREDIT AVAILABLE</a>
</th>
<th nowrap>
<a href='/fats/custlist.php?srid=4969&sort=8&desc=ASC&perpage=ALL' class='sorter'>
NOTES</a>
</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
【问题讨论】:
-
@whitney,当用户单击
时,它会立即移动到下一页(custlist.php)。您可以从 custlist.php 页面加载事件的查询字符串中获取 custm_id 请分享您编写此代码的尝试并解释您遇到的问题?此外,请确保您的 html 格式正确且不要更改它。@QHarr 到目前为止,我添加了我的 vba 代码...从 onClick 获取 id 的最后部分我被卡住了。@QHarr 我想通了,请参阅下面的答案...非常感谢!非常感谢您的帮助! :)
标签: javascript html excel vba web-scraping