python抓取股票_Python股票信息抓取~

#-*-coding:utf-8 -*-

importurllibimportreimportjsonimporturllib2from lxml importetreeimportrequestsimporttimefrom Queue importQueueimportmatplotlib.pyplot as plt

URL= 'http://quote.fx678.com/exchange/WH'nation_que=Queue()

nation=Queue()

high=Queue()

start_time=Queue()

Chart=[]def __cmp__(self, other):if self.time

elif self.time ==other.time:return0else:return 1

classShare:def __init__(self,time,score):

self.time=time

self.score=scoredef __getitem__(self, key):

self.item.get(key)def __cmp__(self, other):returncmp(self.time, other.time)def __str__(self):return self.time + " " +str(self.score)def download(url, headers, num_try=2):while num_try >0:

num_try-= 1

try:

content= requests.get(url, headers=headers)returncontent.textexcepturllib2.URLError as e:print 'Download error', e.reasonreturnNonedefget_type_url():

headers={'User_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36','Referer': 'http://quote.fx678.com/exchange/WH','Cookie': 'io=-voMclEjiizK9nWKALqB; UM_distinctid=15f5938ddc72db-089cf9ba58d9e5-31657c00-fa000-15f5938ddc8b24; Hm_lvt_d25bd1db5bca2537d34deae7edca67d3=1509030420; Hm_lpvt_d25bd1db5bca2537d34deae7edca67d3=1509031023','Accept-Language': 'zh-CN,zh;q=0.8','Accept-Encoding': 'gzip, deflate','Accept': '*/*'}

content=download(URL,headers)

html=etree.HTML(content)

result= html.xpath('//a[@class="mar_name"]/@href')for each inresult:printeach

st= each.split('/')

nation_que.put(st[len(st)-1])

get_precent()defget_precent():while notnation_que.empty():

ss=nation_que.get(False)printss

url= 'http://api.q.fx678.com/history.php?symbol=' + ss +'&limit=288&resolution=5&codeType=8100&st=0.43342772855649625'

printurl

headers= {'Accept':'application/json, text/javascript, */*; q=0.01','Accept-Encoding':'gzip, deflate','Accept-Language':'zh-CN,zh;q=0.8','Connection':'keep-alive','Host':'api.q.fx678.com','Origin':'http://quote.fx678.com','Referer':'http://quote.fx678.com/symbol/USD','User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}

num_try= 2

while num_try >0:

num_try-= 1

try:

content= requests.get(url, headers=headers)

html=json.loads(content.text)

st= html['h']

T_time= html['t']if len(st) > 0 and len(T_time) >0:

nation.put(ss)

high.put(st)

start_time.put(T_time)break

excepturllib2.URLError as e:print 'Download error', e.reason

nation_que.task_done()

draw_pict()defsub_sort(array,array1,low,high):

key=array[low]

key1=array1[low]while low =key:

high-= 1

while low < high and array[high]

array[low]=array[high]

array1[low]=array1[high]

low+= 1array[high]=array[low]

array1[high]=array1[low]

array[low]=key

array1[low]=key1returnlowdefquick_sort(array,array1,low,high):if low

key_index=sub_sort(array,array1,low,high)

quick_sort(array,array1,low,key_index)

quick_sort(array,array1,key_index+1,high)defdraw_pict():whileTrue:

Nation=nation.get(False)

High=high.get(False)

Time=start_time.get(False)

T_time=[]

High_Rate=[]

num=0for each,high1 inzip(Time,High):

st=time.localtime(float(each))if st.tm_min ==0:

T_time.append(st.tm_hour)

High_Rate.append(high1)

quick_sort(T_time,High_Rate,0,len(T_time)-1)

plt.plot(T_time,High_Rate, marker='*')

plt.show()

plt.title(Nation)

nation.task_done()

high.task_done()break

if __name__ == '__main__':

get_type_url()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值