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

python在網頁裡抓取信息!

編輯:Python
問題遇到的現象和發生背景
問題相關代碼

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

我的解答思路和嘗試過的方法
我想要達到的結果

幫忙運行出來


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