最近在看mongodb,然后会用了一些最简单的mongodb的操作,然后想着结合股票信息的数据的抓取,然后将数据存储在mongodb中,对于mongo和数据库的最大的区别是,mongo不需要建表,直接进行存储,然后在选择数据表的时候在进行插入数据的时候要将str格式的字符串转换成json的格式进行插入,这个我在插入数据的时候调试了十多分钟,一直以为是自己字符串的原因,然后看了看插入数据的格式和百度,然后才发现这点。然后我是插入在本机的test.Share表中的,然后其他的注重点就没有什么了~代码写的很丑,冗余也很大,还是会继续更新~并且程序是但进程进行的数据抓取~嗯~ 很蠢~

  1. #-*-coding:utf-8 -*-
  2. import urllib
  3. import re
  4. import json
  5. import urllib2
  6. from lxml import etree
  7. import requests
  8. import time
  9. from Queue import Queue
  10. from pymongo import MongoClient
  11. import matplotlib.pyplot as plt
  12. URL = 'http://quote.fx678.com/exchange/WH'
  13. nation_que = Queue()
  14. client = MongoClient('localhost',27017)
  15. db = client.test
  16. Share = db.Share
  17.  
  18. def sub_sort(array,array1,low,high):
  19. key = array[low]
  20. key1 = array1[low]
  21. while low < high:
  22. while low < high and array[high] >= key:
  23. high -= 1
  24. while low < high and array[high] < key:
  25. array[low] = array[high]
  26. array1[low] = array1[high]
  27. low += 1
  28. array[high] = array[low]
  29. array1[high] = array1[low]
  30. array[low] = key
  31. array1[low] = key1
  32. return low
  33.  
  34. def quick_sort(array,array1,low,high):
  35. if low < high:
  36. key_index = sub_sort(array,array1,low,high)
  37. quick_sort(array,array1,low,key_index)
  38. quick_sort(array,array1,key_index+1,high)
  39.  
  40. def download(url, headers, num_try=2):
  41. while num_try >0:
  42. num_try -= 1
  43. try:
  44. content = requests.get(url, headers=headers)
  45. return content.text
  46.  
  47. except urllib2.URLError as e:
  48. print 'Download error', e.reason
  49.  
  50. return None
  51.  
  52. current_quto = Queue()
  53. open_quto = Queue()
  54. high_quto = Queue()
  55. low_quto = Queue()
  56. close_quto = Queue()
  57. update_time = Queue()
  58. def get_type_url():
  59. headers = {
  60. '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',
  61. 'Referer': 'http://quote.fx678.com/exchange/WH',
  62. 'Cookie': 'io=-voMclEjiizK9nWKALqB; UM_distinctid=15f5938ddc72db-089cf9ba58d9e5-31657c00-fa000-15f5938ddc8b24; Hm_lvt_d25bd1db5bca2537d34deae7edca67d3=1509030420; Hm_lpvt_d25bd1db5bca2537d34deae7edca67d3=1509031023',
  63. 'Accept-Language': 'zh-CN,zh;q=0.8',
  64. 'Accept-Encoding': 'gzip, deflate',
  65. 'Accept': '*/*'
  66. }
  67. content = download(URL,headers)
  68. html = etree.HTML(content)
  69. result = html.xpath('//a[@class="mar_name"]/@href')
  70. result1 = html.xpath('//td/text()')
  71. num = 0
  72. for each in result1:
  73.  
  74. if num%6 == 0:
  75. current_quto.put(each)
  76. num += 1
  77. elif num%6 == 1:
  78. open_quto.put(each)
  79. num += 1
  80. elif num%6 == 2:
  81. high_quto.put(each)
  82. num += 1
  83. elif num%6 == 3:
  84. low_quto.put(each)
  85. num += 1
  86. elif num %6 == 4:
  87. close_quto.put(each)
  88. num +=1
  89. elif num %6 == 5:
  90. update_time.put(each)
  91. num +=1
  92. #while not
  93. for each in result:
  94. st = each.split('/')
  95. nation_que.put(st[len(st)-1])
  96.  
  97. get_precent()
  98.  
  99. def get_precent():
  100.  
  101. while not nation_que.empty():
  102. if not update_time.empty():
  103. time_update = update_time.get(False)
  104. update_time.task_done()
  105. if not current_quto.empty():
  106. new_rates = current_quto.get(False)
  107. current_quto.task_done()
  108. if not open_quto.empty():
  109. opening = open_quto.get(False)
  110. open_quto.task_done()
  111. if not high_quto.empty():
  112. high = high_quto.get(False)
  113. high_quto.task_done()
  114. if not low_quto.empty():
  115. low = low_quto.get(False)
  116. low_quto.task_done()
  117. if not close_quto.empty():
  118. closing = close_quto.get(False)
  119. close_quto.task_done()
  120.  
  121. ss = nation_que.get(False)
  122. print ss
  123. print low
  124. print high
  125. print time_update
  126. print new_rates
  127. print opening
  128.  
  129. url = 'http://api.q.fx678.com/history.php?symbol=' + ss +'&limit=288&resolution=5&codeType=8100&st=0.8274405615006541'
  130. print url
  131. headers = {'Accept':'application/json, text/javascript, */*; q=0.01',
  132. 'Accept-Encoding':'gzip, deflate',
  133. 'Accept-Language':'zh-CN,zh;q=0.8',
  134. 'Connection':'keep-alive',
  135. 'Host':'api.q.fx678.com',
  136. 'Origin':'http://quote.fx678.com',
  137. 'Referer':'http://quote.fx678.com/symbol/USD',
  138. 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
  139. }
  140. num_try = 2
  141. while num_try >0:
  142. num_try -= 1
  143. try:
  144. content = requests.get(url, headers=headers)
  145. html = json.loads(content.text)
  146. st = html['h']
  147. T_time = html['t']
  148. if len(st) > 0 and len(T_time) > 0:
  149. draw_pict(ss,T_time,st,time_update,new_rates,opening,high,low,closing)
  150. break
  151. except urllib2.URLError as e:
  152. print 'Download error', e.reason
  153. nation_que.task_done()
  154. List = []
  155. def draw_pict(name,T_time1,high_rate,time_update,new_rate,opening,high,low,closing):
  156.  
  157. High = T_time1
  158. Time = high_rate
  159. High_Rate = []
  160. T_time = []
  161. mmap = "{\"Type\":\"%s\",\"Current_quto\":\"%s\",\"Opening_quto\":\"%s\",\"High_quto\":\"%s\",\"low_quto\":\"%s\",\"Closing_quto\":\"%s\",\"Update_Time\":\"%s\",\"Real_TIme_infor\":{" % ( name, new_rate, opening, high, low, closing, time_update)
  162. print mmap
  163. flag = 0
  164. for each,high1 in zip(T_time1,high_rate):
  165. if flag == 1:
  166. mmap += ","
  167. else:
  168. flag = 1
  169. mm = "\"%s\":\"%s\""%(each,high1)
  170.  
  171. st = time.localtime(float(each))
  172. mmap += mm
  173. if st.tm_min == 0:
  174. T_time.append(st.tm_hour)
  175. High_Rate.append(high1)
  176. else:
  177. pass
  178. mmap += "}}"
  179. mmap1 = json.loads(mmap)
  180. print mmap1
  181. Share.insert(mmap1)
  182. if len(T_time) == len(High_Rate):
  183. quick_sort(T_time,High_Rate,0,len(High_Rate)-1)
  184. List.append(High_Rate)
  185.  
  186. def draw_picture():
  187. colu = len(List)
  188.  
  189. num = 1
  190. for each in List:
  191. plt.subplot(colu/2 + 1,2,num)
  192. num+=1
  193.  
  194. list = each
  195. T_time = []
  196. for i in range(len(list)):
  197. T_time.append(i)
  198. print len(list)
  199. print len(T_time)
  200. plt.plot(T_time, list, marker='*')
  201.  
  202. plt.show()
  203. plt.title('Share Message')
  204.  
  205. if __name__ == '__main__':
  206. get_type_url()
  207. draw_picture()

Python股票信息抓取(三)的更多相关文章

  1. Python股票信息抓取~

    本来想把股票的涨跌抓取出来,用汇通网的股票为例,就找了国际外汇为例. 页面里有xhr请求,并且每个xhr的url请求的 http://api.q.fx678.com/history.php?symbo ...

  2. Python股票信息抓取(二)

    在一的基础上,想着把所有的折线图放在一个图中,然后图的结果如图所示: 不是略丑,是很丑~ 依然的单进程,只是将图标结果放在了一张图里 代码如下: #-*-coding:utf-8 -*- import ...

  3. python 爬虫抓取心得

    quanwei9958 转自 python 爬虫抓取心得分享 urllib.quote('要编码的字符串') 如果你要在url请求里面放入中文,对相应的中文进行编码的话,可以用: urllib.quo ...

  4. Python 爬虫: 抓取花瓣网图片

    接触Python也好长时间了,一直没什么机会使用,没有机会那就自己创造机会!呐,就先从爬虫开始吧,抓点美女图片下来. 废话不多说了,讲讲我是怎么做的. 1. 分析网站 想要下载图片,只要知道图片的地址 ...

  5. Python爬虫----抓取豆瓣电影Top250

    有了上次利用python爬虫抓取糗事百科的经验,这次自己动手写了个爬虫抓取豆瓣电影Top250的简要信息. 1.观察url 首先观察一下网址的结构 http://movie.douban.com/to ...

  6. Python爬虫抓取东方财富网股票数据并实现MySQL数据库存储

    Python爬虫可以说是好玩又好用了.现想利用Python爬取网页股票数据保存到本地csv数据文件中,同时想把股票数据保存到MySQL数据库中.需求有了,剩下的就是实现了. 在开始之前,保证已经安装好 ...

  7. python Web抓取(一)[没写完]

    需要的模块: python web抓取通过: webbrowser:是python自带的,打开浏览器获取指定页面 requests:从因特网上下载文件和网页 Beautiful Soup:解析HTML ...

  8. python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言)

    python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言) 感觉要总结总结了,希望这次能写个系列文章分享分享心得,和大神们交流交流,提升提升. 因为 ...

  9. python数据抓取分析(python + mongodb)

    分享点干货!!! Python数据抓取分析 编程模块:requests,lxml,pymongo,time,BeautifulSoup 首先获取所有产品的分类网址: def step(): try: ...

随机推荐

  1. ZJOI2018 D1

    归途的车上满是悲伤的气息 虽然早就预言到D1会滚粗,但一切都结束之后还是特别难过. 延时15min 50min T1 30pts 1.5h T2 10pts 2.5h T1 50pts 4.5h T3 ...

  2. 【树状数组】【P3902】 递增

    传送门 Description 给你一个长度为\(n\)的整数数列,要求修改最少的数字使得数列单调递增 Input 第一行为\(n\) 第二行\(n\)个数代表数列 Output 输出一行代表答案 H ...

  3. dTree 动态生成树

    http://luohua.iteye.com/blog/451453 dTree 主页:http://destroydrop.com/javascripts/tree/ dTree是个很方便在页面生 ...

  4. Python高级语法总结

    1.Python面向对象 创建类 使用class语句来创建一个新类,class之后为类的名称并以冒号结尾,如下实例: class ClassName: '类的帮助信息' #类文档字符串 class_s ...

  5. Codeforces Round #419 (Div. 2) A B C 暴力 区间更新技巧 +前缀和 模拟

    A. Karen and Morning time limit per test 2 seconds memory limit per test 512 megabytes input standar ...

  6. 电商网站中价格的精确计算(使用BigDecimal进行精确运算(实现加减乘除运算))

    使用BigDecimal的String的构造器.商业计算中,使用bigdecimal的String构造器,一定要用. 重要的事情说三遍: 商业计算中,使用bigdecimal的String构造器! 商 ...

  7. python学习(十一)测试和调试

    最近学习了python的错误处理和几种测试方法 1 try except 可以通过try except方式捕捉异常 try: print('try...') r = 10/0 print('resul ...

  8. centos6.5 mqtt安装

    CentOs 6.5 MQTT 安装部署 所需安装包: libwebsockets-v1.6-stable.tar.gz,mosquitto-1.4.8.tar.gz 1.安装依赖 # yum -y ...

  9. NOIP 2015 提高组 Day1

    期望得分:100+100+100=300 实际得分:100+100+45=245 T3 相似的代码 复制过去 没有改全,痛失55分 http://www.cogs.pro/cogs/page/page ...

  10. select表单元素详解及下拉列表模拟实现

    原文地址:→看过来 写在前面 select 是HTML表单元素中很常用的一个,其中很重要的几个属性常被忽略,但这几个属性却能帮助我们完成很多的功能,当然,select下拉列表默认样式很不友好,所以更多 ...