如何运行 为多个变量编写多次脚本?

How to run script multiple times for multiple variables?

所以我试图让这个脚本 运行 来自 ticker 变量的多个变量

示例ticker = ['NFLX','APPL']

我怎样才能循环此脚本,以便我可以 运行 它在多个变量上

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)

当前脚本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

我希望它仍然 return 相同的布局,但每个结果之间间隔开或用虚线分隔

我是编码的新手,所以我确信这是获得所需结果的非常麻烦的途径,因此,如果有人也可以提供建议以使其更整洁,我们也将不胜感激。

您可以将所有内容放入方法中,然后将参数 'ticker' 传递给它,一次在列表中传递一个变量。 或者,您可以传入整个列表并在方法内执行 for 循环。

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()

我会这样处理,首先你需要定义一个函数来保存抓取过程,然后我们将代码传递到一个名为 x 的变量中。新函数将被称为 scrape(x)

接下来,列出数组中的所有股票代码,并使用 python 的 for 命令循环遍历数组列表中每个股票代码的函数。请参阅下面修改后的代码。

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)