Not saving lines when writing to CSV file using for-loop

mexicanRmy

So I'm trying to run a scrape of a website. The scraper runs very well. But whenever I try to write the scraped information/rows into the csv file it deletes the previous row. I end up just having the very last scrape result in the file at the end. I'm sure it's just an indentation error? I'm still new to Python so any help would be appreciated!

Code:

# create general field names
fields = ['name', 'about', 'job_title', 'location','company',
          'education','accomplishments','linkedin_url']

with open('ScrapeResults.csv', 'w') as f:
    # using csv.writer method from CSV package
    write = csv.writer(f)
    write.writerow(fields)
f.close()

# Loop-through urls to scrape multiple pages at once
for individual,link in contact_dict.items():

    ## assign ##
    the_name = individual
    the_link = link
    # scrape peoples url:
    person = Person(the_link, driver=driver, close_on_complete=False)

    # rows to be written
    rows = [[person.name, person.about, person.job_title, person.location, person.company,
             person.educations, person.accomplishments, person.linkedin_url]]
    # write
    with open('ScrapeResults.csv', 'w') as f:
    # using csv.writer method from CSV package
        write = csv.writer(f)
        write.writerows(rows)
        f.close()
anjaneyulubatta505

You will need to open the file in append mode.

change

with open('ScrapeResults.csv', 'w') as f:

to

with open('ScrapeResults.csv', 'a') as f:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unexpected amount of lines when writing to a csv file

Writing to a CSV file in Python using a 'for' loop

How to add alternating blank lines when saving csv file in Pandas

writing lines into text file with loop

Special charactes when writing to csv file using csv.to_csv

continuously writing to csv file in a loop

syntax when saving a file using dropna().to_csv

When saving scraped item and file, Scrapy inserts empty lines in output csv file

writing and saving CSV file from scraping data using python and Beautifulsoup4

Writing multiple lines to CSV file in Java with Firebase

Function to identify lines and writing in one file (csv)

Lines skipped when writing to csv in ruby

why skipping a line when writing in csv file using python 3.6.4?

Column sortorder when writing CSV file using CsvHelper

NullPointerException when writing csv file

Writing multiple lines to a .txt using a loop

Python: for loop and saving to a new CSV file with pandas

Whitespace between lines when writing to a file

Saving multiple dataframes to CSV using loop in python

Writing each line in loop to rows in csv file

csv writing alternate whitespace in lines within a loop python

Is there a memory efficient way to use 'using' within a recursive function when e.g. writing lines to a file?

Saving a downloaded CSV file using Python

Is there a way to delete or replace the next line in a csv file when using a loop?

How to prevent Tabular format when writing a parquet file into CSV file using pandas.DataFrame?

Empty CSV file when writing lots of data

Rows not filtering when writing to CSV file

preserving whitespace when writing file to csv

csv file gives strings when writing in floats

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive