Home > Software design >  How to run script multiple times for multiple variables?
How to run script multiple times for multiple variables?

Time:11-26

So Im trying to have this script run multiple variables from the ticker variable

Example ticker = ['NFLX','APPL']

How would I be able to loop this script so that I can run it on more than one variable

import requests
from bs4 import BeautifulSoup

ticker = 'NFLX'

url = 'https://finance.yahoo.com/quote/'   ticker

r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')

name = soup.find('div', {'class':'Mt(15px)'}).find_all('h1')[0].text
price = soup.find('div', {'class':'D(ib) Mend(20px)'}).find_all('span')[0].text
change = soup.find('div', {'class':'D(ib) Mend(20px)'}).find_all('span')[1].text
cap = soup.find('div', {'class':'D(ib) W(1/2) Bxz(bb) Pstart(12px) Va(t) ie-7_D(i) ie-7_Pos(a) smartphone_D(b) smartphone_W(100%) smartphone_Pstart(0px) smartphone_BdB smartphone_Bdc($seperatorColor)'}).find_all('span')[0].text
capnumber = soup.find('div', {'class':'D(ib) W(1/2) Bxz(bb) Pstart(12px) Va(t) ie-7_D(i) ie-7_Pos(a) smartphone_D(b) smartphone_W(100%) smartphone_Pstart(0px) smartphone_BdB smartphone_Bdc($seperatorColor)'}).find_all('span')[1].text
topnews = soup.find('h3', {'class':'Mb(5px)'}).find_all('a')[0].text

print(name)
print ('https://finance.yahoo.com/quote/'   ticker)
print("Last Price:",price)
print("Change:", change)
print(cap,":", capnumber)
print("Top News:", topnews)

The current script returns

Netflix, Inc. (NFLX)
https://finance.yahoo.com/quote/NFLX
Last Price: 658.29
Change:  4.23 ( 0.65%)
Market Cap : 291.591B
Top News: Russia investigates Netflix after complaint over LGBT content

I would like it to still return the same layout but then spaced between each result or separated by a dash line

Im super new to coding, so Im sure this is a very cumbersome path to get the desired result, so if someone can provide suggestions too to make it neater that would be appreciated as well.

CodePudding user response:

I'd approach it this way, first you need to define a function that holds the scraping process then we pass the ticker in a variable called x. New function would be called scrape(x)

Next, list all tickers in an array and use python's for command to loop through the function for each ticker in our array list. See modified code below.

import requests
from bs4 import BeautifulSoup

def scrape(x):
    ticker = x

    url = 'https://finance.yahoo.com/quote/'   ticker

    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')

    name = soup.find('div', {'class':'Mt(15px)'}).find_all('h1')[0].text
    price = soup.find('div', {'class':'D(ib) Mend(20px)'}).find_all('span')[0].text
    change = soup.find('div', {'class':'D(ib) Mend(20px)'}).find_all('span')[1].text
    cap = soup.find('div', {'class':'D(ib) W(1/2) Bxz(bb) Pstart(12px) Va(t) ie-7_D(i) ie-7_Pos(a) smartphone_D(b) smartphone_W(100%) smartphone_Pstart(0px) smartphone_BdB smartphone_Bdc($seperatorColor)'}).find_all('span')[0].text
    capnumber = soup.find('div', {'class':'D(ib) W(1/2) Bxz(bb) Pstart(12px) Va(t) ie-7_D(i) ie-7_Pos(a) smartphone_D(b) smartphone_W(100%) smartphone_Pstart(0px) smartphone_BdB smartphone_Bdc($seperatorColor)'}).find_all('span')[1].text
    topnews = soup.find('h3', {'class':'Mb(5px)'}).find_all('a')[0].text

    print(name)
    print ('https://finance.yahoo.com/quote/'   ticker)
    print("Last Price:",price)
    print("Change:", change)
    print(cap,":", capnumber)
    print("Top News:", topnews)
    print("\n")

tickerArray = ["NFLX", "MRK", "ADSK"]
for x in tickerArray:
    scrape(x)

CodePudding user response:

You can put everything into a method, then pass a parameter 'ticker' into it, one variable in the list at a time. Alternatively you can pass the whole list in and do the for loop inside the method.

import requests
from bs4 import BeautifulSoup
#given input
tickers = ['NFLX','APPL']

def urlDisplay(ticker):
    url = 'https://finance.yahoo.com/quote/'   ticker

    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')

    name = soup.find('div', {'class':'Mt(15px)'}).find_all('h1')[0].text
    price = soup.find('div', {'class':'D(ib) Mend(20px)'}).find_all('span')[0].text
    change = soup.find('div', {'class':'D(ib) Mend(20px)'}).find_all('span')[1].text
    cap = soup.find('div', {'class':'D(ib) W(1/2) Bxz(bb) Pstart(12px) Va(t) ie-7_D(i) ie-7_Pos(a) smartphone_D(b) smartphone_W(100%) smartphone_Pstart(0px) smartphone_BdB smartphone_Bdc($seperatorColor)'}).find_all('span')[0].text
    capnumber = soup.find('div', {'class':'D(ib) W(1/2) Bxz(bb) Pstart(12px) Va(t) ie-7_D(i) ie-7_Pos(a) smartphone_D(b) smartphone_W(100%) smartphone_Pstart(0px) smartphone_BdB smartphone_Bdc($seperatorColor)'}).find_all('span')[1].text
    topnews = soup.find('h3', {'class':'Mb(5px)'}).find_all('a')[0].text

    print(name)
    #print('https://finance.yahoo.com/quote/'   ticker)
    #can just print url variable straight away since you declared it already above.
    print(url)
    print("Last Price:",price)
    print("Change:", change)
    print(cap,":", capnumber)
    print("Top News:", topnews)
#driver code
def main():
    for i in tickers:
        urlDisplay(i)
        print('-' * 20)
if __name__ == "__main__":
    main()
  • Related