Created
November 20, 2020 14:17
-
-
Save israel-dryer/350fe21e49bc9445f5b6827a668f247e to your computer and use it in GitHub Desktop.
Example - Saving data to CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import requests | |
from bs4 import BeautifulSoup | |
stock = ['AMZN','AXP','AAPL','AXTA','BAC'] | |
# create csv writer | |
f = open("data_file.csv", "w", newline="", encoding="utf-8") | |
writer = csv.writer(f) | |
# add header to csv file | |
writer.writerow(["Name", "Symbol", "Sector", "Industry", "FullTimeEmpl"]) | |
# extract the data | |
for i in range(0,3): | |
response = requests.get(url_profile.format(stock[i], stock[i])) | |
soup = bs4.BeautifulSoup(response.text, 'html.parser') | |
pattern = re.compile(r'\s--\sData\s--\s') | |
script_data = soup.find('script', text=pattern).contents[0] | |
start = script_data.find('context')-2 | |
json_data = json.loads(script_data[start:-12]) | |
long_name = json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['quoteType']['longName'] | |
symbol = json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['quoteType']['symbol'] | |
sector = json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['assetProfile']['sector'] | |
industry = json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['assetProfile']['industry'] | |
empl_count = json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['assetProfile']['fullTimeEmployees'] | |
# save record to csv file | |
writer.writerow([long_name, symbol, sector, industry, empl_count]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment