excel - 如何提取带有特定文本的 URL?
问题描述
我需要提取包含 URL 特定行的 URL,例如/example/example1/newexample/
<a onfocus="OnLink(this)" href="/example/example1/newexample/testing.aspx">Testing</a>
我当前的代码返回页面上的所有超链接。我如何仅使用/example/example1/newexample/提取这些链接
Sub GetAllLinks()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
url_name = Sheet1.Range("B2")
If url_name = "" Then Exit Sub
IE.navigate (url_name)
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Set AllHyperlinks = IE.document.getElementsByTagName("A")
Sheet1.ListBox1.Clear
For Each Hyperlink In AllHyperlinks
Sheet1.ListBox1.AddItem (Hyperlink)
Next
IE.Quit
MsgBox "Completed"
End Sub
解决方案
使用 CSS 选择器更容易避免初始循环和目标,然后循环返回的 nodeList
Dim aNodeList As Object, i As Long
Set aNodeList = IE.document.querySelector("a[href^='/example/example1/newexample/']")
For i = 0 To aNodeList.Length-1
Debug.print aNodeList.item(i).getAttribute("href")
Next i
方法以^
开头,因此a[href^='/example/example1/newexample/']
是寻找带有a
标签的元素,其中包含href
以开头的属性'/example/example1/newexample/'
这是您的 html 示例中的 CSS 选择器:
推荐阅读
- laravel-5 - Laravel 航海者与 phpunit
- html - 在 WooCommerce 购物车页面中更改运输方式费率值字体粗细
- python - 处理大量文件的错误代码逻辑
- javascript - 如何防止css为元素着色
- openssl - 从 MarkLogic Server 连接的 sslv3 握手失败 (0x14077410)
- google-cloud-dataflow - 丢弃的延迟消息的 Cloud Dataflow 指标
- c - 两个父母的一个孩子进程
- ruby-on-rails - 使用 wicked_pdf gem 时的 SystemStackError(堆栈级别太深)
- c++ - C++ `typedef 指针
(*构造函数)(LayerParams ¶ms);` - c# - 如何模拟持有 CrossFilePicker 的异步方法?