通过VBA Excel更改站点中的数据

时间:2018-12-08 10:15:27

标签: html excel vba excel-vba web-scraping

我正在尝试通过以下代码更改网站中的值,但是如何通过代码单击“提交”按钮?

如果我的.Navigate值应保持从A1-> A2->等移动而不是静态值怎么办?(如何使用'For'循环代替.Navigate)呢? / p>

Sub FillForm()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://www.morovia.com/bulk-check-digit-calculation/index.php"
IE.Visible = True
While IE.busy
DoEvents
Wend
IE.Document.All("").Value = ThisWorkbook.Sheets("Sheet1").Range("b1")
End Sub

任何帮助将不胜感激。 谢谢!

3 个答案:

答案 0 :(得分:2)

Sub FillForm()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://www.morovia.com/bulk-check-digit-calculation/index.php"
IE.Visible = True
While IE.busy
DoEvents
Wend

Value = ThisWorkbook.Sheets("Sheet1").Range("b1")
IE.Document.getelementbyid("sourceNumbers").Value = Value
IE.Document.getelementbyid("submit").Click
End Sub

答案 1 :(得分:2)

一些注意事项:

1)您需要在代码中使用适当的等待行:

using System;
using System.IO;

class TutorialClass
{
    static void Main(string[] args)
    {
        DirectoryInfo search_path = new DirectoryInfo(@"/home/mark");
        FileInfo file = new FileInfo("PixmapComposite.cs");

        DirectoryInfo dir_result = FindFile(search_path, file);

        if (dir_result == null)
            Console.WriteLine("File not found");
        else
            Console.WriteLine("The directory path is: {0}", dir_result.FullName);
    }

    public static DirectoryInfo FindFile(DirectoryInfo dir, FileInfo file_name)
    {
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            Console.WriteLine("File: {0}", file.Name);
            Console.WriteLine();
            if (file.Name == file_name.Name)
            {
                return file.Directory;
            }
            else
            {
                // Search the remaining directories
                Console.WriteLine("Master dir: {0}", dir.FullName);
                DirectoryInfo[] sub_dir = dir.GetDirectories();
                foreach (DirectoryInfo current_dir in sub_dir)
                {
                    Console.WriteLine("Dir: {0}", current_dir.FullName);
                    Console.WriteLine();
                    return FindFile(current_dir, file_name);
                }
            }
        }
        return null;
    }
}

2)提交按钮具有ID。通常,这是最快的检索方法,请使用它。

While ie.Busy Or ie.readyState < 4: DoEvents: Wend

3)尽可能使用ID。例如,输入初始搜索号码

ie.document.querySelector("#submit").click

4)最新的导航方法是:

ie.document.querySelector("#sourceNumbers").value = ThisWorkbook.Sheets("Sheet1").Range("B1").value

5)始终在ie.navigate2 .click之后有另一个等待行,以允许页面加载新内容

6)始终记得.submit应用程序,以防止进程继续运行并可能导致系统资源不足。

.Quit

A列中的链接循环可能看起来像这样:

Public Sub FillForm()
    Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .Navigate2 "https://www.morovia.com/bulk-check-digit-calculation/index.php"

        While .Busy Or .readyState < 4: DoEvents: Wend
        .document.querySelector("#sourceNumbers").Value = ThisWorkbook.Sheets("Sheet1").Range("B1").Value

        .document.querySelector("#submit").Click

        While .Busy Or .readyState < 4: DoEvents: Wend

        Debug.Print .document.querySelector("#results").Value
        .Quit
    End With
End Sub

答案 2 :(得分:0)

Sub FillForm()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://www.morovia.com/bulk-check-digit-calculation/index.php"
IE.Visible = True
While IE.busy
DoEvents
Wend
For i = 1 to 10 ' Loop according to your Number Of Rows Containing Values
Value = ThisWorkbook.Sheets("Sheet1").Range("B" & i).Value
IE.Document.getelementbyid("sourceNumbers").Value = Value
IE.Document.getelementbyid("submit").Click 'here submit should be in all small letters
Next i
End Sub