如何為多個變量多次運行腳本?

所以我試圖讓這個腳本從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)

當前腳本返回

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

我希望它仍然返回相同的佈局,但然後在每個結果之間間隔或用虛線分隔

我對編碼非常陌生,所以我確定這是獲得所需結果的非常麻煩的途徑,所以如果有人也能提供建議以使其更整潔,那也將不勝感激。

Jyles Jin

您可以將所有內容放入一個方法中,然後將一個參數“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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何從使用 bash 腳本運行的 C 程序中獲取變量的值?

為多個向量運行我的多變量函數?

如何運行一個接受多個用戶完整輸入的 bash 腳本,作為 dockerfile 的一部分

使用 nohup 依次運行多個 python 腳本

如何在多個數據目錄上運行我的腳本

運行 Python/PySpark 腳本時出現環境變量錯誤

將包含多個 ID 號的 Python 變量傳遞到外部 BigQuery 腳本

如何將多個變量重構為一個帶參數的變量?

如何為在 docker 容器內運行的 corda 節點運行遷移腳本?

如何讀取“ENV”文件變量並在 Python 中作為單個命令運行

如何從另一個 Python 腳本訪問 Python 腳本的動態變量?

在bash腳本中一次運行多個循環

有沒有辦法在 html 站點上運行多個腳本

使用在其他文件中找到的不同變量運行作業腳本

LUA - 如何停止運行腳本

如何迭代多個變量並使用 Shell 腳本回顯它們?

如何在不發送變量值的情況下從另一個腳本編寫腳本中的變量?

在使用特殊變量 $1, $# 的 bash 腳本中創建一個 bash 腳本

如何在 R at nce 中將 tibble 的多個變量突變為相同的值

如何檢查多個變量的相同條件並為每個變量返回匹配消息?

如何將變量傳遞給驅動程序執行腳本?

如何為多個輸入分配一個整體變量?

Laravel 8,如何在後台運行一個作業(腳本)?

如何從腳本在 xpath 中傳遞變量?

不能為 3 個變量分配多個值

如何在 gitlab-ci 腳本中將 docker inspect 的結果設置為 env 變量

如何將“z”存儲為包含多個值的變量?

如何將多個參數作為變量傳遞給應用程序?

在python中將多個變量作為一個變量傳遞