When scraping and saving data into a file, Which method is more efficient when saving scraped data to a file?
- open the
file
first, scrape, and save the data all while the file is opened, or - store the data into a dictionary and then save it to the
file
?
For example, the following two scripts scrape data from Yahoo Finance. In the first method the file
is opened first, the data is scraped and saved to file
while the file
is opened.
import csv
from requests_html import HTMLSession
URL = 'https://finance.yahoo.com/lookup/'
def get_page(url):
session = HTMLSession()
r = session.get(url)
r.raise_for_status()
return r.html
# Opening the file first
with open('yahoo_finance.csv', 'w', newline='', encoding='utf8') as file:
dictWriter = csv.DictWriter(file,
fieldnames=['Symbol', 'Name', 'lastPrice', 'Change', 'percentChange'],
quoting=csv.QUOTE_MINIMAL,
quotechar="'"
)
dictWriter.writeheader()
content = get_page(URL)
table_rows = content.find('tbody', first=True).find('tr')
for row in table_rows:
symbol = row.find('td')[0].text
name = row.find('td')[1].text
last_price = row.find('td')[2].text
change = float(row.find('td')[3].text.lstrip('+'))
percent_change = float(row.find('td')[4].text.lstrip('+').rstrip('%'))
data = {'Symbol': symbol,
'Name': name,
'lastPrice': last_price,
'Change': change,
'percentChange': percent_change}
# Saving data
dictWriter.writerow(data)
In the second method, the data is scraped, saved to a list, and then the data is written to csv file
.
import csv
from requests_html import HTMLSession
URL = 'https://finance.yahoo.com/lookup/'
def get_page(url):
session = HTMLSession()
r = session.get(url)
r.raise_for_status()
return r.html
content = get_page(URL)
table_rows = content.find('tbody', first=True).find('tr')
records = []
for row in table_rows:
symbol = row.find('td')[0].text
name = row.find('td')[1].text
last_price = row.find('td')[2].text
change = float(row.find('td')[3].text.lstrip('+'))
percent_change = float(row.find('td')[4].text.lstrip('+').rstrip('%'))
data = {'Symbol': symbol,
'Name': name,
'lastPrice': last_price,
'Change': change,
'percentChange': percent_change}
# Saving data to list:
records.append(data)
# Then opening the file:
with open('yahoo_finance.csv', 'w', newline='', encoding='utf8') as file:
dictWriter = csv.DictWriter(file,
fieldnames=['Symbol', 'Name', 'lastPrice', 'Change', 'percentChange'],
quoting=csv.QUOTE_MINIMAL,
quotechar="'"
)
dictWriter.writeheader()
for row in records:
# Saving data to file:
dictWriter.writerow(row)
Questions:
Is the first method more efficient because it skips the need for
appending
to a list, and the extrafor loop
to save the data to a file?Are there any taboos for performing operations on an
open
file within a context manager as in the first method?
scraping
, toscrape
andsave
the data, correct? Now, with all due respect, please help me understand the reasonwhy
I am saving data is relevant to answering my question - Is this the wrong platform to ask these type of questions? Thanks! \$\endgroup\$monospace code blocks
on words that aren't code. \$\endgroup\$