我有一个包含数千行数据的 excel 文件。我正在尝试寻找可以帮助我自动化从 excel 中提取数据并在我拥有的网站上填写数据的过程的 Material 。我只能在浏览器打开的情况下找到视频和说明。我可以在浏览器已经打开并且页面已经加载的情况下执行此操作吗? VBA 或 Python 上的任何内容都会有所帮助,谢谢。
最佳答案
您的问题非常广泛,但总的来说,我认为您正在寻找的基本上是 Python、Pandas 和 Selenium。
您基本上可以安装 Selenium,使用 Pandas 评估您的 csv 文件并循环它,以便它为您输入数据。这可能需要一段时间,因为它会模仿人类程序,可以这么说,但它应该对你有所帮助。
编辑 - 处理 Selenium 和伪代码
1. 积木
对于这个伪演练,我们将使用以下内容:
2. 安装
2.1。 Python 库
运行以下代码以安装必要的库:
!pip install bs4
!pip install pandas
!pip install selenium
2.2. Selenium 驱动程序现在您已经安装了库,让我们处理设置的最后一部分。我们将下载一个 Google Chrome 驱动程序,这将是 Selenium 库运行的基础。
按着这些次序:
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/