Skip to content Skip to sidebar Skip to footer

How Can Write Scraped Content To A Csv File?

I need some help to save the output from a basic web scraper to a CSV file. Here is the code: from urllib.request import urlopen from bs4 import BeautifulSoup import csv html_ = u

Solution 1:

If the elements in nameList_ are rows with the columns delimited by ',' try this:

import csv

withopen('out.csv', 'w') as outf:
    writer = csv.writer(outf)
    writer.writerows(name.get_text().split(',') for name nameList_)

If nameList_.get_text() is just a string and you want to write a single column CSV, you might try this:

import csv

withopen('out.csv', 'w') as outf:
    writer = csv.writer(outf)
    writer.writerows([name.get_text()] for name in nameList_)

Post a Comment for "How Can Write Scraped Content To A Csv File?"