import numpy as np
import pandas as pd
import tushare as ts
import matplotlib.pyplot as plt
import requests
import re
import os
import matplotlib
print(matplotlib.matplotlib_fname())
pro = ts.pro_api('cad11aab38da19bdb978f9e4a3311b9b1665e791e5d06c61eec78331')
data = pro.query('stock_basic', exchange='', list_status='L', fields='ts_code, symbol,name,area,industry,list_date')
allnumber = data.iloc[:, 0].size
print('上市公司總數量為', allnumber, 3)
mylist = np.random.randint(1, allnumber, 3)
mycompany = []
for item in mylist:
mycompany.append({'name': data.iloc[item,]['name'], 'symbol': data.iloc[item,]['symbol']})
print(mycompany)
os.chdir('E:\')
for i in range(len(mycompany)):
print(mycompany[i]['name'] + mycompany[i]['symbol'])
url = 'http://quotes.money.163.com/service/zcfzb' + mycompany[i]['symbol'] + '.html'
print(url)
# urlrequest.urlretrieve(url, a['name'][i] + a['symbol'][i] + '.csv')
r = requests.get(url)
filename = 'a' + mycompany[i]['name'] + mycompany[i]['symbol'] + '.csv'
filename = filename.replace('*', '')
with open(filename, 'w', encoding='ANSI',) as f:
f.write(r.text)
print(mycompany[i]['name'] + mycompany[i]['symbol'] + '.csv 文件已生成完畢')
# print(text)
filename = ''
for item in mycompany:
# os.chdir('E:\')
filename = 'a' + item['name'] + item['symbol'] + '.csv'
filename = filename.replace('*', '')
print(filename)
# f = open(filename, encoding='ANSI', 'r')
mydata = pd.read_csv(filename, encoding='ANSI')
# f.close()
print(item['name'] + "資產負債表中的資產為:",end='')
Traceback (most recent call last):
File "C:/Users/zhoulei/PycharmProjects/pythonProject1/test2.py", line 46, in
mydata = pd.read_csv(filename, encoding='ANSI')
File "C:\Users\zhoulei\PycharmProjects\pythonProject1\venv\lib\site-packages\pandas\util_decorators.py", line 311, in wrapper
return func(*args, **kwargs)
File "C:\Users\zhoulei\PycharmProjects\pythonProject1\venv\lib\site-packages\pandas\io\parsers\readers.py", line 680, in read_csv
return _read(filepath_or_buffer, kwds)
File "C:\Users\zhoulei\PycharmProjects\pythonProject1\venv\lib\site-packages\pandas\io\parsers\readers.py", line 581, in _read
return parser.read(nrows)
File "C:\Users\zhoulei\PycharmProjects\pythonProject1\venv\lib\site-packages\pandas\io\parsers\readers.py", line 1254, in read
index, columns, col_dict = self._engine.read(nrows)
File "C:\Users\zhoulei\PycharmProjects\pythonProject1\venv\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 225, in read
chunks = self._reader.read_low_memory(nrows)
File "pandas_libs\parsers.pyx", line 805, in pandas._libs.parsers.TextReader.read_low_memory
File "pandas_libs\parsers.pyx", line 861, in pandas._libs.parsers.TextReader._read_rows
File "pandas_libs\parsers.pyx", line 847, in pandas._libs.parsers.TextReader._tokenize_rows
File "pandas_libs\parsers.pyx", line 1960, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 39, saw 2
幫忙運行出來