python - 如何将这组数据从垂直保存到水平
问题描述
我需要将数据保存在行中,但脚本将其保存在每个数据的新行中-
这是我的代码和 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 目标是以水平格式导出数据
请帮忙
解决方案
推荐阅读
- ssl - 使域指向另一个 https 域
- nginx - 如何在 nginx 配置文件中设置最大 mss
- typescript - 棘手的装饰类
- php - 在阿里巴巴 ECS 上使用 SSL 的视频流
- pyspark - 在 PySpark 中对具有空值的列应用计数、总和等聚合函数时出现问题
- activemq-artemis - Apache Artemis 没有 clientId 显示在 management-console.UI 的使用者部分
- mysql - 使用 sql 和 vb.net 将条目更改为指定范围数
- java - 使用流过滤 POJO 列表 - 无法投射 ClassCastException java.util.LinkedHashMap
- amazon-web-services - 如何将 iam:PermissionsBoundary 添加到通过 CloudFormation 模板创建的角色
- mysql - 无法添加或更新子行: