填充html自动搜索框并获取结果

填充html自动搜索框并获取结果,html,excel,vba,web-scraping,ie-automation,Html,Excel,Vba,Web Scraping,Ie Automation,我试图在网页上填充一个搜索框,当它被填充时,自动搜索结果。该网站是。如果你去那里输入主板制造商的主板名称,你可以看到它是如何开始缩小可能的选择范围的。我有代码,将填写在搜索框中,但什么也没有发生 Sub-GetMotherboards() Dim ie作为InternetExplorer Set ie=新的InternetExplorer 作为HTMLDocument的Dim doc 作为数据对象的Dim objText 作为对象的Dim objArticleContents Dim objLi

我试图在网页上填充一个搜索框,当它被填充时,自动搜索结果。该网站是。如果你去那里输入主板制造商的主板名称,你可以看到它是如何开始缩小可能的选择范围的。我有代码,将填写在搜索框中,但什么也没有发生

Sub-GetMotherboards()
Dim ie作为InternetExplorer
Set ie=新的InternetExplorer
作为HTMLDocument的Dim doc
作为数据对象的Dim objText
作为对象的Dim objArticleContents
Dim objLinksCollection作为对象
Dim objToClipBoard作为数据对象
作为对象的dimr
用绳子戳的
将prodName设置为字符串
暗淡的lngNumberOfVideos与长
暗弦
Dim strNewString作为字符串,strStr作为字符串,strTestChar作为字符串
作为布尔值的Dim bFlag
strURL=”https://pcpartpicker.com/products/motherboard/“'范围(“J5”)。值
与ie
.导航strURL
.Visible=True
执行While.readyState 4:DoEvents:Loop
申请。现在等待+#12:00:02 AM#
Set doc=ie.document
以
bFlag=False
和医生
Set objArticleContents=.getElementsByClassName(“subTitle\uu表单”)
停止
Set ele=.getElementsByClassName(“字幕形式”)(0)
Set form=.getElementsByClassName(“subTitle_uuform”)(0)。getElementsByClassName(“form label xs inline”)(1)
Set inzputz=ele.getElementsByClassName(“文本输入”)(0)
Call.getElementsByClassName(“文本输入”)(0).setAttribute(“占位符”,“MSI B450战斧”)”.setAttribute(“部件类别搜索”,“MSI B450战斧”)
以
端接头
在阅读了这里的一些帖子(我现在找不到)后,我的想法是有/有事件侦听器和函数需要包含在这段代码中,但这超出了我的理解范围。有人能帮我弄清楚吗


蒂姆·威廉姆斯(Tim Williams)在这里发表了一篇文章(一篇文章的答案),其中讨论了这一点,但现在我找不到它。

将值放入文本框后,需要执行
键控事件

您可以通过使用
execScript
方法来实现这一点

因此,加载网页后,为输入/文本框创建一个变量。在下面的示例中,它是
tb
。将
.Value
属性设置为搜索文本(我使用了“MSI”),然后通过脚本触发keyup事件

Dim tb As HTMLInputElement
Set tb = IE.document.getElementById("part_category_search")
tb.Value = "MSI"
IE.document.parentWindow.execScript "$('#part_category_search').keyup()"
我不太熟悉jQuery,所以这个脚本针对网页上的所有输入。但我已经测试过了,它适合你的搜索

以下是我在测试中使用的完整代码,如果您想缩短您的代码:

Dim IE As InternetExplorer
Set IE = New InternetExplorer
IE.Visible = True
IE.navigate "https://pcpartpicker.com/products/motherboard/"
Do While IE.Busy Or IE.readyState < 4
    DoEvents
Loop

Dim tb As HTMLInputElement
Set tb = IE.document.getElementById("part_category_search")
tb.Value = "MSI"
IE.document.parentWindow.execScript "$('#part_category_search').keyup()"
Dim IE作为InternetExplorer
Set IE=新的InternetExplorer
可见=真实
即“导航”https://pcpartpicker.com/products/motherboard/"
在IE忙或IE readyState<4时执行
多芬特
环
Dim tb作为HTMLInputElement
设置tb=IE.document.getElementById(“部分\类别\搜索”)
tb.Value=“MSI”
IE.document.parentWindow.execScript“$('#part_category_search').keyup()”

将值放入文本框后,需要执行
键控
事件

您可以通过使用
execScript
方法来实现这一点

因此,加载网页后,为输入/文本框创建一个变量。在下面的示例中,它是
tb
。将
.Value
属性设置为搜索文本(我使用了“MSI”),然后通过脚本触发keyup事件

Dim tb As HTMLInputElement
Set tb = IE.document.getElementById("part_category_search")
tb.Value = "MSI"
IE.document.parentWindow.execScript "$('#part_category_search').keyup()"
我不太熟悉jQuery,所以这个脚本针对网页上的所有输入。但我已经测试过了,它适合你的搜索

以下是我在测试中使用的完整代码,如果您想缩短您的代码:

Dim IE As InternetExplorer
Set IE = New InternetExplorer
IE.Visible = True
IE.navigate "https://pcpartpicker.com/products/motherboard/"
Do While IE.Busy Or IE.readyState < 4
    DoEvents
Loop

Dim tb As HTMLInputElement
Set tb = IE.document.getElementById("part_category_search")
tb.Value = "MSI"
IE.document.parentWindow.execScript "$('#part_category_search').keyup()"
Dim IE作为InternetExplorer
Set IE=新的InternetExplorer
可见=真实
即“导航”https://pcpartpicker.com/products/motherboard/"
在IE忙或IE readyState<4时执行
多芬特
环
Dim tb作为HTMLInputElement
设置tb=IE.document.getElementById(“部分\类别\搜索”)
tb.Value=“MSI”
IE.document.parentWindow.execScript“$('#part_category_search').keyup()”

您可以避免浏览器的开销,并执行与返回json的页面相同的操作。您需要一个json解析器来处理响应

Json库:

我使用jsonconverter.bas。从名为JsonConverter的标准模块下载原始代码并将其添加到该模块中。然后需要转到VBE>工具>引用>添加对Microsoft脚本运行时的引用。从复制的代码中删除顶部属性行

我展示了一个部分实现,它请求不同的类别和产品,并使用完整和部分字符串搜索。这是一个部分实现,我将响应读取到json对象中,并打印json字符串,但不尝试访问json对象中的所有项。可以根据您提供的更多细节进行改进。对于演示版,我将访问
(“结果”)(“数据”)
,它将为您提供价格和名称信息。原始响应json的一部分将
html
作为访问者
(“结果”)(“html”)
的值。这有描述信息,例如插座/CPU和主板项目

Option Explicit

Public Sub ProductSearches()
    Dim xhr As Object, category As String, items()

    Set xhr = CreateObject("MSXML2.XMLHTTP")
    category = "motherboard"
    items = Array("Gigabyte B450M DS3H", "MSI B450 TOMAHAWK", "random string")

    PrintListings items, xhr, category

    category = "memory"
    items = Array("Corsair Vengeance") 'partial search

     PrintListings items, xhr, category

End Sub

Public Function GetListings(ByVal xhr As Object, ByVal category As String, ByVal item As String) As Object
    Dim json As Object
    With xhr
        .Open "GET", "https://pcpartpicker.com/products/" & category & "/fetch/?xslug=&location=&search=" & item, False
        .setRequestHeader "User-Agent", "Mozilla/5.0"
        .send
        Set json = JsonConverter.ParseJson(.responseText)("result")("data")
        Set GetListings = json
    End With
End Function

Public Sub PrintListings(ByRef items(), ByVal xhr As Object, ByVal category As String)
    'Partially implemented. You need to decide what to do with contents of json object
    Dim json As Object, i As Long
    For i = LBound(items) To UBound(items)
        Set json = GetListings(xhr, category, items(i))
        'Debug.Print Len(JsonConverter.ConvertToJson(json)) ' Len(JsonConverter.ConvertToJson(json)) =2 i.e {} then no results
        Debug.Print JsonConverter.ConvertToJson(json)  'demo purposes only
        'do something with json
    Next
End Sub

Json解析:


阅读有关在vba中使用JsonConverter和解析json的内容。

您可以避免浏览器的开销,并执行与页面返回json相同的操作。您需要一个json解析器来处理响应

Json库:

我使用jsonconverter.bas。从名为JsonConverter的标准模块下载原始代码并将其添加到该模块中。然后需要转到VBE>工具>引用>添加对Microsoft脚本运行时的引用。从复制的代码中删除顶部属性行

我展示了一个部分实现,它对不同的类别和产品发出请求,并使用这两种类型