I'm wanting to scrape data on different shoe companies. I am trying to scrape the EPS from yahoo-finance however I cant find anywhere how to do this. The only way I see so far is finding stock data as open,close etc. How can I scrape data from yahoo-fiance that is not stock data.
Asked
Active
Viewed 328 times
-3
-
1What did you try? Can you show us a snippet of code? – Maaz Feb 07 '20 at 10:29
2 Answers
0
This will grab the metrics you want and write everything to a CSV file.
import csv
import requests
from bs4 import BeautifulSoup
url_base = "https://finviz.com/quote.ashx?t="
tckr = ['SBUX','MSFT','AAPL']
url_list = [url_base + s for s in tckr]
with open('C:/your_path_here/metrics.csv', 'a', newline='') as f:
writer = csv.writer(f)
for url in url_list:
try:
fpage = requests.get(url)
fsoup = BeautifulSoup(fpage.content, 'html.parser')
# write header row
writer.writerow(map(lambda e : e.text, fsoup.find_all('td', {'class':'snapshot-td2-cp'})))
# write body row
writer.writerow(map(lambda e : e.text, fsoup.find_all('td', {'class':'snapshot-td2'})))
except HTTPError:
print("{} - not found".format(url))

ASH
- 20,759
- 19
- 87
- 200
0
Here is another approach for you. Notice: by changing the URL, you can or delete different metrics to download.
import requests
from bs4 import BeautifulSoup
base_url = 'http://finviz.com/screener.ashx?v=152&s=ta_topgainers&o=price&c=0,1,2,3,4,5,6,7,25,63,64,65,66,67'
html = requests.get(base_url)
soup = BeautifulSoup(html.content, "html.parser")
main_div = soup.find('div', attrs = {'id':'screener-content'})
light_rows = main_div.find_all('tr', class_="table-light-row-cp")
dark_rows = main_div.find_all('tr', class_="table-dark-row-cp")
data = []
for rows_set in (light_rows, dark_rows):
for row in rows_set:
row_data = []
for cell in row.find_all('td'):
val = cell.a.get_text()
row_data.append(val)
data.append(row_data)
# sort rows to maintain original order
data.sort(key=lambda x: int(x[0]))
import pandas
pandas.DataFrame(data).to_csv("C:/Users/ryans/OneDrive/Desktop/today.csv", header=False)

ASH
- 20,759
- 19
- 87
- 200