def getJsonObj2(obj):
if not obj: return None
if hasHTML(obj): return None
partern = re.compile("data:.*?\"]")
list = re.findall(partern, obj)
if list and len(list) > 0:
s = list[0]
sepString = s.split(':')[1]
return simplejson.loads(sepString)
else:
return None
python类split()的实例源码
def getJsonObj3(obj):
if not obj: return None
if hasHTML(obj): return None
partern = re.compile("data:.*?\"]")
list = re.findall(partern, obj)
if list and len(list) > 0:
s = list[0]
sepString = s.split(':[')[1]
return simplejson.loads('[' + sepString)
else:
return None
def getJsonObj4(obj):
if not obj: return None
if hasHTML(obj): return None
partern = re.compile("rank:.*?\"]")
list = re.findall(partern, obj)
if list and len(list) > 0:
s = list[0]
sepString = s.split(':[')[1]
return simplejson.loads('[' + sepString)
else:
return None
def getJsonObj5(obj):
if not obj: return None
if hasHTML(obj): return None
partern = re.compile("\"Value\":.*?\"]")
list = re.findall(partern, obj)
if list and len(list) > 0:
s = list[0]
sepString = s.split(':[')[1]
return simplejson.loads('[' + sepString)
else:
return None
def getThreeDaysMaxStockList(self):
'''???????'''
res = getHtmlFromUrl(threeDaysMaxPriceUrl)
companyListObj = getJsonObj(res)
if companyListObj:
list = companyListObj['Results']
cList = []
if list and len(list):
for item in list:
stockInfo = item.split(',')
cinfo = CompanyInfo(stockInfo[1],stockInfo[2])
cList.append(cinfo)
return cList
return None
def getFiveDaysMaxStockList(self):
'''??5????'''
res = getHtmlFromUrl(fiveDaysMaxPriceUrl)
companyListObj = getJsonObj(res)
if companyListObj:
list = companyListObj['Results']
cList = []
if list and len(list):
for item in list:
stockInfo = item.split(',')
cinfo = CompanyInfo(stockInfo[1],stockInfo[2])
cList.append(cinfo)
return cList
return None
def getRcommandRankList(self):
'''????????'''
res = getHtmlFromUrl(reommendRankUrl)
companyListObj = getJsonObj(res)
if companyListObj:
list = companyListObj['data']
cList = []
if list and len(list):
for item in list:
li = item.split(',')
info = CompanyRecommandRankInfo(li[1],
li[2],li[5],li[6], li[7])
cList.append(info)
return cList
return None
def getStockNameFromCode(self,code):
res = getHtmlFromUrl(companyNameUrl % code,utf8coding=True)
pa = re.compile('=.*?;')
li = re.findall(pa,res)
if li and len(li):
s = li[0]
ret = (s[1:-1]).split(',')
if ret and len(ret):
return ret[4]
else:
return None
def getGoodFundList(self):
fl = []
res = getHtmlFromUrl(goodFundUrl)
fundList = getJsonObj7(res)
if fundList:
for item in fundList['datas']:
fundArray = item.split(',')
m = FundDetailModel(fundArray[0],fundArray[1],fundArray[3], fundArray[5],fundArray[6],fundArray[7],fundArray[8],fundArray[4],fundArray[11])
fl.append(m)
return fl
def getStockPriceEachMonth(self,code,onlyYears=False):
'''??????'''
res = getHtmlFromUrl(companyStockPriceEachMonth % (code+getMarketId(code)))
if not res: return None
if hasHTML(res): return None
partern = re.compile("\({\"name\":.*?}\)")
rel = re.findall(partern, res)
mList = []
if rel and len(rel) > 0:
s = rel[0]
m = simplejson.loads(s[1:-1])
if m:
plist = m['data']
if plist and len(plist):
for i in plist:
parray = i.split(',')
time = parray[0]
if onlyYears:
if time and getFloatFromString(time.split('-')[1]) == 12:
pmodel = StockEachMonthInfo(code,m['name'],parray[0],parray[1],parray[2],parray[3],parray[4])
mList.append(pmodel)
else:continue
else:
pmodel = StockEachMonthInfo(code, m['name'], parray[0], parray[1], parray[2], parray[3],parray[4])
mList.append(pmodel)
else:
return None
if mList and len(mList) > 0:
return mList
else:return None
def getWeekKLineForCode(self,code):
dataList = []
url = weekKLineUrl % (code + getMarketId(code))
res = getHtmlFromUrl(url)
if not res:return None
obj = getJsonObj6(res)
if not obj:return None
priceList = obj['data']
for data in priceList:
dList = data.split(',')
if len(dList) < 8:continue
d = CompanyKLineDetailDataList(dList[0],dList[1],dList[2],dList[3],dList[4],dList[5],dList[6],dList[7])
dataList.append(d)
detail = CompanyKLineDataDetail(obj['code'],obj['name'],dataList)
return detail
def get_year(time):
year = time.split('-')[0]
return int(year)
# ????????????2010???????[????????????????]
def fromChunk(data):
"""Convert chunk to string.
@returns: tuple (result, remaining), may raise ValueError.
"""
prefix, rest = data.split('\r\n', 1)
length = int(prefix, 16)
if length < 0:
raise ValueError("Chunk length must be >= 0, not %d" % (length,))
if not rest[length:length + 2] == '\r\n':
raise ValueError, "chunk must end with CRLF"
return rest[:length], rest[length + 2:]
def parseContentRange(header):
"""Parse a content-range header into (start, end, realLength).
realLength might be None if real length is not known ('*').
"""
kind, other = header.strip().split()
if kind.lower() != "bytes":
raise ValueError, "a range of type %r is not supported"
startend, realLength = other.split("/")
start, end = map(int, startend.split("-"))
if realLength == "*":
realLength = None
else:
realLength = int(realLength)
return (start, end, realLength)
def setLastModified(self, when):
"""Set the X{Last-Modified} time for the response to this request.
If I am called more than once, I ignore attempts to set
Last-Modified earlier, only replacing the Last-Modified time
if it is to a later value.
If I am a conditional request, I may modify my response code
to L{NOT_MODIFIED} if appropriate for the time given.
@param when: The last time the resource being returned was
modified, in seconds since the epoch.
@type when: number
@return: If I am a X{If-Modified-Since} conditional request and
the time given is not newer than the condition, I return
L{http.CACHED<CACHED>} to indicate that you should write no
body. Otherwise, I return a false value.
"""
# time.time() may be a float, but the HTTP-date strings are
# only good for whole seconds.
when = long(math.ceil(when))
if (not self.lastModified) or (self.lastModified < when):
self.lastModified = when
modified_since = self.getHeader('if-modified-since')
if modified_since:
modified_since = stringToDatetime(modified_since.split(';', 1)[0])
if modified_since >= when:
self.setResponseCode(NOT_MODIFIED)
return CACHED
return None
def setETag(self, etag):
"""Set an X{entity tag} for the outgoing response.
That's \"entity tag\" as in the HTTP/1.1 X{ETag} header, \"used
for comparing two or more entities from the same requested
resource.\"
If I am a conditional request, I may modify my response code
to L{NOT_MODIFIED} or L{PRECONDITION_FAILED}, if appropriate
for the tag given.
@param etag: The entity tag for the resource being returned.
@type etag: string
@return: If I am a X{If-None-Match} conditional request and
the tag matches one in the request, I return
L{http.CACHED<CACHED>} to indicate that you should write
no body. Otherwise, I return a false value.
"""
if etag:
self.etag = etag
tags = self.getHeader("if-none-match")
if tags:
tags = tags.split()
if (etag in tags) or ('*' in tags):
self.setResponseCode(((self.method in ("HEAD", "GET"))
and NOT_MODIFIED)
or PRECONDITION_FAILED)
return CACHED
return None
def getRequestHostname(self):
"""Get the hostname that the user passed in to the request.
This will either use the Host: header (if it is available) or the
host we are listening on if the header is unavailable.
"""
return (self.getHeader('host') or
socket.gethostbyaddr(self.getHost()[1])[0]
).split(':')[0]
def headerReceived(self, line):
"""Do pre-processing (for content-length) and store this header away.
"""
header, data = line.split(':', 1)
header = header.lower()
data = data.strip()
if header == 'content-length':
self.length = int(data)
reqHeaders = self.requests[-1].received_headers
reqHeaders[header] = data
if len(reqHeaders) > self.maxHeaders:
self.transport.write("HTTP/1.1 400 Bad Request\r\n\r\n")
self.transport.loseConnection()
def checkPersistence(self, request, version):
"""Check if the channel should close or not."""
connection = request.getHeader('connection')
if connection:
tokens = map(str.lower, connection.split(' '))
else:
tokens = []
# HTTP 1.0 persistent connection support is currently disabled,
# since we need a way to disable pipelining. HTTP 1.0 can't do
# pipelining since we can't know in advance if we'll have a
# content-length header, if we don't have the header we need to close the
# connection. In HTTP 1.1 this is not an issue since we use chunked
# encoding if content-length is not available.
#if version == "HTTP/1.0":
# if 'keep-alive' in tokens:
# request.setHeader('connection', 'Keep-Alive')
# return 1
# else:
# return 0
if version == "HTTP/1.1":
if 'close' in tokens:
request.setHeader('connection', 'close')
return 0
else:
return 1
else:
return 0
def fix_text(text):
if text is None:
return ''
if hasattr(text, 'text'):
text = text.text
return ' '.join(text.split())