简体   繁体   English

如何使用 VBA 将 excel 表中的信息输入到特定网站的搜索栏?

[英]How do I enter information from my excel sheet to a specific website's search bar using VBA?

I'm trying to automate a process that enters information I have stored in an excel sheet to the website seen in the code below.我正在尝试自动化一个过程,该过程将我存储在 excel 表中的信息输入到下面代码中看到的网站。 I'm trying to identify the search bar to run a test to see if my information will appear before I work on the search button, but I get an error in the last line of code.The way I've been attempting to identify the search bar was to inspect the element on the website.我正在尝试识别搜索栏以运行测试,以查看我的信息是否会在我使用搜索按钮之前出现,但在最后一行代码中出现错误。我一直在尝试识别的方式搜索栏用于检查网站上的元素。 I'm pretty new with this and I've tried a few things, but I'm not sure what else I should do.我对此很陌生,我已经尝试了一些东西,但我不确定我还应该做什么。

The next steps of my project would be to gather a number generated based off of the information I entered into the search bar and enter it back into the excel sheet before moving onto the next row of information and repeating the same process.我项目的下一步是收集基于我在搜索栏中输入的信息生成的数字,并将其输入回 excel 工作表,然后再转到下一行信息并重复相同的过程。

Thank you in advance!先感谢您!

Dim ie As New SHDocVw.InternetExplorer
Dim website As String

website = "https://cardmavin.com/category/football"
ie.navigate website
ie.Visible = True

Do While ie.readyState <> READYSTATE_COMPLETE
    
Loop

'input name
Dim idoc As MSHTML.HTMLDocument
Set idoc = ie.document

idoc.all.q.Value = "Test"

That element has an id, so I would use that:该元素有一个id,所以我会使用它:

idoc.getElementById("search-field").Value = "Test"

May I recommend not automating Internet Explorer and instead using background HTTP objects?我可以建议不要自动化 Internet Explorer 而是使用后台 HTTP 对象吗? In many cases it becomes a lot easier than trying to find buttons and search bars and waiting for IE to load etc.在许多情况下,它比尝试查找按钮和搜索栏以及等待 IE 加载等要容易得多。

Eg for your example whenever you put text in the search bar and press "search", it actuallysends those search terms to the address in the routine below.例如,对于您的示例,每当您在搜索栏中输入文本并按“搜索”时,它实际上会将这些搜索词发送到下面例程中的地址。 You don't need to navigate to the search page and identify the elements...您无需导航到搜索页面并识别元素...

Sub Example()
Dim httpReq As Object
Set httpReq = CreateObject("MSXML2.ServerXMLHTTP")
    httpReq.Open "GET", "https://mavin.io/search?q=2002+Mattel"
    httpReq.Send
MsgBox httpReq.responseText
End Sub

The URL https://mavin.io/search?q= is where the request is posted to, and the stuff after it is the search terms I'm using ("2002" and "Mattel", where a space becomes a + ) URL https://mavin.io/search?q=是发布请求的位置,之后的内容是我正在使用的搜索词(“2002”和“Mattel”,其中空格变为+ )

The httpReq response can be read as text, or data, or put straight into an HTML document for parsing. httpReq响应可以读取为文本或数据,或直接放入 HTML 文档进行解析。 No Internet Explorer is created, no loading times, no graphics etc. are downloaded.没有创建 Internet Explorer,没有加载时间,没有下载图形等。 It's just a plain request for the HTML document without all the trash that comes with it.这只是对 HTML 文档的简单请求,没有附带的所有垃圾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用自己的搜索栏在特定网站上搜索项目? - How do I search item on a specific website using my own search bar? 使用Excel VBA填写网站搜索栏 - Fill in website search bar using Excel VBA 我如何使用excel vba将网站上的特定数据(名称,详细信息)提取到excel中? - How do i extract specific data (name, details )from the website into excel with excel vba? 如何从我的网站中删除水平滚动条 - How do I remove horizontal scroll bar from my website 单击或输入后,如何获取搜索栏中的信息。 然后将其粘贴到另一页上的另一个输入中? - How can I get the Information in search bar on click or Enter. Then paste it to another input on another page? 如何使用Excel / VBA通​​过Element ID从外部网站获取价值? - How do I pull value from external website by Element ID with Excel / VBA? 除了侧面导航栏外,如何向网站添加内容(文字)? - How do I add content (writings) to my website apart from my side navigation bar? 如何在我的网站上将导航栏居中? - How do I center my navigation bar on my website? 为什么我的VBA代码没有从网站的HTMLDoc中提取信息? - Why isn't my VBA code pulling information from a website's HTMLDoc? 如何使我的搜索栏在聚焦时不会折叠? - How do I make it so that my search bar does not collapse when it's on focus?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM