excel - 将网页内容复制到字符串
问题描述
我需要访问网页并将其内容(所有内容)复制到一个字符串中,然后从中提取一些数字。
网页地址每次都会变化,因为我基本上是在访问一个在线模拟工具,我每次都必须指定模拟参数。并且输出始终是一个大约 320 个字符的字符串。该网页仅包含该文本。
网址/查询示例:
网页内容(要检索的字符串)的示例: 37 0 1 54.9 72.1 7.21 2 73.1 96.0 12.0 12.0 3 114 149 15.5 4 121 160 17.9 5 140 185 140 185 11.3 6 142 188 9.31 7.31 7 161 212 212 10.2 10 83.0 109 15.5 11 55.8 73.3 13.5 12 55.8 73.2 9.47 年 1270 1680 58.8 AOI 损失:2.7% 光谱影响:- 温度和低辐照度损失:8.0% 综合损失:24.1%
向你提问
有没有一种方法可以复制该字符串而不必每次都打开和关闭浏览器?当我运行我的分析时,我必须重复该操作(确定查询参数,检索相关字符串,从字符串中提取我需要的值)总共7200 次,我希望它尽可能平滑和快速尽可能。
注意:我不需要将字符串文本保存在文档中,但如果需要,可以这样做,然后打开文件并检索我的字符串。但这听起来效率很低,我相信一定有更好的方法来做到这一点!
解决方案
有了这么多的请求,最好使用一个类来保存 xmlhttp 对象,而不是使用一个函数(每次都在其中创建和销毁对象)。然后运行一个将所有 url 传递给该对象的子程序。为类提供返回字符串的方法。
班级模块:clsHTTP
Option Explicit
Private http As Object
Private Sub Class_Initialize()
Set http = CreateObject("MSXML2.XMLHTTP")
End Sub
Public Function GetString(ByVal url As String) As String
Dim sResponse As String
With http
.Open "GET", url, False
.send
GetString = .responseText
End With
End Function
标准模块1:
Option Explicit
Public Sub GetStrings()
Dim urls, ws As Worksheet, i As Long, http As clsHTTP
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set http = New clsHTTP
'read in from sheet the urls
urls = Application.Transpose(ws.Range("A1:A2").Value) 'Alter range to get all urls
Application.ScreenUpdating = False
For i = LBound(urls) To UBound(urls)
ws.Cells(i, 2) = http.GetString(urls(i))
Next
Application.ScreenUpdating = True
End Sub
推荐阅读
- c# - 转换 Android.App.Application.Context 时出现 InvalidCastException
- python - 使用数据库行填充 html 页面
- java - 如何使用 Gradle 从可执行 Jar 文件中创建可执行文件?
- java - 斯坦福核心 NLP ner 4.0.0 错误:无法找到或加载主类 stanford-ner.jar;lib.*
- tensorflow - 将 TensorFlow 对象检测教程扩展到多个类时出错
- github - 我无法在 vscode 中启动终端
- spring - 如何应用所有 In 子句并使用 Spring JPA 中的条件获取列表
- azure - 从公共 Web 上的 Azure Webjob 删除或保护 Webhook
- .htaccess - 使用 .htaccess 覆盖现有的“noindex,nofollow”X-Robots-Tag 标头?
- c - 下面的代码应该接受初始输入,并且应该继续要求输入,直到两个输入都从 0 到 2000