I have scraped data (basically the train details like No, Name, Type, Zone etc) from a website using the below code in jupyter notebook:
How can I put the result obtained in 'output' into a DataFrame and then into a csv file?
import requests
from bs4 import BeautifulSoup
import pandas as pd
r=requests.get("https://indiarailinfo.com/arrivals/kanpur-central-cnb/452")
print(r.text[0:200000])
soup=BeautifulSoup(r.text,'html.parser')
results=soup.find_all('div',attrs={'class':'tdborder'})
results1=soup.find_all('div',attrs={'class':'tdborderhighlight'}) //for 'To' and 'Sch'
lresult=results[11:570]
lresult
for i in range(11,550):
output=lresult[i].text
print(output)
You need to dump every thing in a numpy arrange (easiest way) Then use the object to export it EXAMPLE
import numpy
a = numpy.asarray([ [1,2,3], [4,5,6], [7,8,9] ])
numpy.savetxt("foo.csv", a, delimiter=",").
I'm not entirely sure how you want the output csv to look like, but you could try something like this to convert your data to a dataframe, then output to a csv:
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = 'https://indiarailinfo.com/arrivals/kanpur-central-cnb/452'
html = requests.get(url).text
soup = BeautifulSoup(html, 'lxml')
res = soup.find_all('div',attrs={'class':'tdborder'})
headers = [header.text.strip() for header in res[:11]]
lines = [[x.text.strip() for x in res[11:][i:i+11]] for i in range(0, len(res[11:]), 11)]
df = pd.DataFrame(lines, columns=headers)
df.to_csv('trains.csv', encoding='utf-8', index=False)
print(open('trains.csv', 'r').read())
Which gives this csv:
No.,Name,Type,Zone,PF,Arrival Days,From,Sch,Delay,ETA,LKL
12303,Poorva Express (via Patna) (PT),SF,ER,1,S TW S,HWH,08:05,3h 53m late,03:58,DER/Dadri
12381,Poorva Express (via Gaya) (PT),SF,ER,1,M TF,HWH,08:15,no arr today,no arr today,n/a
11015,Kushinagar Express (PT),Exp,CR,6,SMTWTFS,LTT,22:45,57m late,01:07,GKP/Gorakhpur Junction
...
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.