2、为下面的程序的每一行标上注释; import requests import re import pandas as pd import time import datetime url = 'http://datacenter-web.eastmoney.com/api/data/v1/get?' name_list = [] code_list = [] trader_date_list = [] close_list = [] change_rate_list = [] buy_num_list = [] result_list = [] result_df = pd.DataFrame() for page in range(1, 4): params = ( ('callback', 'jQuery112305930880286224138_1632364981303'), ('sortColumns', 'NET_BUY_AMT,TRADE_DATE,SECURITY_CODE'), ('sortTypes', '-1,-1,1'), ('pageSize', '50'), ('pageNumber', str(page)), ('reportName', 'RPT_ORGANIZATION_TRADE_DETAILS'), ('columns', 'ALL'), ('source', 'WEB'), ('clientl', 'WE'), ('filter', "(TRADE_DATE>='2021-09-17')") ) response = requests.get(url, params=params) text = response.text print(text) # re准则查找数据 name = re.findall('"SECURITY_NAME_ABBR":"(.*?)"', text) # 名称 code = re.findall('"SECURITY_CODE":"(.*?)"', text) # 股票代码 trader_date = re.findall('"TRADE_DATE":"(.*?)"', text) # 交易日期 close = re.findall('"CLOSE_PRICE":(.*?)\,', text) # 收盘价 change_rate = re.findall('"CHANGE_RATE":(.*?)\,', text) # 涨幅 buy_num = re.findall('"BUY_TIMES":(.*?)\,', text) # 买入机构数量 # 将对应的列表里的数据全部加起来 name_list = name_list + name code_list = code_list + code trader_date_list = trader_date_list + trader_date close_list = close_list + close change_rate_list = change_rate_list + change_rate buy_num_list = buy_num_list + buy_num time.sleep(2) # 将所有列表合并成二维数组 result_list = [trader_date_list, code_list, name_list, close_list, change_rate_list, buy_num_list] # 将数据转为DataFrame格式 result_df = pd.DataFrame(result_list).T.rename( columns={0: '交易日期', 1: '股票代码', 2: '股票名称', 3: '收盘价', 4: '涨幅', 5: '买入机构'}) result_df['交易日期'] =pd.to_datetime(result_df['交易日期']) # 时间只取年月日 result_df = result_df.sort_values(by='交易日期', ascending=True) print(result_df)
时间: 2024-03-18 16:43:36 浏览: 80
python调试文件时发生import requests报错.doc
5星 · 资源好评率100%
# 导入需要的库
import requests
import re
import pandas as pd
import time
import datetime
# 定义请求的url
url = 'http://datacenter-web.eastmoney.com/api/data/v1/get?'
# 定义空列表用于存储数据
name_list = []
code_list = []
trader_date_list = []
close_list = []
change_rate_list = []
buy_num_list = []
result_list = []
result_df = pd.DataFrame()
# 循环请求数据
for page in range(1, 4):
params = (
('callback', 'jQuery112305930880286224138_1632364981303'),
('sortColumns', 'NET_BUY_AMT,TRADE_DATE,SECURITY_CODE'),
('sortTypes', '-1,-1,1'),
('pageSize', '50'),
('pageNumber', str(page)),
('reportName', 'RPT_ORGANIZATION_TRADE_DETAILS'),
('columns', 'ALL'),
('source', 'WEB'),
('clientl', 'WE'),
('filter', "(TRADE_DATE>='2021-09-17')")
)
# 发送请求,并获取响应数据
response = requests.get(url, params=params)
text = response.text
print(text)
# 使用正则表达式查找数据
name = re.findall('"SECURITY_NAME_ABBR":"(.*?)"', text) # 名称
code = re.findall('"SECURITY_CODE":"(.*?)"', text) # 股票代码
trader_date = re.findall('"TRADE_DATE":"(.*?)"', text) # 交易日期
close = re.findall('"CLOSE_PRICE":(.*?)\,', text) # 收盘价
change_rate = re.findall('"CHANGE_RATE":(.*?)\,', text) # 涨幅
buy_num = re.findall('"BUY_TIMES":(.*?)\,', text) # 买入机构数量
# 将对应的列表里的数据全部加起来
name_list = name_list + name
code_list = code_list + code
trader_date_list = trader_date_list + trader_date
close_list = close_list + close
change_rate_list = change_rate_list + change_rate
buy_num_list = buy_num_list + buy_num
# 暂停2秒钟
time.sleep(2)
# 将所有列表合并成二维数组
result_list = [trader_date_list, code_list, name_list, close_list, change_rate_list, buy_num_list]
# 将数据转为DataFrame格式
result_df = pd.DataFrame(result_list).T.rename(columns={0: '交易日期', 1: '股票代码', 2: '股票名称', 3: '收盘价', 4: '涨幅', 5: '买入机构'})
# 将时间列转换为日期格式,并只取年月日
result_df['交易日期'] = pd.to_datetime(result_df['交易日期'])
# 根据交易日期排序
result_df = result_df.sort_values(by='交易日期', ascending=True)
# 打印结果
print(result_df)
阅读全文