15

I am trying to load data from the web source and save it as a Excel file but not sure how to do it. What should I do?

import requests
import pandas as pd
import xmltodict


url = "https://www.kstan.ua/sitemap.xml"
res = requests.get(url)
raw = xmltodict.parse(res.text)

data = [[r["loc"], r["lastmod"]] for r in raw["urlset"]["url"]]
print("Number of sitemaps:", len(data))
df = pd.DataFrame(data, columns=["links", "lastmod"])
lv10
  • 1,469
  • 7
  • 25
  • 46
Игорь
  • 151
  • 1
  • 1
  • 3
  • Assuming that you don't have the any issues with reading in the XML all you have to do is: `df.to_excel('out.xlsx')` to learn more about `DataFrame.to_excel` go to: to learn more about I/O for pandas dataframes visit: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_excel.html#pandas.DataFrame.to_excel https://pandas.pydata.org/pandas-docs/stable/reference/frame.html#serialization-io-conversion – lv10 Mar 14 '19 at 19:20

3 Answers3

23
df.to_csv("output.csv", index=False)

OR

df.to_excel("output.xlsx")
mujjiga
  • 16,186
  • 2
  • 33
  • 51
3

You can write the dataframe to excel using the pandas ExcelWriter, such as this:

import pandas as pd
with pd.ExcelWriter('path_to_file.xlsx') as writer:
    dataframe.to_excel(writer)
CodyF
  • 4,977
  • 3
  • 26
  • 38
razimbres
  • 4,715
  • 5
  • 23
  • 50
1

If you want to create multiple sheets in the same file

with pd.ExcelWriter('csv_s/results.xlsx') as writer:
   same_res.to_excel(writer, sheet_name='same')
   diff_res.to_excel(writer, sheet_name='sheet2')