爬虫小案例—雪球网行情中心板块数据抓取

雪球网行情中心网址:https://xueqiu.com/hq

目标:市场一览板块、热股榜板块、新股预告板块、关注排行榜板块

源代码如下:

import datetimeimport requestsheaders = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',}# 实例化session对象,进行会话保持
session = requests.Session()
url = 'https://xueqiu.com'
session.get(url, headers=headers)# 时间戳转成日期的函数
def timestamp_to_date(timestamp_data):timestamp = timestamp_data# 使用datetime.fromtimestamp()方法将时间戳转换为datetime对象datetime_obj = datetime.datetime.fromtimestamp(timestamp)# 使用strftime()方法将datetime对象格式化为字符串表示的日期formatted_date = datetime_obj.strftime("%Y-%m-%d")return formatted_date# 获取四大板块的指数
def get_four_index():url = 'https://stock.xueqiu.com/v5/stock/batch/quote.json?symbol=SH000001,SZ399001,SZ399006,SH000688'res = session.get(url, headers=headers)items_lst = res.json()['data']['items']print('市 场 一 览:')print()print('板块名称\t指 数\t涨跌幅\t\t\t总市值')for item in items_lst:data_dic = item['quote']print(data_dic['name'], data_dic['current'], str(data_dic['chg']) + '(' + str(data_dic['percent']) + ')\t\t',f"{data_dic['market_capital'] / 1000000000000: >.2f}万亿")print('- ' * 50)def get_stock(url_dict):for stock_type in url_lst_dict.keys():res = session.get(url_dict[stock_type], headers=headers)res.encoding = res.apparent_encodingstock_data = res.json()print(f'热股榜——{stock_type}:\n')print('股票代码\t\t', '股票名称\t\t\t\t\t', '股票涨跌幅')for stock in stock_data['data']['items']:print(f'{stock["code"]:8}\t', f'{stock["name"]:<25}', f'{stock["percent"]:>8}')print('- ' * 30)# 定义获取新股json函数
def get_json(new_stock_url):res = session.get(new_stock_url, headers=headers)new_stock = res.json()return new_stock# 定义获取港股新股函数
def hk_new_stokc(hk_new_stock_url):new_stock = get_json(hk_new_stock_url)print('- ' * 40)print('港股')print('新股代码\t', '新股名称\t', '上市日期\t', '招股价下限\t', '招股价上限')new_stock_info = new_stock['data']['items']for new_stock_item in new_stock_info:# 上市日期时间戳list_timestamp = new_stock_item['list_date'] / 1000print(new_stock_item['symbol'], '\t', new_stock_item['name'], '\t', timestamp_to_date(list_timestamp), '\t',new_stock_item['issprice_min'], '\t', new_stock_item['issprice_max'])# 定义沪深新股抓取函数
def hs_new_stock(hs_new_stock_url):new_stock = get_json(hs_new_stock_url)print('新 股 预 告')print('- ' * 40)print('沪深')print('新股发行数量:', new_stock['data']['count'])print('新股代码\t', '新股名称\t', '申购代码\t', '预计发行量(万股)\t', '申购上限(万股)\t', '申购日期\t', '中签号公布日')new_stock_info = new_stock['data']['items']for new_stock_item in new_stock_info:# 申购日期时间戳distr_timestamp = new_stock_item['onl_distr_date'] / 1000# 公布中签日期时间戳draw_timestamp = new_stock_item['onl_lotwiner_stpub_date'] / 1000print(new_stock_item['symbol'], new_stock_item['name'], '\t', new_stock_item['onl_subcode'], '\t',new_stock_item['actissqty'], '\t\t\t', new_stock_item['onl_sub_maxqty'], '\t\t',timestamp_to_date(distr_timestamp), '\t', timestamp_to_date(draw_timestamp))# 定义美股新股抓取函数
def un_new_stock(un_new_stock_url):new_stock = get_json(un_new_stock_url)print('- ' * 50)print('美股')print('新股发行数量:', new_stock['data']['count'])print('新股代码\t', '新股名称\t', '上市日期\t', '\t股本', '\t招股价下限\t', '招股价上限')new_stock_info = new_stock['data']['items']for new_stock_item in new_stock_info:# 上市日期时间戳list_timestamp = new_stock_item['list_date'] / 1000if new_stock_item['shares']:new_shares = '\t' + str(new_stock_item['shares'] / 10000) + '万'else:new_shares = '\t\t-\t'if new_stock_item['issprice_min']:new_min = '\t' + str(new_stock_item['issprice_min']) + '\t'if new_stock_item['issprice_max']:new_max = '\t' + str(new_stock_item['issprice_max']) + '\t'else:new_max = '\t-'new_min = '\t-\t'print(new_stock_item['symbol'], '\t', new_stock_item['name'][:8], '\t', timestamp_to_date(list_timestamp),new_shares, new_min, new_max)# 本周排行榜, 本周新增股票,最热门股票
def get_new_add_stock(new_add_url):new_add_stock = get_json(new_add_url)print('- ' * 50)print('关注排行榜——本周新增')new_list = new_add_stock['data']['list']print('股票名称\t\t股 价\t\t关 注')for add_stock in new_list:print(f"{add_stock['name']}\t\t{add_stock['current']}\t\t{int(add_stock['follow7d']):<}")# 本周排行榜,最热门股票
def get_hot_stock(new_hot_url):hot_stock = get_json(new_hot_url)print('- ' * 50)print('关注排行榜——最热门')hot_lst = hot_stock['data']['list']print('股票名称\t\t股 价\t\t关 注')for hot_stock in hot_lst:print(f"{hot_stock['name']}\t\t{hot_stock['current']}\t\t{int(hot_stock['follow']):<}")if __name__ == '__main__':# 四大板块信息get_four_index()# 热门股票url_lst_dict = {'沪深': 'https://stock.xueqiu.com/v5/stock/hot_stock/list.json?page=1&size=9&_type=12&type=12','港股': 'https://stock.xueqiu.com/v5/stock/hot_stock/list.json?page=1&size=9&_type=13&type=13','美股': 'https://stock.xueqiu.com/v5/stock/hot_stock/list.json?page=1&size=9&_type=11&type=11'}get_stock(url_lst_dict)print()# 沪深新股网址hs_new_stock_url = 'https://stock.xueqiu.com/v5/stock/preipo/cn/query.json?type=subscribe&order_by=onl_subbeg_date&order=asc&source=new_subscribe&page=1&size=10'hs_new_stock(hs_new_stock_url)# 港股新股网址hk_new_stock_url = 'https://stock.xueqiu.com/v5/stock/preipo/hk/query.json?order=desc&order_by=list_date&type=unlisted&page=1&size=10'hk_new_stokc(hk_new_stock_url)# 美股新股网址un_new_stock_url = 'https://stock.xueqiu.com/v5/stock/preipo/us/list.json?order=desc&order_by=list_date&type=unlisted&page=1&size=10'un_new_stock(un_new_stock_url)# 关注排行榜,本周新增new_add_stock_url = 'https://stock.xueqiu.com/v5/stock/screener/screen.json?page=1&only_count=0&size=10&category=CN&order_by=follow7d&order=desc'get_new_add_stock(new_add_stock_url)# 最热门股票hot_stock_url = 'https://stock.xueqiu.com/v5/stock/screener/screen.json?page=1&only_count=0&size=10&category=CN&order_by=follow&order=desc'

本案例利用requests请求模块的知识,请大家登录https://zglg.work,或者通过下方【阅读原文】跳转体验在线编程的乐趣。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/640280.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一周时间,开发了一款封面图生成工具

介绍 这是一款封面图的制作工具&#xff0c;根据简单的配置即可生成一张好看的封面图&#xff0c;目前已有七款主题可以选择。做这个工具的初衷来自平时写文章&#xff0c;都为封面图发愁&#xff0c;去图片 网站上搜索很难找到满意的&#xff0c;而且当你要的图如果要搭配上文…

【JavaEE进阶】 关于⽇志框架(SLF4J)

文章目录 &#x1f333;SLF4j&#x1f332;⻔⾯模式(外观模式)&#x1f6a9;⻔⾯模式的定义&#x1f6a9;⻔⾯模式的优点 &#x1f343;关于SLF4J框架&#x1f6a9;不引⼊⽇志⻔⾯&#x1f6a9;引⼊⽇志⻔⾯ ⭕总结 &#x1f333;SLF4j SLF4J不同于其他⽇志框架,它不是⼀个真正…

构建高效外卖系统:技术实践与代码示例

外卖系统在现代社会中扮演着重要的角色&#xff0c;为用户提供了便捷的用餐解决方案。在这篇文章中&#xff0c;我们将探讨构建高效外卖系统的技术实践&#xff0c;同时提供一些基础的代码示例&#xff0c;帮助开发者更好地理解和应用这些技术。 1. 技术栈选择 构建外卖系统…

自动化运维神器—ansible详解

一、ansible简介 1.ansible定义 ansible是目前最受运维欢迎的自动化运维工具&#xff0c;基于Python开发&#xff0c;集合了众多运维工具&#xff08;SaltStack puppet、chef、func、fabric&#xff09;的优点&#xff0c;实现了批量系统配置、批量程序部署、批量运行命令等功…

[Java并发基础]多进程编程

Java并发基础&#xff1a;多进程编程 在Java编程中&#xff0c;多进程编程是一种并发编程的方法&#xff0c;它允许我们同时执行多个独立的进程。每个进程都有自己的内存空间和执行环境&#xff0c;它们可以独立运行&#xff0c;相互之间不会干扰。 文章目录 Java并发基础&…

【算法详解】力扣415.字符串相加

一、题目描述 力扣链接&#xff1a;力扣415.字符串相加 给定两个字符串形式的非负整数 num1 和num2 &#xff0c;计算它们的和并同样以字符串形式返回。 你不能使用任何內建的用于处理大整数的库&#xff08;比如 BigInteger&#xff09;&#xff0c; 也不能直接将输入的字符串…

BP蓝图映射到C++笔记1

教程链接&#xff1a;示例1&#xff1a;CompleteQuest - 将蓝图转换为C (epicgames.com) 1.常用的引用需要记住&#xff0c;如图所示。 2.蓝图中可以调用C函数&#xff0c;也可以实现C函数 BlueprintImplementableEvent:C只创建&#xff0c;不实现&#xff0c;在蓝图中实现 B…

C++提高编程---模板---类模板

目录 一、类模板 1.模板 2.类模板的作用 3.语法 4.声明 二、类模板和函数模板的区别 三、类模板中成员函数的创建时机 四、类模板对象做函数参数 五、类模板与继承 六、类模板成员函数类外实现 七、类模板分文件编写 八、类模板与友元 九、类模板案例 一、类模板 …

373. 查找和最小的 K 对数字

373. 查找和最小的 K 对数字 题目链接&#xff1a;373. 查找和最小的 K 对数字 代码如下&#xff1a; //参考leetcode官方题解&#xff1a;https://leetcode.cn/problems/find-k-pairs-with-smallest-sums/solutions/1208350/cha-zhao-he-zui-xiao-de-kdui-shu-zi-by-l-z526 …

软件测试的需求人才越来越多,为什么大家还是不太愿意走软件测试的道路?

&#x1f525; 交流讨论&#xff1a;欢迎加入我们一起学习&#xff01; &#x1f525; 资源分享&#xff1a;耗时200小时精选的「软件测试」资料包 &#x1f525; 教程推荐&#xff1a;火遍全网的《软件测试》教程 &#x1f4e2;欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1…

【ARMv8M Cortex-M33 系列 5 -- RT-Thread .rti_fn 段简介】

文章目录 .rti_fn 段的作用 .rti_fn 段的作用 在 RT-Thread 实时操作系统中&#xff0c;.rti_fn 代码段通常用于存放初始化函数。RT-Thread 的启动过程中包括了一系列的初始化步骤&#xff0c;这些初始化函数被分配到特定的代码段中&#xff0c;以便操作系统在启动时按照预定的…

【动态规划】【C++算法】801. 使序列递增的最小交换次数

作者推荐 【动态规划】【广度优先搜索】【状态压缩】847 访问所有节点的最短路径 本文涉及知识点 动态规划汇总 数组 LeetCode801使序列递增的最小交换次数 我们有两个长度相等且不为空的整型数组 nums1 和 nums2 。在一次操作中&#xff0c;我们可以交换 nums1[i] 和 num…

路飞项目--03

二次封装Response模块 # drf提供的Response&#xff0c;前端想接收到的格式 {code:xx,msg:xx} 后端返回&#xff0c;前端收到&#xff1a; APIResponse(tokneasdfa.asdfas.asdf)---->{code:100,msg:成功,token:asdfa.asdfas.asdf} APIResponse(code101,msg用户不存在) ---…

学习笔记-李沐动手学深度学习(一)(01-07,概述、数据操作、tensor操作、数学基础、自动求导)

个人随笔 第三列是 jupyter记事本 官方github上啥都有&#xff08;代码、jupyter记事本、胶片&#xff09; https://github.com/d2l-ai 多体会 【梯度指向的是值变化最大的方向】 符号 维度 &#xff08;弹幕说&#xff09;2&#xff0c;3&#xff0c;4越后面维度越低 4…

Java 面向对象案例 02 (黑马)

代码&#xff1a; public class foodTest {public static void main(String[] args) {//1、构建一个数组food[] arr new food[3];//2、创建三个商品对象food f1 new food("apple","123",3.2,500);food f2 new food("pear","456",4…

临时工说:AI 人工智能化对于DBA 的工作的影响

这开头还是介绍一下群&#xff0c;如果感兴趣PolarDB ,MongoDB ,MySQL ,PostgreSQL ,Redis, Oceanbase, Sql Server等有问题&#xff0c;有需求都可以加群群内&#xff0c;可以解决你的问题。加群请联系 liuaustin3 &#xff0c;&#xff08;共1900人左右 1 2 3 4 5&#xf…

ChatGPT:关于 OpenAI 的 GPT-4工具,你需要知道的一切

ChatGPT&#xff1a;关于 OpenAI 的 GPT-4工具&#xff0c;你需要知道的一切 什么是GPT-3、GPT-4 和 ChatGPT&#xff1f;ChatGPT 可以做什么&#xff1f;ChatGPT-4 可以做什么&#xff1f;ChatGPT 的费用是多少&#xff1f;GPT-4 与 GPT-3.5 有何不同&#xff1f;ChatGPT 如何…

开源堡垒机JumpServer本地安装并配置公网访问地址

文章目录 前言1. 安装Jump server2. 本地访问jump server3. 安装 cpolar内网穿透软件4. 配置Jump server公网访问地址5. 公网远程访问Jump server6. 固定Jump server公网地址 前言 JumpServer 是广受欢迎的开源堡垒机&#xff0c;是符合 4A 规范的专业运维安全审计系统。JumpS…

HBuilderx发布苹果的包需要注意什么

在HBuilderX中发布苹果的包&#xff0c;需要注意以下几点&#xff1a; 开发者账号注册&#xff1a;在发布应用到App Store之前&#xff0c;需要先注册一个苹果开发者账号。注册过程较为繁琐&#xff0c;需要提供个人信息并支付年费。应用标识和证书&#xff1a;在发布iOS应用之…

ONLYOFFICE服务器无法连接,请联系管理员问题解决

1、现象 部署好了nextcloud和onlyoffice后&#xff0c;新建文本文档报错ONLYOFFICE服务器无法连接&#xff0c;请联系管理员。 用快捷键“F12”进入控制台&#xff0c;点开错误提示栏&#xff0c;找到有“api.js“文件&#xff0c;“https://ONLYOFFICED的地址/web-apps/apps/…