WFU

2023年1月4日 星期三

python | 爬蟲_下載外資每日買賣超


#外資每日買賣超

import requests
from bs4 import BeautifulSoup

#輸入股票代號
stock_num = 2330

#查詢網址
url = "https://tw.stock.yahoo.com/quote/" +str(stock_num)+ "/institutional-trading"

# stock_name

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


print("股票代號:"+ str(stock_num))


#印出外資買賣超
for foreign_total in soup.find_all('span',  class_="Jc(fe)") [3] :
    if  "-" in foreign_total :
        
        print("【外資賣超】:"+ foreign_total + "張")
    
    else:
        print("【外資買超】:"+ foreign_total + "張")