所以我試圖讓這個腳本從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
我希望它仍然回傳相同的布局,但然后在每個結果之間間隔或用虛線分隔
我對編碼非常陌生,所以我確定這是獲得所需結果的非常麻煩的途徑,所以如果有人也能提供建議以使其更整潔,那也將不勝感激。
uj5u.com熱心網友回復:
我會以這種方式處理它,首先您需要定義一個保存抓取程序的函式,然后我們將股票代碼傳遞到一個名為 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)
uj5u.com熱心網友回復:
您可以將所有內容放入一個方法中,然后將一個引數“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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/369479.html
