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)