python - 如何通过网络抓取将表格数据从网站写入 CSV
问题描述
我是网络抓取的新手,我正在尝试在登录后从网站上抓取表格数据。我希望将第二列乘以 10。
目前该表正在写入 csv 但我真正想要的工作是将第二列乘以 10 并写入 csv
我尝试过的是:
r2=session.post("http://www.example.com")
soup = BeautifulSoup(r2.text, "html.parser")
csvFile=open('Table.csv','w')
output = csv.writer(csvFile)
for table in soup.find_all('table')[5:]:
for row in table.find_all('tr'):
col = map(cell_text, row.find_all(re.compile('t[dh]')))
output.writerow(col)
output.writerow([])
csvFile.close()
例如,如果我在网站上有一个包含数据的表格:
Time Pressure Mass Temp
0.00 1.01 21 23.09
1.00 2.0908 21.1 10.07
2.0 2.8666 22.3 13.6
0.555 2.6545 2.4 32.56
The data for writing csv file should be:
0.00 10.1 21 23.09
1.00 20.908 21.1 10.07
2.0 28.666 22.3 13.6
0.555 26.545 2.4 32.56
怎么做?
解决方案
它取决于元素的放置方式,在这里我有可以将其应用于 csv 的解决方案。
import pandas as pd
df = pd.read_csv("Table.csv")
df.Pressure = df.Pressure * 10
df.to_csv("Table_Updated.csv",index=False)
df.to_csv("DataExport.csv",index=False,header=False) # Store without header
推荐阅读
- python-2.7 - 是什么导致了这个错误?请求 url 中缺少方案:h
- influxdb - 通过文本文件导入信息,每 1 分钟更新一次 Influxdb 数据库
- python - Pandas read_html 不会从维基百科页面收集所有表格
- c++ - 为什么我不能将迭代器应用于接受 const_iterator 引用的函数?
- python - 在 R 中用 igraph 绘制图形:边长与权重成正比
- json - 使用 yaml 对 Swagger API 描述进行“模板化”
- python - SQLAlchemy - 计算用户输入变量的总百分比
- javascript - 为什么我在 php foreach 循环中的 jQuery show() 函数仅适用于第一个元素
- jquery - Onclick 事件仅适用于双击
- .htaccess - htacess - 在 url 中使用可选值