【发布时间】:2021-09-21 10:07:29
【问题描述】:
我正在使用 VBA 代码将信息从网站提取到 excel 单元格中,数字信息很好,但我遇到了文本字符串问题。我主要是从格鲁吉亚网站中提取信息,而格鲁吉亚语言的文本在 excel 中没有正确显示,所以我想知道是否有机会(代码或其他东西)我可以将这些符号转换为正确的语言。
Sub GetData()
Dim request As Object
Dim response As String
Dim html As New HTMLDocument
Dim website As String
Dim price As Variant
Dim address As Variant
Dim x As Integer
Dim y As Range
x = 1
Do Until x = 9
Set y = Worksheets(1).Range("A21:A200"). _
Find(x, LookIn:=xlValues, lookat:=xlWhole)
website = "https://www.myhome.ge/ka/pr/11247371/iyideba-Zveli-ashenebuli-bina-veraze-T.-WoveliZis-qucha"
' Create the object that will make the webpage request.
Set request = CreateObject("MSXML2.XMLHTTP")
' Where to go and how to go there.
request.Open "GET", website, False
' Get fresh data.
request.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
' Send the request for the webpage.
request.send
' Get the webpage response data into a variable.
response = StrConv(request.responseBody, vbUnicode)
' Put the webpage into an html object.
html.body.innerHTML = response
' Get info from the specified element on the page.
address = html.getElementsByClassName("address").Item(0).innerText
price = html.getElementsByClassName("d-block convertable").Item(0).innerText
y.Offset(0, 1).Value = address
y.Offset(0, 5).Value = price
x = x + 1
Loop
End Sub
这是我从 youtube 视频 (https://www.youtube.com/watch?v=IOzHacoP-u4) 中获取的代码并稍作修改,它可以工作,我只是对 excel 如何在文本字符串中显示字符有疑问。
【问题讨论】:
-
@gugu - 你不需要这行
response = StrConv(request.responseBody, vbUnicode),只需将responsetext分配给innerhtml,html.body.innerHTML = request.responseText -
非常感谢@RaymondWu,您的回答非常有用且简单。我一直在到处寻找这个。再次感谢您。
-
@gugu - 我在代码中看不到任何与 ID 相关的内容(我只看到
address和price?)所以我不确定为什么你的“代码”不是在职的。但假设我在阅读网站时是正确的,请尝试Trim$(Replace(html.getElementsByClassName("id-container")(0).innerText, ":", vbNullString))。在您的示例中,这将返回: 11247371,我已应用Replace和Trim$函数来处理结果。 -
成功了。再次非常感谢您。
标签: excel vba text unicode utf