waditu / tushare

TuShare is a utility for crawling historical data of China stocks
BSD 3-Clause "New" or "Revised" License
12.87k stars 4.29k forks source link

Bad request #1009

Closed StepfenShawn closed 5 years ago

StepfenShawn commented 5 years ago

I ran the code with Python 3.7

# -*- coding:utf-8 -*-
import tushare
import pandas as pd
import datetime
import os

def StockPriceInraday(ticker,folder):
    #Step 1: Get the data online
    indraday = tushare.get_hist_data(ticker,ktype = '5')

    #Step 2: If the file exists,append
    file = folder+"/"+ticket+".csv"
    if os.path.exists(file):
        history = pandas.read_csv(file,index_col = 0)
        indraday.append(history)
    #Step 3: Inverse based on index
    indraday.sort_index(inplace = True)
    indraday.index.name = "timestamp"
    #Step 4: Save.
    indraday.to_csv(file)
    print("indraday for ["+ticker+"] got.")

#Step 5:Get the tickers online
tickersRawData = tushare.get_stock_basics()
tickers = tickersRawData.index.tolist()

#Step 5:Get the stock price (indraday) for all
for i,ticker in enumerate(tickers):
    try:
        print("Indraday",i,"/",len(tickers))
        StockPriceInraday(tickers,folder = "../data/Indraday-CN")
    except:
        pass
    if i>3:
        break
print("Indraday for all stock got")

result:

Indraday 0 / 3611
[WinError 10053] 你的主机中的软件中止了一个已建立的连接。
HTTP Error 400: Bad request
[WinError 10054] 远程主机强迫关闭了一个现有的连接。
Indraday 1 / 3611
HTTP Error 400: Bad request
HTTP Error 400: Bad request
HTTP Error 400: Bad request
Indraday 2 / 3611
HTTP Error 400: Bad request
HTTP Error 400: Bad request
[WinError 10053] 你的主机中的软件中止了一个已建立的连接。
Indraday 3 / 3611
[WinError 10054] 远程主机强迫关闭了一个现有的连接。
HTTP Error 400: Bad request
HTTP Error 400: Bad request
Indraday 4 / 3611
HTTP Error 400: Bad request
HTTP Error 400: Bad request
HTTP Error 400: Bad request
Indraday for all stock got
[Finished in 11.5s]

How to solve this problem?Please help me.Thank you.

jimmysoa commented 5 years ago

请用Pro版行情,https://tushare.pro/document/2?doc_id=27

AidenYang-Github commented 2 years ago

请用Pro版行情,https://tushare.pro/document/2?doc_id=27

目前使用的就是pro版的(python3.7),但是跑了两次都复现了这个问题“10054, '远程主机强迫关闭了一个现有的连接”。 一次获取了2750支股票数据(每支股票采了4天的daily数据)之后复现出这个错。一次获取了4300支股票数据之后出现这个错。(目前股票总数差不多4953) 请问出现这个错会不会是因为频繁调用接口导致服务器不稳定造成的吗(目前积分只有120,所以每分钟调用次数<60)?多谢!

报错信息: Traceback (most recent call last): File "D:\Program Files (x86)\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen chunked=chunked) File "D:\Program Files (x86)\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 384, in _make_request six.raise_from(e, None) File "", line 2, in raise_from File "D:\Program Files (x86)\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 380, in _make_request httplib_response = conn.getresponse() File "D:\Program Files (x86)\Anaconda3\lib\http\client.py", line 1336, in getresponse response.begin() File "D:\Program Files (x86)\Anaconda3\lib\http\client.py", line 306, in begin version, status, reason = self._read_status() File "D:\Program Files (x86)\Anaconda3\lib\http\client.py", line 267, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "D:\Program Files (x86)\Anaconda3\lib\socket.py", line 589, in readinto return self._sock.recv_into(b) ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "D:\Program Files (x86)\Anaconda3\lib\site-packages\requests\adapters.py", line 449, in send timeout=timeout File "D:\Program Files (x86)\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen _stacktrace=sys.exc_info()[2]) File "D:\Program Files (x86)\Anaconda3\lib\site-packages\urllib3\util\retry.py", line 368, in increment raise six.reraise(type(error), error, _stacktrace) File "D:\Program Files (x86)\Anaconda3\lib\site-packages\urllib3\packages\six.py", line 685, in reraise raise value.with_traceback(tb) File "D:\Program Files (x86)\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen chunked=chunked) File "D:\Program Files (x86)\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 384, in _make_request six.raise_from(e, None) File "", line 2, in raise_from File "D:\Program Files (x86)\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 380, in _make_request httplib_response = conn.getresponse() File "D:\Program Files (x86)\Anaconda3\lib\http\client.py", line 1336, in getresponse response.begin() File "D:\Program Files (x86)\Anaconda3\lib\http\client.py", line 306, in begin version, status, reason = self._read_status() File "D:\Program Files (x86)\Anaconda3\lib\http\client.py", line 267, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "D:\Program Files (x86)\Anaconda3\lib\socket.py", line 589, in readinto return self._sock.recv_into(b) urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None))

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "D:\Program Files (x86)\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevd.py", line 1741, in main() File "D:\Program Files (x86)\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevd.py", line 1735, in main globals = debugger.run(setup['file'], None, None, is_module) File "D:\Program Files (x86)\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevd.py", line 1135, in run pydev_imports.execfile(file, globals, locals) # execute the script File "D:\Program Files (x86)\JetBrains\PyCharm 2019.1.1\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "D:/A Skateboy S/Python/knowledge_archive-master/Collections/Stocks/mysql_stock.py", line 256, in next_trade_date = gap_date(last_date) File "D:/A Skateboy S/Python/knowledge_archive-master/Collections/Stocks/mysql_stock.py", line 140, in gap_date "pretrade_date"]) File "D:\Program Files (x86)\Anaconda3\lib\site-packages\tushare\pro\client.py", line 40, in query res = requests.post(self.http_url, json=req_params, timeout=self.timeout) File "D:\Program Files (x86)\Anaconda3\lib\site-packages\requests\api.py", line 116, in post return request('post', url, data=data, json=json, kwargs) File "D:\Program Files (x86)\Anaconda3\lib\site-packages\requests\api.py", line 60, in request return session.request(method=method, url=url, kwargs) File "D:\Program Files (x86)\Anaconda3\lib\site-packages\requests\sessions.py", line 533, in request resp = self.send(prep, send_kwargs) File "D:\Program Files (x86)\Anaconda3\lib\site-packages\requests\sessions.py", line 646, in send r = adapter.send(request, kwargs) File "D:\Program Files (x86)\Anaconda3\lib\site-packages\requests\adapters.py", line 498, in send raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None))

10054_1.txt 10054_2.txt 10054_1 10054_2