58彩票网计划群

Python爬取股票信息,并可视化数据的示例

发布时间: 2020-09-26 16:33:23 来源: 互联网 栏目: python 点击:

这篇文章主要介绍了Python爬取股票信息,并可视化数据的示例,帮助大家更好的理解和使用python爬虫,感兴趣的朋友可以了解下

前言

58彩票网计划群截止2019年年底我国股票投资者数量为15975.24万户, 如此多的股民热衷于炒股,首先抛开炒股技术不说, 那么多股票数据是不是非常难找, 找到之后是不是看着密密麻麻的数据是不是头都大了?

今天带大家爬取雪球平台的股票数据, 并且实现数据可视化

先看下效果图

Python爬取股票信息,并可视化数据的示例

基本环境配置

  • python 3.6
  • pycharm
  • requests
  • csv
  • time

目标地址

58彩票网计划群http://xueqiu.com/hq

Python爬取股票信息,并可视化数据的示例

爬虫代码

请求网页

import requests
url = 'http://xueqiu.com/service/v5/stock/screener/quote/list'
response = requests.get(url=url, params=params, headers=headers, cookies=cookies)
html_data = response.json()

解析数据

data_list = html_data['data']['list']
for i in data_list:
 dit = {}
 dit['股票代码'] = i['symbol']
 dit['股票名字'] = i['name']
 dit['当前价'] = i['current']
 dit['涨跌额'] = i['chg']
 dit['涨跌幅/%'] = i['percent']
 dit['年初至今/%'] = i['current_year_percent']
 dit['成交量'] = i['volume']
 dit['成交额'] = i['amount']
 dit['换手率/%'] = i['turnover_rate']
 dit['市盈率TTM'] = i['pe_ttm']
 dit['股息率/%'] = i['dividend_yield']
 dit['市值'] = i['market_capital']
 print(dit)

保存数据

import csv
f = open('股票数据.csv', mode='a', encoding='utf-8-sig', newline='')
csv_writer = csv.DictWriter(f, fieldnames=['股票代码', '股票名字', '当前价', '涨跌额', '涨跌幅/%', '年初至今/%', '成交量', '成交额', '换手率/%', '市盈率TTM', '股息率/%', '市值'])
csv_writer.writeheader()
csv_writer.writerow(dit)
f.close()

完整代码

import pprint

import requests
import time
import csv

f = open('股票数据.csv', mode='a', encoding='utf-8-sig', newline='')
csv_writer = csv.DictWriter(f, fieldnames=['股票代码', '股票名称', '当前价', '涨跌额', '涨跌幅/%', '年初至今/%', '成交量', '成交额', '换手率/%', '市盈率TTM', '股息率/%', '市值'])
csv_writer.writeheader()

for page in range(1, 53):
 time.sleep(1)
 url = 'http://xueqiu.com/service/v5/stock/screener/quote/list'
 date = round(time.time()*1000)
 params = {
  'page': '{}'.format(page),
  'size': '30',
  'order': 'desc',
  'order_by': 'amount',
  'exchange': 'CN',
  'market': 'CN',
  'type': 'sha',
  '_': '{}'.format(date),
 }
 cookies = {
  'Cookie': 'acw_tc=2760824216007592794858354eb971860e97492387fac450a734dbb6e89afb; xq_a_token=636e3a77b735ce64db9da253b75cbf49b2518316; xqat=636e3a77b735ce64db9da253b75cbf49b2518316; xq_r_token=91c25a6a9038fa2532dd45b2dd9b573a35e28cfd; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTYwMjY0MzAyMCwiY3RtIjoxNjAwNzU5MjY3OTEwLCJjaWQiOiJkOWQwbjRBWnVwIn0.bengzIpmr0io9f44NJdHuc_6g9EIjtrSlMgnqwKSWVzI4syI_yIH1F-GJfK4bTelWzDirufjWMW9DfDMyMkI75TpJqiwIq8PRsa1bQ7IuCXLbN71ebsiTOGfA5OsWSPQOdVXQA0goqC4yvXLOk5KgC5FQIzZut0N4uaRDLsq7vhmcb8CBw504tCZnbIJTfGGIFIfw7TkwuUCXGY6Q-0mlOG8U4EUTcOCuxN87Ej_OIKnXN8cTSVh7XW6SFxOgU6p3yUXDgvS04rt-nFewpNNqfbGAKk965N-HJ9Mq8E52BRJ3rt_ndYP8yCaeQ6xSsz5P2mNlKwNFe9EQeltim_mDg; u=501600759279498; device_id=24700f9f1986800ab4fcc880530dd0ed; Hm_lvt_1db88642e346389874251b5a1eded6e3=1600759286; _ga=GA1.2.2049292015.1600759388; _gid=GA1.2.391362708.1600759388; s=du11eogy79; __utma=1.2049292015.1600759388.1600759397.1600759397.1; __utmc=1; __utmz=1.1600759397.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmt=1; __utmb=1.3.10.1600759397; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1600759448'
 }
 headers = {
  'Host': 'xueqiu.com',
  'Pragma': 'no-cache',
  'Referer': 'http://xueqiu.com/hq',
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
 }

 response = requests.get(url=url, params=params, headers=headers, cookies=cookies)
 html_data = response.json()
 data_list = html_data['data']['list']
 for i in data_list:
  dit = {}
  dit['股票代码'] = i['symbol']
  dit['股票名称'] = i['name']
  dit['当前价'] = i['current']
  dit['涨跌额'] = i['chg']
  dit['涨跌幅/%'] = i['percent']
  dit['年初至今/%'] = i['current_year_percent']
  dit['成交量'] = i['volume']
  dit['成交额'] = i['amount']
  dit['换手率/%'] = i['turnover_rate']
  dit['市盈率TTM'] = i['pe_ttm']
  dit['股息率/%'] = i['dividend_yield']
  dit['市值'] = i['market_capital']
  csv_writer.writerow(dit)

  print(dit)

f.close()

Python爬取股票信息,并可视化数据的示例

Python爬取股票信息,并可视化数据的示例

数据分析代码

c = (
 Bar()
  .add_xaxis(list(df2['股票名称'].values))
  .add_yaxis("股票成交量情况", list(df2['成交量'].values))
  .set_global_opts(
  title_opts=opts.TitleOpts(title="成交量图表 - Volume chart"),
  datazoom_opts=opts.DataZoomOpts(),
 )
  .render("data.html")
)

Python爬取股票信息,并可视化数据的示例

以上就是Python爬取股票信息,并可视化数据的示例的详细内容,更多关于Python爬取股票信息的资料请关注我们其它58彩票网计划群相关的文章!

本文标题: Python爬取股票信息,并可视化数据的示例
本文地址: http://cooldogg.com/jiaoben/python/349889.html

如果认为本文对您有所帮助请赞助本站

支付宝扫一扫赞助微信扫一扫赞助

  • 支付宝扫一扫赞助
  • 微信扫一扫赞助
  • 支付宝先领红包再赞助
    声明:凡注明"本站原创"的所有文字图片等资料,版权均属编程客栈所有,欢迎转载,但务请注明出处。
    如何利用python发送邮件python实现sm2和sm4国密(国家商用密码)算法的示例
    Top 送彩金棋牌网站 菠萝彩票计划群 彩之家彩票计划群 大无限彩票计划群 注册送38彩金可提现 百益彩票计划群