使用VBA搜索网站

编程入门 行业动态 更新时间:2024-10-25 10:30:15
本文介绍了使用VBA搜索网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想要做的是使用VBA搜索

在即时窗口中打印:

修改:

后期绑定版本

Option Explicit Public Sub GetInfo() Dim IE As Object,html As Object 使用CreateObject(InternetExplorer.Application) .Visible = True .navigatepl.pons/t%C5%82umaczenie-tekstu 而.Busy或.readyState< 4:DoEvents:Wend 设置html = CreateObject(htmlfile)设置html = .document 使用html 。 getElementsByClassName(text-translation-source source)(0).innerText =Translate .getElementsByClassName(btn btn-primary submit)(0).Click Application.Wait Now + TimeSerial (0,0,2) Dim i As Long For i = 0 To .getElementsByClassName(text-translation-target target)。Length - 1 Debug。打印.getElementsByClassName(text-translation-target target)(i).innerText 下一页i 止损结束时 .Quit 结束 End Sub

What I would like to do is to search a website using VBA, putting some words in the left box and getting results on the right.

The problem is that I don't know HTML and I don't know how to refer to this box. I use GetElementByID but I received error in line:

objIE.Document.GetElementByID("text-translation-video-ad").Value = "piłka". "Object doesn't support this property or method".

Here's my code:

Sub www() Set objIE = CreateObject("InternetExplorer.Application") objIE.Top = 0 objIE.Left = 0 objIE.Width = 800 objIE.Height = 600 objIE.AddressBar = 0 objIE.StatusBar = 0 objIE.Toolbar = 0 objIE.Visible = True objIE.Navigate ("pl.pons/tłumaczenie-tekstu") Do DoEvents Loop Until objIE.ReadyState = 4 pagesource = objIE.Document.Body.Outerhtml objIE.Document.GetElementByID("text-translation-video-ad").Value = "piłka" objIE.Document.GetElementByID("qKeyboardInputInitiator").Click Do DoEvents Loop Until objIE.ReadyState = 4 End Sub

解决方案

Without changing any language settings, the following translates "Hello"

Code:

Option Explicit Public Sub GetInfo() Dim IE As New InternetExplorer, html As HTMLDocument, translation As String Const TRANSLATION_STRING As String = "Hello" With IE .Visible = True .navigate "pl.pons/t%C5%82umaczenie-tekstu" While .Busy Or .readyState < 4: DoEvents: Wend Set html = .document With html .querySelector("textarea.text-translation-source.source").Value = TRANSLATION_STRING .querySelector("button.btn.btn-primary.submit").Click Application.Wait Now + TimeSerial(0, 0, 3) translation = .querySelector("div.translated_text").innerText End With Debug.Print translation 'Quit '<== Remember to quit application End With End Sub

View:

Print out in immediate window:

Edit:

Late bound version

Option Explicit Public Sub GetInfo() Dim IE As Object, html As Object With CreateObject("InternetExplorer.Application") .Visible = True .navigate "pl.pons/t%C5%82umaczenie-tekstu" While .Busy Or .readyState < 4: DoEvents: Wend Set html = CreateObject("htmlfile") Set html = .document With html .getElementsByClassName("text-translation-source source")(0).innerText = "Translate" .getElementsByClassName("btn btn-primary submit")(0).Click Application.Wait Now + TimeSerial(0, 0, 2) Dim i As Long For i = 0 To .getElementsByClassName("text-translation-target target").Length - 1 Debug.Print .getElementsByClassName("text-translation-target target")(i).innerText Next i Stop End With .Quit End With End Sub

更多推荐

使用VBA搜索网站

本文发布于:2023-11-28 19:30:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643677.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:搜索网站   VBA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!