Excel VBA:无法在网站上执行自动搜索
最近我正在学习使用excel宏在网站上搜索。我已经阅读了几个论坛帖子,并提出了下面的代码。但是,当我到达行时会出现错误Excel VBA:无法在网站上执行自动搜索,excel,vba,web-scraping,ex,Excel,Vba,Web Scraping,Ex,最近我正在学习使用excel宏在网站上搜索。我已经阅读了几个论坛帖子,并提出了下面的代码。但是,当我到达行时会出现错误 SearchBox(0).Value = SearchString 我试图删除(0),但也出现了另一个错误。我现在很沮丧,想征求你的专业意见。该代码在其他网站上运行良好。我应该如何更改它们以适应此站点?非常感谢你 另外,我还想知道点击搜索按钮的方法。谢谢 Sub Searchstockcode() Dim SearchString As String SearchStri
SearchBox(0).Value = SearchString
我试图删除(0),但也出现了另一个错误。我现在很沮丧,想征求你的专业意见。该代码在其他网站上运行良好。我应该如何更改它们以适应此站点?非常感谢你
另外,我还想知道点击搜索按钮的方法。谢谢
Sub Searchstockcode()
Dim SearchString As String
SearchString = "700"
Set ie = CreateObject("InternetExplorer.Application")
With ie
ie.Visible = True
End With
ie.Navigate "http://www.hkexnews.hk/listedco/listconews/advancedsearch/search_active_main.aspx"
While ie.ReadyState <> 4
DoEvents
Wend
Dim SearchBox As Object
Set SearchBox = ie.Document.GetElementsByName("ct100$txt_stock_code")
SearchBox(0).Value = SearchString
Dim SearchButton As Object
Set SearchButton = ie.Document.GetElementsByName
End Sub
子搜索股票代码()
将搜索字符串设置为字符串
SearchString=“700”
设置ie=CreateObject(“InternetExplorer.Application”)
与ie
可见=真实
以
即“导航”http://www.hkexnews.hk/listedco/listconews/advancedsearch/search_active_main.aspx"
而ie.ReadyState 4
多芬特
温德
将搜索框变暗为对象
Set SearchBox=ie.Document.GetElementsByName(“ct100$txt\U股票代码”)
SearchBox(0)。值=SearchString
将搜索按钮变暗为对象
设置SearchButton=ie.Document.GetElementsByName
端接头
问候,,
LLC我不知道您的名称选择问题是否是由于元素有两个名称属性造成的,但这似乎是可能的 您可以使用以下命令 对于搜索框,我使用它的id来定位元素。这在文档上通常是唯一的,并且是最快的选择器方法 对于搜索按钮,我使用CSS属性+值选择器
[src*='/image/search.gif']
这将通过元素的值来定位元素的src
属性[]
<代码>*表示包含。选择器查找值中包含/image/search.gif
的src
属性
您可以在此处观察该属性:
选项显式
子代码()
Dim SearchString作为字符串,SearchBox作为对象,SearchButton作为对象,ie作为对象
SearchString=“700”
设置ie=CreateObject(“InternetExplorer.Application”)
可见=真实
即“导航”http://www.hkexnews.hk/listedco/listconews/advancedsearch/search_active_main.aspx"
当ie.Busy或ie.readyState<4:DoEvents:Wend时
设置SearchBox=ie.document.getElementById(“ctl00\U txt\U股票代码”)
SearchBox.Value=SearchString
设置SearchButton=ie.document.querySelector(“[src*=”/image/search.gif']”)
搜索按钮。单击
当ie.Busy或ie.readyState<4:DoEvents:Wend时
停止“请与我们共享您收到的确切错误消息。此外,该宏还能与哪些其他网站配合使用?以便我们可以比较/对比他们的html。谢谢Qharr!!它工作得非常好!我想我出错的原因是我把“l”和“1”搞糟了哈哈!关于下一步,我还有一个问题:我总是想单击单击搜索按钮后出现的第一个链接。我假设这段代码应该可以工作:“Set TargetFile=I.e.document.getElementById(“ctl00\u gvMain\u ctl02\u hlTitle”)TargetFile.Click”,但之后我不知道如何保存或打开该文件。我的目的是打开文件并提取某些数据。提前非常感谢!假设什么代码应该工作?发布一个你正在尝试的问题,并将链接留在这里,我会看一看。确保您陈述了应该发生的事情和正在发生的事情。设置TargetFile=i.e.document.getElementById(“ctl00\u gvMain\u ctl02\u hlTitle”)TargetFile。单击发布问题,因为在此处的注释中更容易调试。
Option Explicit
Sub Searchstockcode()
Dim SearchString As String, SearchBox As Object, SearchButton As Object, ie As Object
SearchString = "700"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://www.hkexnews.hk/listedco/listconews/advancedsearch/search_active_main.aspx"
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
Set SearchBox = ie.document.getElementById("ctl00_txt_stock_code")
SearchBox.Value = SearchString
Set SearchButton = ie.document.querySelector("[src*='/image/search.gif']")
SearchButton.Click
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
Stop '<==Delete me
'other code
ie.Quit
End Sub