Search code examples
pythoncsvweb-scrapingexport-to-csv

How to save a dictionary value to a csv file in python within a loop?


I am trying to save scraped results to a CSV file.

Each result returns as a dictionary. I want to add each result to a row of the CSV file. What is happening is I am only getting the last result of the iteration saved to the CSV. I'm not sure why this is happening any help to fix the issue would be appreciated.

class GoogleMapScraper:

    def __init__(self):
        self.PATH = "/Users/laurabrooks/code/LHB410/scraping/chromedriver"
        self.driver = webdriver.Chrome(self.PATH)
        self.business_info = {}
        self.business_list = []
        self.business_info["name"] = "NA"
        self.business_info["rating"] = "NA"
        self.business_info["reviews_count"] = "NA"
        self.business_info["address_en"] = "NA"
        self.business_info["contact"] = "NA"
        self.business_info["website"] = "NA"

      # the actual scraping part of each place site
    def get_business_info(self, url):
        self.driver.get(url)
        self.driver.implicitly_wait(10)
        # Parse data out of the page
        self.business_info["name"] = self.driver.find_element(
            By.CLASS_NAME, "DUwDvf").text
        self.business_info["rating"] = self.driver.find_element(
            By.CLASS_NAME, "mmu3tf").text
        self.business_info["reviews_count"] = self.driver.find_element(
            By.CLASS_NAME, "DkEaL").text
        self.business_info["address_en"] = self.driver.find_elements(
            By.CLASS_NAME, "Io6YTe")[0].text

        self.business_info["website"] = self.driver.find_elements(
            By.CLASS_NAME, "Io6YTe")[2].text
        self.business_info["contact"] = self.driver.find_elements(
            By.CLASS_NAME, "Io6YTe")[3].text



        #add business info to business list
        # self.business_list.append(self.business_info)


links = results['link']

BusinessScraper = GoogleMapScraper()
for url in links:
    BusinessScraper.get_business_info(url)
    print(BusinessScraper.business_info)
    row = [BusinessScraper.business_info]

    fieldnames = ["name", "rating", "reviews_count", "address_en", "website", "contact"]

    with open('scrape_results.csv', 'w', encoding='UTF8', newline='') as csvfile:
          writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
          writer.writeheader()
          writer.writerows(row)

This is the csv file result after scraping

name,rating,reviews_count,address_en,website,contact
Time Out,"4.0
81 reviews",81 reviews,"〒150-0011 Tokyo, Shibuya City, Higashi, 3 Chome−16−6 リキッドルーム 2F",Floor 2 · LIQUIDROOM,timeoutcafe.jp


Solution

  • Open file in append mode. Maybe somehting like this:

    your code
    # Open our existing CSV file in append mode
    # Create a file object for this file
    with open('event.csv', 'a') as f_object:
      
        # Pass this file object to csv.writer()
        # and get a writer object
        writer_object = writer(f_object)
        your code