# 3.4 Google Finance 個股資訊

這隻要細心一點, 除了會把指定編號的股票資訊顯示出來之外, 另外也實作了取得不同頁歷史資訊的方法:

```python
import requests
from bs4 import BeautifulSoup

# To query the specific stock info, add "MARKET:STOCK_ID" after the url.
# e.g.: https://www.google.com/finance?q=[MARKET]:[STOCK_ID]
GOOGLE_FINANCE_INFO = "https://www.google.com/finance?q="
GOOGLE_FINANCE_HISTORY = "https://www.google.com/finance/historical?q="
CODE_TSMC = "TPE:2330"
FINANCE_HISTORY_START_BASE = 30
FINANCE_HISTORY_RECORD_SIZE = 30


def get_web_page(url, query):
    if query:
        resp = requests.get(url+query)
    else:
        resp = requests.get(url)

    if resp.status_code != 200:
        print('Invalid url: ', resp.url)
        return None
    else:
        return resp.text


def get_stock_info(dom):
    soup = BeautifulSoup(dom, 'html5lib')
    stock = dict()
    stock['name'] = soup.title.text.split(':')[0]
    stock['current_price'] = soup.find(id='price-panel').span.span.text
    stock['current_change'] = soup.find(id='price-panel').find('div', 'id-price-change').text.strip().replace('\n', '')
    for table in soup.find('div', 'snap-panel').find_all('table'):
        for tr in table.find_all('tr'):
            key = tr.find('td', 'key').text.lower().strip()
            value = tr.find('td', 'val').text.strip()
            stock[key] = value
    return stock


def get_stock_history(dom):
    soup = BeautifulSoup(dom, 'html5lib')
    table = soup.find('table', 'historical_price')
    header_row = table.find('tr', 'bb')
    headers = [header for header in header_row.stripped_strings]
    print(headers)
    for tds in table.find_all('tr')[1:]:
        print([data for data in tds.stripped_strings])


def main():
    finance_info_page = get_web_page(GOOGLE_FINANCE_INFO, CODE_TSMC)
    if finance_info_page:
        stock = get_stock_info(finance_info_page)
        for k, v in stock.items():
            print(k + ":", v)

    # To fetch history data by pagination, you need to add two query params: start and num.
    # Just like the following link:
    # https://www.google.com/finance/historical?q=TPE:2330&start=30&num=30
    for page in range(0, 3, 1):
        history_url = GOOGLE_FINANCE_HISTORY + CODE_TSMC + "&start=" + str(page * FINANCE_HISTORY_START_BASE) + "&num=" + str(FINANCE_HISTORY_RECORD_SIZE)
        finance_history_page = get_web_page(history_url, None)
        get_stock_history(finance_history_page)


if __name__ == '__main__':
    main()
```

輸出如下:

```
name: Taiwan Semiconductor Mfg. Co. Ltd.
current_price: 207.00
current_change: 0.00(0.00%)
range: 205.00 - 207.00
52 week: 153.50 - 208.50
open: 205.00
vol.: 32.56M
mkt cap: 5.37T
p/e: 15.03
div/yield: 7.00/2.90
eps: 13.77
shares: 25.93B
beta: -
inst. own: -
['Date', 'Open', 'High', 'Low', 'Close', 'Volume']
['May 26, 2017', '205.00', '207.00', '205.00', '207.00', '32,492,000']
['May 25, 2017', '206.00', '207.00', '205.50', '207.00', '22,311,000']
['May 24, 2017', '205.00', '206.00', '205.00', '205.50', '13,378,000']
['May 23, 2017', '205.00', '207.00', '204.50', '205.00', '19,988,000']
['May 22, 2017', '203.50', '205.00', '203.00', '205.00', '13,714,000']
['May 19, 2017', '203.50', '204.50', '202.50', '203.00', '16,483,000']
['May 18, 2017', '202.50', '204.00', '201.50', '203.50', '22,448,000']
['May 17, 2017', '203.00', '204.00', '203.00', '204.00', '21,558,000']
['May 16, 2017', '205.00', '205.00', '203.50', '204.50', '33,212,000']
['May 15, 2017', '204.00', '206.00', '204.00', '206.00', '25,655,000']
['May 12, 2017', '205.00', '207.00', '205.00', '206.00', '24,996,000']
['May 11, 2017', '204.50', '208.50', '204.00', '207.50', '43,692,000']
['May 10, 2017', '204.00', '206.00', '204.00', '205.50', '28,296,000']
['May 9, 2017', '205.50', '207.00', '203.50', '203.50', '48,047,000']
['May 8, 2017', '199.00', '202.50', '199.00', '202.50', '36,514,000']
['May 5, 2017', '197.00', '198.50', '197.00', '197.50', '17,022,000']
['May 4, 2017', '198.50', '199.00', '197.00', '198.00', '22,076,000']
['May 3, 2017', '198.00', '198.50', '197.00', '198.00', '25,702,000']
['May 2, 2017', '198.50', '199.00', '195.50', '196.50', '44,102,000']
['Apr 28, 2017', '193.50', '194.50', '193.00', '194.50', '34,837,000']
['Apr 27, 2017', '192.00', '193.00', '190.50', '193.00', '27,936,000']
['Apr 26, 2017', '192.00', '192.00', '190.50', '191.00', '29,905,000']
['Apr 25, 2017', '190.50', '192.00', '189.50', '192.00', '31,926,000']
['Apr 24, 2017', '191.00', '191.50', '188.50', '190.00', '19,312,000']
['Apr 21, 2017', '187.50', '190.00', '187.50', '190.00', '17,360,000']
['Apr 20, 2017', '186.50', '188.00', '186.50', '187.00', '21,487,000']
['Apr 19, 2017', '187.50', '187.50', '186.50', '186.50', '30,472,000']
['Apr 18, 2017', '188.00', '189.50', '187.50', '188.00', '24,964,000']
['Apr 17, 2017', '189.00', '189.00', '187.50', '187.50', '17,091,000']
['Apr 14, 2017', '189.50', '189.50', '188.00', '189.00', '30,662,000']
['Date', 'Open', 'High', 'Low', 'Close', 'Volume']
['Apr 13, 2017', '189.50', '192.00', '189.50', '191.50', '14,762,000']
['Apr 12, 2017', '190.50', '191.50', '190.00', '191.00', '17,270,000']
['Apr 11, 2017', '190.00', '191.00', '189.00', '191.00', '8,433,000']
['Apr 10, 2017', '191.00', '191.50', '189.50', '190.00', '17,005,000']
['Apr 7, 2017', '192.00', '192.00', '188.50', '191.00', '18,898,000']
['Apr 6, 2017', '193.00', '193.00', '191.00', '191.50', '16,170,000']
['Apr 5, 2017', '189.50', '193.00', '189.50', '193.00', '43,707,000']
['Mar 31, 2017', '191.00', '192.00', '189.00', '189.00', '33,621,000']
['Mar 30, 2017', '192.50', '193.00', '190.50', '191.50', '28,296,000']
['Mar 29, 2017', '194.50', '194.50', '191.00', '191.50', '33,367,000']
['Mar 28, 2017', '195.00', '195.00', '192.50', '194.50', '38,247,000']
['Mar 27, 2017', '194.50', '194.50', '192.00', '193.00', '29,239,000']
['Mar 24, 2017', '192.00', '193.00', '192.00', '192.50', '17,515,000']
['Mar 23, 2017', '192.00', '193.50', '192.00', '193.50', '18,463,000']
['Mar 22, 2017', '192.50', '193.50', '191.50', '193.50', '27,368,000']
['Mar 21, 2017', '192.00', '195.00', '191.50', '195.00', '32,234,000']
['Mar 20, 2017', '191.50', '191.50', '189.50', '191.50', '16,211,000']
['Mar 17, 2017', '189.50', '191.50', '189.00', '191.50', '33,690,000']
['Mar 16, 2017', '188.00', '190.00', '188.00', '190.00', '31,480,000']
['Mar 15, 2017', '185.50', '187.00', '185.50', '186.50', '14,129,000']
['Mar 14, 2017', '188.00', '188.00', '186.00', '186.00', '22,241,000']
['Mar 13, 2017', '185.00', '186.50', '185.00', '186.50', '10,475,000']
['Mar 10, 2017', '184.00', '184.50', '183.00', '183.50', '20,046,000']
['Mar 9, 2017', '185.50', '186.00', '184.00', '184.50', '28,483,000']
['Mar 8, 2017', '186.00', '188.00', '185.50', '187.00', '31,786,000']
['Mar 7, 2017', '184.00', '185.50', '184.00', '185.50', '18,791,000']
['Mar 6, 2017', '184.00', '184.50', '183.50', '183.50', '12,527,000']
['Mar 3, 2017', '184.50', '185.00', '184.00', '184.00', '28,085,000']
['Mar 2, 2017', '188.00', '188.50', '185.00', '186.00', '39,914,000']
['Mar 1, 2017', '188.50', '188.50', '186.00', '186.00', '46,176,000']
['Date', 'Open', 'High', 'Low', 'Close', 'Volume']
['Feb 24, 2017', '187.50', '190.50', '187.50', '189.00', '22,746,000']
['Feb 23, 2017', '188.00', '189.50', '187.00', '188.50', '35,720,000']
['Feb 22, 2017', '190.50', '191.00', '188.50', '188.50', '25,672,000']
['Feb 21, 2017', '190.00', '190.00', '188.50', '190.00', '31,138,000']
['Feb 17, 2017', '190.00', '190.50', '189.00', '189.50', '20,373,000']
['Feb 16, 2017', '190.00', '190.50', '188.00', '189.00', '26,986,000']
['Feb 15, 2017', '187.00', '189.50', '186.50', '189.00', '39,206,000']
['Feb 14, 2017', '189.00', '190.00', '187.50', '187.50', '45,690,000']
['Feb 13, 2017', '186.50', '188.00', '186.50', '187.50', '25,078,000']
['Feb 10, 2017', '184.50', '185.50', '184.00', '185.50', '34,694,000']
['Feb 9, 2017', '184.00', '185.50', '183.50', '184.00', '18,966,000']
['Feb 8, 2017', '183.50', '184.50', '183.00', '183.50', '29,144,000']
['Feb 7, 2017', '183.50', '185.00', '183.00', '184.50', '23,614,000']
['Feb 6, 2017', '184.50', '185.50', '184.00', '184.50', '24,767,000']
['Feb 3, 2017', '186.00', '186.00', '183.50', '184.50', '29,508,000']
['Feb 2, 2017', '188.00', '188.50', '184.00', '184.50', '94,068,000']
['Jan 24, 2017', '185.00', '186.50', '184.50', '185.50', '55,004,000']
['Jan 23, 2017', '182.50', '185.00', '182.50', '185.00', '36,985,000']
['Jan 20, 2017', '181.00', '181.50', '180.50', '181.00', '23,420,000']
['Jan 19, 2017', '179.50', '181.00', '179.50', '180.50', '24,595,000']
['Jan 18, 2017', '180.50', '181.00', '179.50', '181.00', '23,685,000']
['Jan 17, 2017', '180.50', '181.00', '179.50', '181.00', '13,148,000']
['Jan 16, 2017', '180.00', '180.50', '179.00', '179.50', '30,736,000']
['Jan 13, 2017', '180.50', '182.50', '180.50', '181.50', '52,340,000']
['Jan 12, 2017', '182.50', '185.50', '182.50', '184.50', '41,122,000']
['Jan 11, 2017', '185.00', '185.00', '181.50', '182.00', '29,089,000']
['Jan 10, 2017', '184.50', '185.50', '183.50', '184.00', '20,169,000']
['Jan 9, 2017', '184.00', '185.00', '183.00', '184.00', '18,555,000']
['Jan 6, 2017', '184.00', '184.50', '183.50', '184.00', '22,429,000']
['Jan 5, 2017', '182.00', '183.50', '181.50', '183.50', '20,962,000']

Process finished with exit code 0
```

~~十萬青年十萬肝 GG輪班救台灣~~

~~十萬青年十萬肝 GG輪班妹來含~~

原始碼[點我](https://github.com/yotsuba1022/web-crawler-practice/blob/master/ch3/google_finance.py)
