-
-
Save yishingene/c8ca0e36b4bf93f31fc843861b56792d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 股價收盤價抓取練習.py | |
| 只能抓取近一個月,因為網站提供資料有限 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import urllib.request | |
| import lxml.etree as etree | |
| import datetime | |
| def get_date_close_price(stocknum,date_string,date_format='%Y/%m/%d'): | |
| #ex:'2330','2015/8/24','%Y/%m/%d' | |
| #dateobj.strftime('%#m')可以 08 => 8 in windows | |
| #dateobj.strftime('%-m')可以 08 => 8 in linux? | |
| dateobj = datetime.datetime.strptime(date_string,date_format) | |
| #print(dateobj) | |
| stocknum = str(stocknum) | |
| try: | |
| data = urllib.request.urlopen('http://www.cnyes.com/twstock/ps_historyprice/'+str(stocknum)+'.htm').read().decode('utf-8',errors='ignores') | |
| except Exception as e: | |
| #return e.args | |
| return 'null' | |
| page = etree.HTML(data) | |
| #print(page) | |
| #print(dateobj.strftime('%Y/%m/%d')) | |
| try: | |
| return page.xpath('//tr/td[contains(text(),"'+ dateobj.strftime('%Y/%m/%d') +'")]/following-sibling::*')[3].text | |
| except Exception as e: | |
| #return e.args | |
| return 'null' | |
| #datetime.datetime.strptime('2015/8/24','%Y/%m/%d') | |
| get_date_close_price('2330','2015/09/17') | |
| #data = urllib.request.urlopen('http://www.cnyes.com/twstock/ps_historyprice/1101.htm').read().decode('utf-8',errors='ignores') | |
| #page = etree.HTML(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment