我正在尝试自动执行返回信息表的搜索。我可以在 .text 中打印结果,但我的问题是如何将结果传递到 Pandas 数据框中。我问这个问题的原因有两个:因为我想将结果打印到 CSV 文件中,并且我需要 Pandas 中的结果以便稍后进行数据分析。如果有人可以提供帮助,我们将不胜感激。我的代码如下:
import time
from selenium import webdriver
import pandas as pd
search = ['0501020210597400','0501020210597500','0501020210597600']
df = pd.DataFrame(search)
chrome_path = [Chrome Path]
driver = webdriver.Chrome(chrome_path)
driver.get('https://enquiry.mpsj.gov.my/v2/service/cuk_search/')
x = 0
while x <(len(df.index)):
search_box = driver.find_element_by_name('sel_value')
new_line = (df[0][x]).format(x)
search_box.send_keys(new_line)
search_box.submit()
time.sleep(5)
table = driver.find_elements_by_class_name('tr-body')
for data in table:
print(data.text)
driver.find_element_by_name('sel_value').clear()
x +=1
driver.close()
最佳答案
要将 CSV 文件加载到 DataFrame,您可以执行以下操作:
df = pd.read_csv('example.csv')
参见在线文档:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html#pandas.read_csv
要将数据写入 CSV,请参阅本文:Pandas writing dataframe to CSV file就这样。
解决办法是:
df.to_csv(file_name, sep='\t')
关于python - 将 Selenium 数据结果传递给 Pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45653019/