简体   繁体   English

如何在循环内将字典值保存到 python 中的 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.我正在尝试将抓取的结果保存到 CSV 文件中。

Each result returns as a dictionary.每个结果都作为字典返回。 I want to add each result to a row of the CSV file.我想将每个结果添加到 CSV 文件的一行。 What is happening is I am only getting the last result of the iteration saved to the CSV.发生的事情是我只得到保存到 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这是csv文件抓取后的结果

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

Open file in append mode.以 append 模式打开文件。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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