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

这篇具有很好参考价值的文章主要介绍了爬虫案例—雪球网行情中心板块数据抓取。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

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

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

import datetime

import requests

headers = {
    '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_encoding

        stock_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'] / 1000
        print(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'] / 1000
        print(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'] / 1000
        if 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'
    get_hot_stock(hot_stock_url)

运行结果如下:

爬虫案例—雪球网行情中心板块数据抓取,爬虫案例,编程,笔记,爬虫,python

爬虫案例—雪球网行情中心板块数据抓取,爬虫案例,编程,笔记,爬虫,python

爬虫案例—雪球网行情中心板块数据抓取,爬虫案例,编程,笔记,爬虫,python文章来源地址https://www.toymoban.com/news/detail-803267.html

到了这里,关于爬虫案例—雪球网行情中心板块数据抓取的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 爬虫案例—爬取ChinaUnix.net论坛板块标题

    ChinaUnix.net论坛网址:http://bbs.chinaunix.net 目标:抓取各个板块的标题和内容的标题 网站截图: 利用requests和xpath实现目标。源码如下: 运行结果如下:

    2024年01月19日
    浏览(51)
  • 使用分布式HTTP代理爬虫实现数据抓取与分析的案例研究

    在当今信息爆炸的时代,数据已经成为企业决策和发展的核心资源。然而,要获取大规模的数据并进行有效的分析是一项艰巨的任务。为了解决这一难题,我们进行了一项案例研究,通过使用分布式HTTP代理爬虫,实现数据抓取与分析的有效整合。本文旨在分享我们的研究成果

    2024年02月15日
    浏览(41)
  • Day:004(1) | Python爬虫:高效数据抓取的编程技术(数据解析)

    数据解析-正则表达式 在前面我们已经搞定了怎样获取页面的内容,不过还差一步,这么多杂乱的代码夹杂文字我们怎样 把它提取出来整理呢?下面就开始介绍一个十分强大的工具,正则表达式!         正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的

    2024年04月12日
    浏览(63)
  • 35 | RESTful & Socket:行情数据对接和抓取

    上一节,我们介绍了交易所的交易模式,数字货币交易所 RESTful 接口的常见概念,以及如何调用 RESTful 接口进行订单操作。众所周知,买卖操作的前提,是你需要已知市场的最新情况。这节课里,将介绍交易系统底层另一个最重要的部分,行情数据的对接和抓取。 行情数据,

    2024年02月05日
    浏览(28)
  • 爬虫案例—抓取小米商店应用

    代码如下: # 抓取第一页的内容 import requests from lxml import etree url = ‘https://app.mi.com/catTopList/0?page=1’ headers = { ‘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’ } res = requests.get(url, headers=headers) content = res.content.decode(‘

    2024年01月16日
    浏览(28)
  • 爬虫案例—表情党图片data-src抓取

    表情党网址:https://qq.yh31.com 抓取心情板块的图片data-src 由于此页面采用的是懒加载技术,为了节省网络带宽和减轻服务器压力。不浏览的图片,页面不加载,统一显示LOADING…。如下图: 按F12(谷歌浏览器)通过分析,表情图片的真正链接为data-src 通过分析,在搜索框里输入

    2024年01月16日
    浏览(29)
  • 冠达管理:有色金属迎顺周期行情 板块估值降至历史低位

    近期,A股地产链相继迸发,家居用品、房地产服务等细分板块持续反弹。沉寂多时的地产链上游——有色金属板块相同遭到资金青睐。证券时报·数据宝统计,8月28日以来,有色金属指数累计上涨近6%,跑赢同期上证指数。 从个股来看,有色金属股票8月28日以来平均上涨5.4

    2024年02月10日
    浏览(28)
  • 爬虫数据抓取怎么弄?

    爬虫数据抓取是一种自动化的数据采集技术,可以快速、高效地从互联网上获取大量的数据。本文将介绍爬虫数据抓取的基本原理、常用的爬虫框架和工具、爬虫数据抓取的注意事项以及爬虫数据抓取的应用场景。 一、爬虫数据抓取的基本原理 爬虫数据抓取的基本原理是通

    2024年02月05日
    浏览(29)
  • 逆向爬虫进阶实战:突破反爬虫机制,实现数据抓取

    随着网络技术的发展,网站为了保护自己的数据和资源,纷纷采用了各种反爬虫机制。然而,逆向爬虫技术的出现,使得我们可以突破这些限制,实现对目标网站的深入分析和抓取。本文将介绍逆向爬虫进阶实战的一些技巧和代码片段,帮助读者更好地理解和掌握这一技术。

    2024年02月04日
    浏览(38)
  • 高并发数据抓取实战:使用HTTP爬虫ip提升抓取速度

    又到每天一期学习爬虫的时间了,作为一名专业的爬虫程序员,今天要跟你们分享一个超实用的技巧,就是利用HTTP爬虫ip来提升高并发数据抓取的速度。听起来有点高大上?别担心,我会用通俗易懂的话来和你们说,让你们秒懂怎么操作的。 首先,咱们得理解一下为什么HT

    2024年02月11日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包