程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python grabs information from web pages!

編輯:Python
The phenomenon and background of the problem

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())

Get stock name code

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(' The total number of listed companies is ', 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)

Start downloading files

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 File generation completed ')
# print(text)

Start reading file data

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'] + " The assets in the balance sheet are :",end='')

Operation results and error reporting contents

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

My solution ideas and tried methods
What I want to achieve

Help run it


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved