使用 Python Selenium Openpyxl 在 Excel 中追加数据
Append data in Excel using Python Selenium Openpyxl
我有一个带有 5 个注册号的 sheet,已成功将其输入到网页搜索中,代码运行时没有错误,同时使用 'For' 语句对 'A' 列 (Col=0).
我现在尝试将代码返回的颜色附加到 'B' 列 (col=1)
中的相应行中
我写的代码(在这里有以前的帮助)如下:
from openpyxl import Workbook, load_workbook
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
wb = load_workbook('data.xlsx')
ws = wb.active
for row in ws.iter_rows(min_row=2, max_col=0, max_row=6, values_only=True):
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.google.com")
driver.get ("https://vehicleenquiry.service.gov.uk/")
time.sleep(5)
search = driver.find_element(By.ID , "wizard_vehicle_enquiry_capture_vrn_vrn")
search.send_keys(str(row))
search.send_keys(Keys.RETURN)
try:
main = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "summary-no-action"))
)
print(WebDriverWait(driver,10).until(
EC.visibility_of_element_located((By.XPATH,
"//dt[text()='Colour']/following::dd[1]"))).text
)
time.sleep(5)
finally:
driver.quit()
带回颜色和打印的代码是:
print(WebDriverWait(driver,10).until(
EC.visibility_of_element_located((By.XPATH,
"//dt[text()='Colour']/following::dd[1]"))).text
)
有没有一种简单的方法可以将这个字符值放入我没有看到的相邻行中?
我假设我只是类似于:
for col in ws.iter_cols(min_row=2, max_col=2, max_row=6):
for cell in col:
append(cell)
但是,我认为这不会起作用,因为颜色值尚未设置为变量。关于可行的解决方案有什么想法吗?
在excel中,第一个单元格(第一行第一列)是1,1
没有 0 列或 0 行
有帮助吗
wb = openpyxl.load_workbook('data.xlsx')
ws = wb.active
for row in range(2,7):
YOUR_CODE
ws.cell(row=row,column=2).value = YOUR_OUTPUT_DATA
如您所见,您可以使用 ws.cell(row=row,column=1).value
我有一个带有 5 个注册号的 sheet,已成功将其输入到网页搜索中,代码运行时没有错误,同时使用 'For' 语句对 'A' 列 (Col=0).
我现在尝试将代码返回的颜色附加到 'B' 列 (col=1)
中的相应行中我写的代码(在这里有以前的帮助)如下:
from openpyxl import Workbook, load_workbook
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
wb = load_workbook('data.xlsx')
ws = wb.active
for row in ws.iter_rows(min_row=2, max_col=0, max_row=6, values_only=True):
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.google.com")
driver.get ("https://vehicleenquiry.service.gov.uk/")
time.sleep(5)
search = driver.find_element(By.ID , "wizard_vehicle_enquiry_capture_vrn_vrn")
search.send_keys(str(row))
search.send_keys(Keys.RETURN)
try:
main = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "summary-no-action"))
)
print(WebDriverWait(driver,10).until(
EC.visibility_of_element_located((By.XPATH,
"//dt[text()='Colour']/following::dd[1]"))).text
)
time.sleep(5)
finally:
driver.quit()
带回颜色和打印的代码是:
print(WebDriverWait(driver,10).until(
EC.visibility_of_element_located((By.XPATH,
"//dt[text()='Colour']/following::dd[1]"))).text
)
有没有一种简单的方法可以将这个字符值放入我没有看到的相邻行中? 我假设我只是类似于:
for col in ws.iter_cols(min_row=2, max_col=2, max_row=6):
for cell in col:
append(cell)
但是,我认为这不会起作用,因为颜色值尚未设置为变量。关于可行的解决方案有什么想法吗?
在excel中,第一个单元格(第一行第一列)是1,1 没有 0 列或 0 行
有帮助吗
wb = openpyxl.load_workbook('data.xlsx')
ws = wb.active
for row in range(2,7):
YOUR_CODE
ws.cell(row=row,column=2).value = YOUR_OUTPUT_DATA
如您所见,您可以使用 ws.cell(row=row,column=1).value