我的代码有问题,其中只有gene_name和description的最后一个cdi数据将被保存。硒会同时打开两个cdi并带回gene_name和描述。由于某些原因,仅第二个cdi被保存到excel文件中。有什么方法可以将gene_name的cdi和描述都保存到1个excel文件中。
from selenium import webdriver
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
import pandas as pd
path = (r"C:\Users\hogyu\PycharmProjects\Project_Selenium\drivers\chromedriver.exe")
driver = webdriver.Chrome(path)
#B0041.2c, ZC518.3b
cdis = ["ZC518.3b","B0041.2c"]
for cdi in cdis:
data = {}
df = pd.DataFrame(columns=['Gene_name', 'Description'])
dictionary = {'One': 1, 'Two': 2}
try:
driver.get("https://wormbase.org/#012-34-5")
search = driver.find_element_by_id("Search")
search.send_keys(cdi)
search.send_keys(Keys.RETURN)
id = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "locus")))
description = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "text-min")))
data['Gene_name'] = id.text
data['Description'] = description.text
df = df.append(data, ignore_index=True)
df.index += 1
print(df)
finally:
time.sleep(1)
driver.back()
driver.refresh()
time.sleep(2)
driver.quit()
print((df).to_excel(r"C:/Users/hogyu/OneDrive/Desktop/Python codes/experiments/worbase_data.xlsx"))