首页 > 解决方案 > 如何将这组数据从垂直保存到水平

问题描述

我需要将数据保存在行中,但脚本将其保存在每个数据的新行中-

这是我的代码和 excel 文件的结果。

from selenium import webdriver
from selenium.webdriver.support.ui import Select

import pandas as pd


wd = webdriver.Chrome(r'D:\selenium\chromedriver_win32\chromedriver.exe')

wd.implicitly_wait(3)

url = 'https://srh.bankofchina.com/search/whpj/search_cn.jsp'

wd.get(url)

date1='2021-03-10'

date2='2021-03-11'

cur ='美元'

def lookup(date1,date2,cur):

    begin = wd.find_element_by_xpath('//[@id="historysearchform"]/div/table/tbody/tr/td[2]/div/input')
    begin.send_keys(date1)
    end = wd.find_element_by_xpath('//*[@id="historysearchform"]/div/table/tbody/tr/td[4]/div/input')
    end.send_keys(date2)
    currency = Select(wd.find_element_by_id("pjname"))
    currency.select_by_value(cur)
    search = wd.find_element_by_xpath('//*[@id="historysearchform"]/div/table/tbody/tr/td[7]/input').click()

list = []

for n in range(1,7):

    def set_num(n):
        element = wd.find_elements_by_css_selector('.boc_main.publish tr td:nth-child(n)')
            for a in element:
                row = a.get_property('textContent')
                data = {'n':row}
                list.append(data)

lookup(date1,date2,cur)

set_num(n)

df = pd.DataFrame(list)

df.to_excel('HGFF.xlsx')

wd.close()

图片1; 结果提取并保存

Picture2 目标是以水平格式导出数据

请帮忙

标签: pythonselenium

解决方案


推荐阅读