在excel中将奇怪的字符和符号转换为普通语言

问题描述

我正在使用 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 如何显示文本字符串中的字符有疑问。

enter image description here

解决方法

对于您在问题中的问题

  1. 删除此行 response = StrConv(request.responseBody,vbUnicode),因为它不是必需的。
  2. html.body.innerHTML = response 更改为 html.body.innerHTML = request.responseText

对于您在评论中的问题

要检索属性的 ID,可以从类 id-container 中检索它,但您需要执行一些字符串处理以删除提取 :

propertyID = Trim$(Replace(html.getElementsByClassName("id-container")(0).innerText,":",vbNullString))

注意:您应该尽量避免将变量声明为 VariantinnerText 属性返回 String 数据类型,因此您应该将 addressprice 声明为 String