简体   繁体   中英

How to write table data from website to CSV through web scarping

I am newbie to web scraping I am trying to scrap a table data from website after login. I wish to multiply 2nd column by 10.

Currently the table is writing to csv but what I actually want to work is to multiply 2nd column by 10 and write to csv

What I have tried is:

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()

For example if I have a table with data in website as:

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

How to do so?

Its depends on how the elements are placed, here i have solution that you can apply it on 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM