python - 我正在尝试使用 Excel 中的数据用 Python 填写 Web 表单

标签 python excel python-3.x vba

我有一个包含数千行数据的 excel 文件。我正在尝试寻找可以帮助我自动化从 excel 中提取数据并在我拥有的网站上填写数据的过程的 Material 。我只能在浏览器打开的情况下找到视频和说明。我可以在浏览器已经打开并且页面已经加载的情况下执行此操作吗? VBA 或 Python 上的任何内容都会有所帮助,谢谢。

最佳答案

您的问题非常广泛,但总的来说,我认为您正在寻找的基本上是 Python、Pandas 和 Selenium。
您基本上可以安装 Selenium,使用 Pandas 评估您的 csv 文件并循环它,以便它为您输入数据。这可能需要一段时间,因为它会模仿人类程序,可以这么说,但它应该对你有所帮助。
编辑 - 处理 Selenium 和伪代码
1. 积木
对于这个伪演练,我们将使用以下内容:

  • 美丽汤:用于获取 HTML 和 XML 信息的 Python 库
  • Pandas :用于读取和操作数据的 Python 库。这就是我们用来处理 Excel 文件的内容。
  • Selenium :一个网络浏览器自动化工具,它将为我们处理繁重的工作。
  • 谷歌浏览器:取消演示

  • 2. 安装
    2.1。 Python 库
    运行以下代码以安装必要的库:
    !pip install bs4
    !pip install pandas
    !pip install selenium
    
    2.2. Selenium 驱动程序
    现在您已经安装了库,让我们处理设置的最后一部分。我们将下载一个 Google Chrome 驱动程序,这将是 Selenium 库运行的基础。
    按着这些次序:
  • 打开谷歌浏览器,点击右上角的三个点
  • 转到帮助 > 关于 Google Chrome
  • 检查您的 Chrome 版本是什么
  • 访问 http://chromedriver.chromium.org/downloads并下载与您的版本对应的驱动程序
  • 确保将驱动程序保存到您将来使用的文件夹中

  • 3. 完成任务
    所以,我还没有完全运行这段代码,很抱歉我可能忽略了任何语法错误。然而,这就是它的样子:
    from bs4 import BeautifulSoup
    from requests import get
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import time
    import pandas as pd
    
    # using Pandas to read the csv file
    source_information = pd.read_csv('my_file.csv',header=None,skiprows=[0])
    
    # setting the URL for BeautifulSoup to operate in
    url = "yourwebform.com"
    my_web_form = get(url).content
    soup = BeautifulSoup(my_web_form, 'html.parser')
    
    # Setting parameters for selenium to work
    path = r'C:/Users/itsme/Desktop/Util/chromedriver.exe' #make sure you insert the path to your driver here!
    options = webdriver.ChromeOptions()
    options.add_argument("--start-maximized")
    driver = webdriver.Chrome(path, chrome_options=options)
    driver.get(url)
    
    # creating a procedure to fill the form
    def fulfill_form(username, user_email):
        # use Chrome Dev Tools to find the names or IDs for the fields in the form
        input_customer_name = driver.find_element_by_name('username')
        input_customer_email = driver.find_element_by_name('email')
        submit = driver.find_element_by_name('Submit')
    
        #input the values and hold a bit for the next action
        input_customer_name.send_keys(username)
        time.sleep(1)
        input_customer_email.send_keys(user_email)
        time.sleep(1)
        submit.click()
        time.sleep(7)
    
    
    # creating a list to hold any entries should them result in error
    failed_attempts = []
    
    # creating a loop to do the procedure and append failed cases to the list
    for customer in source_information:
        try:
            fulfill_form(source_information[customer_name], source_information[customer_email])
        except:
            failed_attempts.append(source_information[customer_name])
            pass
    
    if len(failed_attemps) > 0:
        print("{} cases have failed").format(len(failed_attempts))
    
    print("Procedure concluded")
    
    如果您遇到任何问题,请告诉我,我们将一起努力解决!

    关于python - 我正在尝试使用 Excel 中的数据用 Python 填写 Web 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58753500/

    相关文章:

    python - python 生成器的不一致行为

    python - 重新格式化 Dataframe 列,以便将任何数字月份子字符串替换为月份字符串

    python - 使用 Python 更改 csv 数据列格式

    Python切片[:] inconsistent behavior

    python - AttributeError : '_tkinter.tkapp' object has no attribute 'text' in tkinter

    javascript - 发布到 python eve 时缺少 CORS header ‘Access-Control-Allow-Origin’

    python - 使用 nltk 对阿拉伯语文本进行分块

    excel - 在vba中声明和使用范围

    Excel - 如何向单元格添加文件选择按钮?

    algorithm - 使用 python 检查有效的信用卡号