I'm taking an intro Python class and I've been working on a problem to scrape top 10 most active tickers and get specific data points from yahoo finance. The assignment is to write the results to a comma separated text file. I have all the required data points (admittedly my code is not very elegant but it is returning the right results). The issue I'm facing is that no matter how I try to write the details to a csv file I get the TypeError: 'list' object is not callable. What can I do differently to create this file?
Also, I'm having trouble converting the PE ratio to 0.0 when it returns N/A.
Things I've tried:
- writing directly to a txt file in the for loop
- using import csv
Here is the latest version of my program.
from urllib.request import urlopen
import requests
import re
mytickerlist=['GE', 'CCL', 'F', 'BAC', 'PFE', 'WFC', 'NCLH', 'T', 'XOM', 'MRO'] #hardcoded for ease
tickerurl= 'https://finance.yahoo.com/quote/{myticker}?p={myticker}&.tsrc=fin-srch-v1'
for ticker in mytickerlist:
mystock_handle=requests.get(tickerurl.format(myticker = ticker))
mystock_text = mystock_handle.text
filehandle = open('projectoutput.txt','r') #read all the lines in the file into a list called lines
lines = filehandle.readlines()
filehandle.close()
#get open from yahoo
open = re.findall('data-reactid="103">(.*?[^< ]*)', mystock_text)
#get PE Ratio from yahoo
peratio = re.findall('data-reactid="149">(.*?[^< ]*)', mystock_text)
#if peratio =='N/A': #**need to convert N/A PE Ratio to 0...this block is not working
#peratio = 0
#print('PE Ratio is: ',peratio)
#else:
#print('PE Ratio is: ',peratio)
#get average vol from yahoo
avgvol = re.findall('data-reactid="131">(.*?[^< ]*)', mystock_text)
masterlist=[ticker,open[0],peratio[0],avgvol[0]]
#Append the list to the new text
lines.append(masterlist)
filehandle = open('projectoutput.txt','w')
#write all the lines into the file
filehandle.writelines(lines)
filehandle.close()
Here is the other version I attempted with the same results.
from urllib.request import urlopen
import requests
import re
mytickerlist=['GE', 'CCL', 'F', 'BAC', 'PFE', 'WFC', 'NCLH', 'T', 'XOM', 'MRO'] #hardcoded for ease
tickerurl= 'https://finance.yahoo.com/quote/{myticker}?p={myticker}&.tsrc=fin-srch-v1'
datatowrite=[]
for ticker in mytickerlist:
mystock_handle=requests.get(tickerurl.format(myticker = ticker))
mystock_text = mystock_handle.text
#get open from yahoo
open = re.findall('data-reactid="103">(.*?[^< ]*)', mystock_text)
#get PE Ratio from yahoo
peratio = re.findall('data-reactid="149">(.*?[^< ]*)', mystock_text)
#if peratio =='N/A': #**need to convert N/A PE Ratio to 0...this block is not working
#peratio = 0
#print('PE Ratio is: ',peratio)
#else:
#print('PE Ratio is: ',peratio)
#get average vol from yahoo
avgvol = re.findall('data-reactid="131">(.*?[^< ]*)', mystock_text)
masterlist=[ticker,open[0],peratio[0],avgvol[0]]
datatowrite.append(masterlist)
import csv
f= open('projectoutput', 'w')
csv_writer = csv.writer(f)
for i in datatowrite:
csv_writer.writerow(i)
f.close()