import pandas as pd
import requests
from bs4 import BeautifulSoup
import re
from json import loads
#stock_code = "0056"
stock_code = input("stock code:")
soup = BeautifulSoup(requests.get("tw.stock.yahoo.com/quote{}.TW/holding".format(stock_code)).content)
script = soup.find("script",string=re.compile("root.App.main")).text
data = re.search("root.App.main\s+=\s+(\{.*\})", script).group(1)
print(data)
result = re.findall("\[(.*?)\]",data,re.I|re.M)
dict_data = ""
for item in result:
if("ticker" in item and "weighting" in item):
dict_data = item
print(dict_data)
dict_data_mod ='{"holdingDetail":['+ dict_data + ']}'
print(dict_data_mod)
jsonData = dict_data_mod;
text = loads(jsonData)
print(text['holdingDetail'][0]['name'])
new = pd.DataFrame.from_dict(text['holdingDetail'])
print("{} top 10 constituent stock".format(stock_code))
new