0

I am trying to save the data scraped from URLs such as "https://www.holidify.com/places/shimla/mall-road-shimla-sightseeing-3502.html". When saving the data in a csv file only the data from last url from the range gets saved in the csv file. I need that data from all of the URLs gets saved in the csv file.

pages = []
for i in range(1, 10, 1):
    url = "https://www.holidify.com/places/shimla/mall-road-shimla-sightseeing-350" + str(i) + '.html'
    pages.append(url)
    for item in pages:
        page = requests.get(item)
        soup = BeautifulSoup(page.text, 'html.parser')
        Place = list(soup.find(class_="col-md-10 col-xs-10 nopadding"))[1].get_text()
        City = list(soup.find_all(class_="smallerText"))[1].get_text()
        State = list(soup.find_all(class_="smallerText"))[2].get_text()
        Country = list(soup.find_all(class_="smallerText"))[3].get_text()
        About = list(soup.find_all(class_="biggerTextOverview"))[0].get_text()
        more_About = list(soup.find_all(class_="objHeading smallerText"))[0].get_text()
        Weather = soup.find(class_="currentWeather").get_text()
        demo = pd.DataFrame({ "Place": Place, "City": City, "State": State, "Country": Country, "About": About,"More About Places": more_About}, index=[0])
        demo.to_csv('demo.csv', index=False, encoding='utf-8')

2 Answers 2

1

You need to append data into that file

demo.to_csv('demo.csv', index=False, encoding='utf-8', mode = 'a')
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for the quick help. Now the all data does get saved in the csv file but each time with the coloumn names being printed with each entry.
@benazir there is option in to_csv to skip headers. You will have to use it in all loops except first. So maybe you should write headers before all loops and then write always without headers. Or put all data in list/dict and save it only once after forloop.
0

As suggested by @Umair append the data into the data frame and place the command demo.to_csv('demo.csv', index=False, encoding='utf-8') outside the loop.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.