Skip to content

Instantly share code, notes, and snippets.

@1111yoyo1
Created July 15, 2012 09:50
Show Gist options
  • Select an option

  • Save 1111yoyo1/3116122 to your computer and use it in GitHub Desktop.

Select an option

Save 1111yoyo1/3116122 to your computer and use it in GitHub Desktop.
stock information downloaded from Tencent Stock
# -*- coding: utf-8 -*-
import urllib2,re,os,sys,time
space=10
def output(obj_list=['Name','Number','Price','Change','Rate'],nums_of_lines=1,blackspace=10):
for i in range(nums_of_lines):
for objs in obj_list:
print objs.center(blackspace),
print
def sz_or_sh(num):
if len(num.split(','))==2:
return num.split(',')[1]
else:
url='http://smartbox.gtimg.cn/s3/?q=%s&t=all' % num
content=urllib2.urlopen(url).readlines()
for line in content:
pattern=re.compile(r'v_hint="(\w{2})')
match=pattern.match(line)
if match:
return match.group(1)
def price(origin_num):
stock_type=sz_or_sh(origin_num)
if len(origin_num.split(','))==2:
num=origin_num.split(',')[0]
else:
num=origin_num
url='http://qt.gtimg.cn/q=s_%s%s' % (stock_type,num)
content=urllib2.urlopen(url).readlines()
for line in content:
if stock_type =="sh":
pattern_string=r'v_s_%s%s="1~(.{2,8})~(\d{6})~(\d{1,3}.\d{1,2})~([-+]?\d{1,3}.\d{1,2})~([-+]?\d{1,2}.\d{1,2})'% (stock_type, num)
elif stock_type=="sz" :
pattern_string=r'v_s_%s%s="51~(.{2,8})~(\d{6})~(\d{1,3}.\d{1,2})~([-+]?\d{1,3}.\d{1,2})~([-+]?\d{1,2}.\d{1,2})'% (stock_type, num)
pattern=re.compile(pattern_string)
match=pattern.match(line)
if match:
output(match.groups(),1,10)
def read(loc):
stocklist=[]
file=open(loc)
for line in file:
stocklist.append(line.rstrip())
file.close()
return stocklist
def main():
output()
file_path='stocks.txt'
stocklist=read(file_path)
for stocks in stocklist:
price(stocks)
if __name__ == "__main__":
for i in range(100):
main()
time.sleep(5)
output([' '],1,10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment