Python采集股票数据信息【含带完整源码】

嗨嗨,各位晚上好 ~

Python采集股票数据信息【含带完整源码】_第1张图片

今天给大家分享用python采集股票数据信息

开发环境 & 第三方模块:

  • 解释器版本: python 3.8
  • 代码编辑器: pycharm 2021.2
  • requests: pip install requests 爬虫
  • pyecharts: pip install pyecharts 数据分析
  • pandas: pip install pandas 数据分析
如果安装python第三方模块:
  1. win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests)
    回车
  2. 在pycharm中点击Terminal(终端) 输入安装命令
如果有不会的可以点击蓝字,找管理老师领取哦~【想要源码和解答问题也可以找哦】

代码实现:

发送请求

获取数据

解析数据

保存数据

代码

因审核机制原因,我把网址里的一些东西删掉了,朋友们可以自己添加一下哈,很容易的

也可以直接找管理姐姐要哦 点击这里

导入模块

import requests     # 发送请求 第三方模块
import csv          # 内置模块 不需要额外安装

加伪装

# 伪装
headers = {
    # 用户基本信息
    'Cookie': 's=bq119wflib; xq_a_token=fb81acf5170aad073886d6f4710230100565c245; xq_r_token=7b20eba2d7f8fb7ff92d34226863aebb43cc1bfc; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTY2MDUyMDE1MywiY3RtIjoxNjU4NDg4MDQ2MDE0LCJjaWQiOiJkOWQwbjRBWnVwIn0.Oy2cyGJLCPutPUQW07bInBWrDMu3nSMBLiDzpZeLJP51GGa7jcLA2Cbv9AGmK6dDQxrHXWVdeGSRU207sBCQ8LyBHzh86zX2jOM9O8a6vmiWWWE1vVrika0-IPC061IqnDWoNX6UYglkOTsROyMBl1ToFMSwnbLtSYjFHDlCrS4LaWccbUoTN1WI8m0gQMJ7uQRuzcrwT3Qz9OomgP2WuEstcW4cWlkUBQy7015lGkUCdAfHSpKw2iKpOIBbgyilylPjsFavfhdhlzWaMaL5v8RVo1pgk-EJf_l3dH8lD0aQSL2ivrJ7KI63LTQLgccVXs449M9wPc_PRGLlwQm94Q; u=701658488083353; cookiesu=701658488083353; device_id=90ec0683f24e4d1dd28a383d87fa03c5; Hm_lvt_1db88642e346389874251b5a1eded6e3=1658488084; __utma=1.1460934076.1658488091.1658488091.1658488091.1; __utmc=1; __utmz=1.1658488091.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); Hm_lpvt_1db88642e346389874251b5a1eded6e3=1658488099; acw_tc=2760779916584917111362559e5892bf3e77c79f159bb96b31b388f4669de3',
    # 域名
    'Host': '.com',
    # 防盗链
    'Referer': 'https://.com/hq',
    # 浏览器基本信息
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
}
f = open('股票.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.writer(f)
csv_writer.writerow(['symbol', 'name', 'current', 'chg', 'percent', 'current_year_percent', 'volume', 'amount', 'turnover_rate', 'pe_ttm', 'dividend_yield', 'market_capital'])
for page in range(1, 162):

发送请求

    # 变量 专门用来接收 网页链接
    url = f'https://.com/service/v5/stock/screener/quote/list?page={page}&size=30&order=desc&orderby=percent&order_by=percent&market=CN&type=sh_sz&_=1658491936321'
    response = requests.get(url=url, headers=headers)
    # : 无法定位到资源 没有权限
    # : 请求成功

获取数据

    json_data = response.json()

解析数据

    data_list = json_data['data']['list']
    # 循环 节省代码量的
    for i in range(0, len(data_list)):
        symbol = data_list[i]['symbol']
        name = data_list[i]['name']
        current = data_list[i]['current']
        chg = data_list[i]['chg']
        percent = data_list[i]['percent']
        current_year_percent = data_list[i]['current_year_percent']
        volume = data_list[i]['volume']
        amount = data_list[i]['amount']
        turnover_rate = data_list[i]['turnover_rate']
        pe_ttm = data_list[i]['pe_ttm']
        dividend_yield = data_list[i]['dividend_yield']
        market_capital = data_list[i]['market_capital']
        print(symbol, name, current, chg, percent, current_year_percent, volume, amount, turnover_rate, pe_ttm, dividend_yield, market_capital)

保存数据

        # mode='a': 追加写入
        # encoding='utf-8': 编码方式
        # newline='': 数据空行
        f = open('雪球股票.csv', mode='a', encoding='utf-8', newline='')
        csv_writer = csv.writer(f)
        csv_writer.writerow([symbol, name, current, chg, percent, current_year_percent, volume, amount, turnover_rate, pe_ttm, dividend_yield, market_capital])

好啦,今天的分享到这里就结束了 ~

对文章有问题的,或者有其他关于python的问题,可以在评论区留言或者私信我哦

觉得我分享的文章不错的话,可以关注一下我,或者给文章点赞(/≧▽≦)/

Python采集股票数据信息【含带完整源码】_第2张图片

你可能感兴趣的:(python,开发语言)