Excel VBA-从html复制文本并粘贴到Excel中
我期待从一个特定的网站复制数据,并粘贴到excel的信息。 我已经创建了一个搜索框,您可以输入项目代码,例如“CPINT00038”,它将打开特定页面。 但就我的一生而言,我不知道如何将图像和文本刮取并粘贴到excel中 这是我目前的代码,请帮忙Excel VBA-从html复制文本并粘贴到Excel中,vba,excel,Vba,Excel,我期待从一个特定的网站复制数据,并粘贴到excel的信息。 我已经创建了一个搜索框,您可以输入项目代码,例如“CPINT00038”,它将打开特定页面。 但就我的一生而言,我不知道如何将图像和文本刮取并粘贴到excel中 这是我目前的代码,请帮忙 Sub SearchCC() 'what to search for Dim SearchString As String SearchString = InputBox("What do you want to search for?") '
Sub SearchCC()
'what to search for
Dim SearchString As String
SearchString = InputBox("What do you want to search for?")
'create and a new instance of IE
Dim IE As New InternetExplorer
IE.Visible = True
'to find what you're looking for
IE.Navigate "http://www.canadacomputers.com/search_results.php?
search_in=&keywords=" & SearchString
While IE.Busy Or IE.ReadyState <> 4: DoEvents: Wend
End Sub
子搜索cc()
“搜索什么
将搜索字符串设置为字符串
SearchString=InputBox(“您想搜索什么?”)
'创建并创建一个新的IE实例
Dim IE成为新的InternetExplorer
可见=真实
“找到你要找的东西
即“导航”http://www.canadacomputers.com/search_results.php?
search_in=&keywords=“&SearchString
当IE忙或IE ReadyState 4:DoEvents:Wend时
端接头
对于描述,它非常简单,您只需将其一次性放入变量:
description = IE.Document.getElementById("desc1").getElementsByTagName("p")(0).innerText
。。。然后将其打印到所需的位置,作为Range(“A1”)。Value=description
对于图像来说,这有点棘手。要从web下载图像,您可以:
1) 在模块顶部添加对库urlmon.dll
提供的URLDownloadToFile
函数的声明(注意:如果Excel是32位而不是64位,则需要删除关键字PtrSafe
):
2) 使用如下函数将图像保存到具有给定名称的给定路径中:
samplePath = "C:\Users\Yourself\YourFolder\yourImage.jpg"
imgSource = IE.Document.getElementsByClassName("prodimg")(0).getAttribute("src")
Response = URLDownloadToFile(0, imgSource, samplePath, 0, 0)
如果Response
等于0
,则图像已成功从web下载并保存到“C:\Users\Yourself\YourFolder\yourImage.jpg”中
我将允许您录制一个宏,以便从给定路径在Excel中插入图像(我们将此宏称为子插入图像(ByVal filePath为字符串)
,然后:
If Response = 0 Then
InsertImage samplePath
End If
然后,您可以从您的计算机中删除下载的图像您尝试了什么?在SO和internet上通常有无数关于如何从网站获取图像/信息的帖子。这就是我现在拥有的,我可以提取描述,但我不知道如何提取图像。Sub SearchCC()'搜索Dim SearchString的内容作为String SearchString=InputBox(“您想搜索什么?”)'创建并创建一个新的IE Dim IE实例作为新的InternetExplorer IE.Visible=True'以查找您要搜索的IE.Navigate“”IE.Busy或IE.ReadyState 4:DoEvents:Wend'Grab Description From Site Description=IE.Document.getElementsByClassName(“prod descripp”)(0)。innerText With ActiveCell.Value=Description End With End Sub
If Response = 0 Then
InsertImage samplePath
End If