|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +网易财经股票历史数据下载 |
| 5 | +@author: 闲欢 |
| 6 | +""" |
| 7 | + |
| 8 | +import requests |
| 9 | +import json |
| 10 | +from bs4 import BeautifulSoup |
| 11 | +import traceback |
| 12 | +import pymysql |
| 13 | + |
| 14 | +class StockHisInfo: |
| 15 | + |
| 16 | + def __init__(self): |
| 17 | + self.conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='east_money', charset='utf8') |
| 18 | + self.cur = self.conn.cursor() |
| 19 | + |
| 20 | + |
| 21 | + def get_data(self, code, year, season): |
| 22 | + url = 'http://quotes.money.163.com/trade/lsjysj_%s.html?year=%s&season=%d' % (code, year, season) |
| 23 | + ua_header = {"Connection": "keep-alive", |
| 24 | + "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36", |
| 25 | + "Host": "quotes.money.163.com", |
| 26 | + "Cookie": "vjuids=2453fea.1759e01b4ef.0.c69c7922974aa; _ntes_nnid=99f0981d725ac03af6da5eec0508354e,1604673713410; _ntes_nuid=99f0981d725ac03af6da5eec0508354e; _ntes_stock_recent_=1300033; _ntes_stock_recent_=1300033; _ntes_stock_recent_=1300033; ne_analysis_trace_id=1604846790608; s_n_f_l_n3=20f075946bacfe111604846790626; _antanalysis_s_id=1604933714338; vjlast=1604673713.1605015317.11; pgr_n_f_l_n3=20f075946bacfe1116050154486829637; vinfo_n_f_l_n3=20f075946bacfe11.1.0.1604846790623.0.1605015456187", |
| 27 | + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", |
| 28 | + "Accept-Encoding": "gzip, deflate", |
| 29 | + "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,fr;q=0.7", |
| 30 | + "Cache-Control": "no-cache", |
| 31 | + "Pragma": "no-cache", |
| 32 | + "Referer": "http://quotes.money.163.com/trade/lsjysj_%s.html" % code, |
| 33 | + "Upgrade-Insecure-Requests": "1", |
| 34 | + } |
| 35 | + response = requests.get(url, headers=ua_header, verify=False) |
| 36 | + content = response.content.decode("utf-8") |
| 37 | + |
| 38 | + return content |
| 39 | + |
| 40 | + def parse_data(self, code, name, content): |
| 41 | + soup = BeautifulSoup(content, 'html.parser') |
| 42 | + table = soup.find("table", class_="table_bg001 border_box limit_sale").prettify() |
| 43 | + tb_soup = BeautifulSoup(table, 'html.parser') |
| 44 | + tr_list = tb_soup.find_all('tr') |
| 45 | + stock_list = [] |
| 46 | + if len(tr_list): |
| 47 | + del tr_list[0] |
| 48 | + for tr in tr_list: |
| 49 | + items = tr.text.split('\n\n') |
| 50 | + if len(items): |
| 51 | + del items[0] |
| 52 | + stock = {} |
| 53 | + stock['day'] = items[0].replace('\n ', '').replace(' ', '') |
| 54 | + stock['code'] = code |
| 55 | + stock['name'] = name |
| 56 | + stock['open_price'] = self.trans_float(items[1].replace('\n ', '').replace(' ', '')) |
| 57 | + stock['top_price'] = self.trans_float(items[2].replace('\n ', '').replace(' ', '')) |
| 58 | + stock['low_price'] = self.trans_float(items[3].replace('\n ', '').replace(' ', '')) |
| 59 | + stock['close_price'] = self.trans_float(items[4].replace('\n ', '').replace(' ', '')) |
| 60 | + # stock['last_price'] = self.trans_float(items[7]) |
| 61 | + stock['add_point'] = self.trans_float(items[5].replace('\n ', '').replace(' ', '')) |
| 62 | + stock['add_percent'] = self.trans_float(items[6].replace('\n ', '').replace(' ', '')) |
| 63 | + stock['volumn'] = self.trans_float(items[7].replace('\n ', '').replace(' ', '').replace(',', '')) |
| 64 | + stock['turnover'] = self.trans_float(items[8].replace('\n ', '').replace(' ', '').replace(',', '')) |
| 65 | + stock['amplitude'] = self.trans_float(items[9].replace('\n ', '').replace(' ', '')) |
| 66 | + stock['exchange_rate'] = self.trans_float(items[10].replace('\n \n', '').replace(' ', '')) |
| 67 | + # stock['market_value'] = self.trans_float(items[13]) |
| 68 | + # stock['flow_market_value'] = self.trans_float(items[14]) |
| 69 | + |
| 70 | + stock_list.append(stock) |
| 71 | + |
| 72 | + return stock_list |
| 73 | + |
| 74 | + def query_lcode(self, day): |
| 75 | + query_sql = "select code,name from stock_info where day='%s'" % day |
| 76 | + |
| 77 | + try: |
| 78 | + lcode = self.cur.execute_sql(query_sql) |
| 79 | + return lcode |
| 80 | + except Exception: |
| 81 | + #输出异常信息 |
| 82 | + traceback.print_exc() |
| 83 | + |
| 84 | + def insertdb(self, data_list): |
| 85 | + attrs = ['day', 'code', 'name', 'open_price', 'top_price', 'low_price', 'close_price', 'add_point', |
| 86 | + 'add_percent', 'volumn', 'turnover', 'amplitude', 'exchange_rate'] |
| 87 | + insert_tuple = [] |
| 88 | + for obj in data_list: |
| 89 | + insert_tuple.append((obj['day'], obj['code'], obj['name'], obj['open_price'], obj['top_price'], obj['low_price'], obj['close_price'], obj['add_point'], obj['add_percent'], obj['volumn'], obj['turnover'], obj['amplitude'], obj['exchange_rate'])) |
| 90 | + values_sql = ['%s' for v in attrs] |
| 91 | + attrs_sql = '('+','.join(attrs)+')' |
| 92 | + values_sql = ' values('+','.join(values_sql)+')' |
| 93 | + sql = 'insert into %s' % 'stock_info' |
| 94 | + sql = sql + attrs_sql + values_sql |
| 95 | + try: |
| 96 | + print(sql) |
| 97 | + for i in range(0, len(insert_tuple), 20000): |
| 98 | + self.cur.executemany(sql, tuple(insert_tuple[i:i+20000])) |
| 99 | + self.conn.commit() |
| 100 | + except pymysql.Error as e: |
| 101 | + self.conn.rollback() |
| 102 | + error = 'insertMany executemany failed! ERROR (%s): %s' % (e.args[0], e.args[1]) |
| 103 | + print(error) |
| 104 | + |
| 105 | + |
| 106 | + @staticmethod |
| 107 | + def trans_float(s): |
| 108 | + try: |
| 109 | + return float(s) |
| 110 | + except Exception: |
| 111 | + return 0.00 |
| 112 | + |
| 113 | + def deal(self, day, year, season): |
| 114 | + lcode = self.query_lcode(day) |
| 115 | + for code,name in lcode: |
| 116 | + content = self.get_data(code, year, season) |
| 117 | + stock_list = self.parse_data(code, name, content) |
| 118 | + if len(stock_list): |
| 119 | + self.insertdb(stock_list) |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + sdi = StockHisInfo() |
| 124 | + sdi.deal('2020-11-11', '2020', 3) |
0 commit comments