File size: 1,299 Bytes
c109682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from bs4 import BeautifulSoup
import urllib.request
import pandas as pd

url = "https://en.wikipedia.org/wiki/Nasdaq-100"

def get_nasdaq100():
    # Set List
    SP500_list = []

    # html 정보 가져오기
    html = urllib.request.urlopen(url).read()
    soup = BeautifulSoup(html, 'html.parser')

    # html에서 table 정보 가져오기
    table = soup.findAll("table", {"class": "wikitable sortable"})[2]

    for row in table.findAll("tr")[1:]:

        ticker = get_ticker(row)
        name = get_name(row)
        sector = get_sector(row)
        industry = get_industry(row)

        SP500_list.append(return_comment_form(ticker, name, sector, industry))

    return SP500_list





def return_comment_form(ticker, name, sector, industry):
    comment = {'ticker': ticker,
               'name': name,
               'sector' : sector,
               'industry' : industry
               }
    return comment






def get_ticker(row):
    return row.select('td')[1].text.strip()

def get_name(row):
    return row.select('td')[0].text.strip()

def get_sector(row):
    return row.select('td')[2].text.strip()

def get_industry(row):
    return row.select('td')[3].text.strip()




if __name__ == '__main__':
    a = get_nasdaq100()

    print(pd.DataFrame(a))
    print("Finish")