# 朋友股票亏惨了，我一怒用Python爬取了证券最新数据

IT生活 作者：专注的阿熊 时间：2021-10-26 17:22:44 0 删除 编辑

import requests

import execjs

import js2py

import math

import time

import pandas as pd

code_list = []

def MCODE():

jscode = '''

function missjson(input) {

var keyStr = "ABCDEFGHIJKLMNOP" + "QRSTUVWXYZabcdef" + "ghijklmnopqrstuv"   + "wxyz0123456789+/" + "=";

var output = "";

var chr1, chr2, chr3 = "";

var enc1, enc2, enc3, enc4 = "";

var i = 0;

do {

chr1 = input.charCodeAt(i++);

chr2 = input.charCodeAt(i++);

chr3 = input.charCodeAt(i++);

enc1 = chr1 >> 2;

enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);

enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);

enc4 = chr3 & 63;

if (isNaN(chr2)) {

enc3 = enc4 = 64;

} else if (isNaN(chr3)) {

enc4 = 64;

}

output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2)

+ keyStr.charAt(enc3) + keyStr.charAt(enc4);

chr1 = chr2 = chr3 = "";

enc1 = enc2 = enc3 = enc4 = "";

} while (i < input.length);

return output;

}

'''

time1 = js2py.eval_js('Math.floor(new Date().getTime()/1000)')

# py 方式

a = math.floor(time.time() / 1000)

mcode = execjs.compile(jscode).call('missjson', '{a}'.format(a=time1))

return mcode

def PageRquest(datetime, mcode):

# 接口可以换

url = '

data = {

'tdate': datetime, # 获取数据时间

'scode': '399001'  # 股票代码 以及交易所简称

}

'mcode': str(mcode),

'Referer': 外汇跟单gendan5.com'

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36'

}

code = response['records']

for i in code:

code_list.append(i)

def main(date):

mcode = MCODE()

PageRquest(date, mcode)

if __name__ == '__main__':

# main()

# 数据分析 pandas 自动化办公的

datetime = pd.period_range('2021/5/1', '2021/10/20', freq='B')

for date in datetime:

main(date)

df = pd.DataFrame(code_list)

df.to_excel('code.xlsx')

• 博文量
248
• 访问量
137512