用js做的网站,怎么知道自己的网站被k,wordpress 小工具 代码,南通单位网站建设目前两个常用的股票信息CPI#xff1a; 腾讯行情CTPAPI接口源码 新浪行情CTPAPI 使用requests模块爬取股票信息#xff0c;这里以查询股票市值为例。
一、根据股票名称查询股票代码
在python文件夹下设置两个表格GPLIST.xlsx#xff0c;其中是A股全部代码和股票名称#…目前两个常用的股票信息CPI 腾讯行情CTPAPI接口源码 新浪行情CTPAPI 使用requests模块爬取股票信息这里以查询股票市值为例。
一、根据股票名称查询股票代码
在python文件夹下设置两个表格GPLIST.xlsx其中是A股全部代码和股票名称query_stock_names.xlsx是要查询的股票名称
stockfinder-3.py
import openpyxl def create_stock_dict(file_path): stock_dict {} wb openpyxl.load_workbook(filenamefile_path) ws wb.active for row in ws.iter_rows(values_onlyTrue): stock_dict[row[1]] row[0] return stock_dict def get_stock_code(stock_name, stock_dict): return stock_dict.get(stock_name, Not Found) def main(): stock_dict create_stock_dict(GPLIST.xlsx) wb openpyxl.load_workbook(filenamequery_stock_names-1.xlsx) ws wb.active for row in ws.iter_rows(min_row1, min_col1, max_col1, values_onlyTrue): stock_name row[0] stock_code get_stock_code(stock_name, stock_dict) ws.cell(rowws.max_row1, column2, valuestock_code) wb.save(query_stock_names-1.xlsx) if __name__ __main__: main()二、股票市值查询
1.单个股票市值查询 lianghua-2.py
import requests
from dateutil import parser
from datetime import datetime,time
from time import sleep def get_tick(stock_code): if stock_code[0] in [5,6]: prefix sh elif stock_code[0] in [0,3]: prefix sz else: raise Exception(prefix) page requests.get(http://qt.gtimg.cn/qprefixstock_code) stock_info page.text stock_info stock_info.split(~) open_ float(stock_info[5])# string high float(stock_info[33]) low float(stock_info[34]) close float(stock_info[3]) shizhi float(stock_info[45]) trade_datetime parser.parse(stock_info[30]) #将string格式解析成日期格式 return shizhi if __name__ __main__: stock_c 601088 print(get_tick(stock_c))2.批量股票市值查询 stockshizhiquery-2.py
import requests
import openpyxl def get_tick(stock_code): if stock_code[0] in [5, 6]: prefix sh elif stock_code[0] in [0, 3]: prefix sz else: raise Exception(Invalid stock code prefix) page requests.get(http://qt.gtimg.cn/q prefix stock_code) stock_info page.text stock_info stock_info.split(~) shizhi float(stock_info[45]) if stock_info[45] else 0.0 # 如果市值为空则设为0 return shizhi # 打开 Excel 文件
workbook openpyxl.load_workbook(query_stock_names-1.xlsx) # 获取第一个工作表
sheet workbook.active # 获取每支股票的市值并将其追加到第三列
for row in sheet.iter_rows(min_row1, values_onlyTrue): # 假设股票代码在第一列从第二行开始读取数据 stock_code str(row[0]) shizhi get_tick(stock_code) # sheet.append([shizhi]) sheet.cell(rowsheet.max_row 1, column3, valueshizhi) workbook.save(query_stock_names-1_updated.xlsx)