首页 > 解决方案 > 如何使用 python selenium 自动选择文本并保存到 csv/excel

问题描述

我正在开发一个 selenium Web 驱动程序应用程序,在该应用程序中会自动打开一个网页,并且该网页包含一个表格,所以我的下一个目标是,我想复制(抓取)该表格的第一行并且必须保存在 csv文件,要实现它我必须使用什么?请任何人帮助我。

代码:

from selenium import web driver
from web driver _manager. chrome import Chrome Driver Manager
import time
from selenium. web driver. chrome. options import Options
web = web driver. Chrome(Chrome Driver Manager().install())
web.get('http://ts.vcccms.in/CCMSPDI/FindLogs/Index')
time .sleep(4)
device_id = "slcs010928"
div_id = web.find_element_by_x path ("/html/body/div[2]/div[1]/form/div/div[1]/div[1]/div/input")
div_id.send_keys(device_id)
time.sleep(4)

submit = web.find_element_by_x path("/html/body/div[2]/div[1]/form/div/div[1]/div[2]/div/button")
submit.click()
time.sleep(4)

download = web.find_element_by_x path("/html/body/div[2]/div[1]/form/div/div[2]/div/section/div/div/div/div/div/div[1]/div/a[3]/span")
download.click()
time.sleep(4)
web.close()

标签: pythonpython-3.xselenium-webdriverweb-scraping

解决方案


首先,我不会在这里使用 Selenium。您可以从 XHR 和一个简单的请求中获取数据。然后我会pandas用来抓住第一行并使用.to_csv()

import requests
import pandas as pd

device_id = "slcs010928"

url = 'http://ts.vcccms.in/CCMSPDI/FindLogs/GetFindLogs'
payload = {
'DeviceID': device_id,
'_': '1631010215983'    }

jsonData = requests.get(url, params=payload).json()

df = pd.DataFrame(jsonData)
first_row = df.iloc[[0], :]

# The line below could essentially take the place of the 2 lines above
#first_row = pd.DataFrame(jsonData[0], index=[0])

first_row.to_csv('filename.csv', index=False)

输出:

print(first_row.to_string())
                                                                                                                                                                            meterData   CCMSBoxID          Time Latitude Longitude Voltage Current Power  Kvah Frequency    PF ApparentPower ActivePower  CummulativePower BatteryPercentage  TamperSwitchStatus  MCBStatus  ContractorStatus sTamperSwitchStatus sMCBStatus sContractorStatus GsmSignalStrength  VoltageStatus sVoltageStatus MaximumDemand MaximumDemandDate MaximumDemandKVA MaximumDemandKVADate TamperCount             SimNumber  RelayStatus sRelayStatus  WDTCount DeviceCheck MeterType  LightStatus  KeypadMode ScheduleOnTime ScheduleOffTime miscellaneousdata  EmergencyMode EmergencyStatus PoweronHours  DoorStatus sDoorStatus  Emergencyflag RTUStatus          RecivedTime ProtocolType
0  ~|23|010928|210907154508|24600|0000|026|5000|0003|0000|0014056|0000000|0000|000000000000|0000|000000000000|0000000|0000|8991000904215891255F|0|1|1|0|0|74|1800,0600|000|00200025|~  SLCS010928  210907154508     None      None     246       0  None     0        50  0.26         0.003           0             14056              None                   0          0                 1                None       None              Open                74              1           None          0000              None             0000                 None        0000  8991000904215891255F            0         None         0        None      None            0           0          18:00           06:00          00200025              0             OFF      0000000           0       Close              0   Working  07-09-2021 15:45:14         Http

推荐阅读