股票的历史数据爬取

爬取网易财经的个股历史数据
爬取链接:http://quotes.money.163.com/trade/lsjysj_000001.html?

先爬取股票的对应的代码

爬取股票代码的网站
https://www.banban.cn/gupiao/list_sh.html

代码

import requests,pymysql,re,datetime
import pandas as pd
from bs4 import BeautifulSoup
from sqlalchemy import create_engine
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep
#1.创建Chrome浏览器对象,这会在电脑上在打开一个浏览器窗口
headers={'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36'
}
def DownStockCode():print('开始获取上证、深证股票代码')count = 0stockcodeList=[]urlList = ['https://www.banban.cn/gupiao/list_sh.html','https://www.banban.cn/gupiao/list_sz.html']for url in urlList:res = requests.get(url=url,headers=headers)bs_res = BeautifulSoup(res.text,'html.parser')stocklist=bs_res.find('div',id='ctrlfscont').find_all('li')for stock in stocklist:stockhref=stock.find('a')['href']list_stockhref=stockhref.strip().split('/')stock_code=list_stockhref[2]stockcodeList.append(stock_code)count += 1print('当前已获取{}只股票代码'.format(count),end='\r')print('已获取所有上证、深证股票代码:{}个'.format(count))return stockcodeList

然后循环模拟下载数据的过程

全部代码(运用到selenium)

之前没有解决的问题是chrome会弹窗出来,隐藏浏览器

#隐藏浏览器
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
#隐藏浏览器

新代码

import requests,pymysql,re,datetime
import pandas as pd
from bs4 import BeautifulSoup
from sqlalchemy import create_engine
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep
from selenium.webdriver.chrome.options import Options
from PIL import Image,ImageEnhance
#1.创建Chrome浏览器对象,这会在电脑上在打开一个浏览器窗口
headers={'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36'
}
def DownStockCode():print('开始获取上证、深证股票代码')count = 0stockcodeList=[]urlList = ['https://www.banban.cn/gupiao/list_sh.html','https://www.banban.cn/gupiao/list_sz.html']for url in urlList:res = requests.get(url=url,headers=headers)bs_res = BeautifulSoup(res.text,'html.parser')stocklist=bs_res.find('div',id='ctrlfscont').find_all('li')for stock in stocklist:stockhref=stock.find('a')['href']list_stockhref=stockhref.strip().split('/')stock_code=list_stockhref[2]stockcodeList.append(stock_code)count += 1print('当前已获取{}只股票代码'.format(count),end='\r')print('已获取所有上证、深证股票代码:{}个'.format(count))return stockcodeList
all_code = DownStockCode()
#隐藏浏览器
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
#隐藏浏览器
browser = webdriver.Chrome(chrome_options=chrome_options)#声明浏览器chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
chrome_options.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36')
#chrome_options.add_argument("--headless")
#browser = webdriver.Chrome(options=chrome_options) for i in all_code:
#2.通过浏览器向服务器发送URL请求try:browser = webdriver.Chrome(options=chrome_options) browser.get("http://quotes.money.163.com/trade/lsjysj_"+i+".html?")sleep(5)#3.刷新浏览器#定位按钮并点击
#4.设置浏览器的大小browser.set_window_size(1400,800)browser.find_element_by_xpath('//*[@id="downloadData"]').click()browser.find_element_by_xpath('/html/body/div[2]/div[5]/div[2]/form/div[3]/a[1]').click()sleep(2)browser.quit()except:continue

爬取股票的历史数据(个股)相关推荐

  1. python爬取股票大单历史记录_利用bs4爬取股票的历史交易数据

    听起来,爬取股票的所有历史交易数据跟高大上,有木有? 不过写这个爬虫的时候,发现基于网易财经的股票历史数据的爬取其实挺简单,最后再写到txt文档里(暂时写txt,以后会写csv的.可以在用机器学习干一 ...

  2. 用Python爬取股票数据,绘制K线和均线并用机器学习预测股价(来自我出的书)

    最近我出了一本书,<基于股票大数据分析的Python入门实战 视频教学版>,京东链接:https://item.jd.com/69241653952.html,在其中用股票范例讲述Pyth ...

  3. 运用python爬取股票的股吧评论、新闻报道(附完整代码)

    大家好,我是带我去滑雪,每天分享一个小技巧! 当前,文本信息挖掘十分火热,文本信息挖掘作为一个新的数据挖掘领域,其目的在于把文本信息转化为人可利用的知识,而获取文本信息是文本信息挖掘工作中最重要.最首 ...

  4. 用Python爬取股票数据,绘制K线和均线并用机器学习预测股价

    最近我出了一本书,<基于股票大数据分析的Python入门实战 视频教学版>,在其中用股票范例讲述Python爬虫.数据分析和机器学习的技术,大家看了我的书,不仅能很快用比较热门的案例学好P ...

  5. 用python爬取股票数据的一点小结

    一.背景 网上对于爬取股票数据有相对完善的教程.不过大部分教程都是只能够爬取一段时间的股票数据,针对某一只股票的历史数据爬取,目前还没有看到比较好的教程.下面对近期学的东西进行一点点小结. 二.股票数 ...

  6. 爬虫:爬取股票历史交易数据

    爬虫策略制定 1. 从东方财富网中获取(http://quote.eastmoney.com/stocklist.html)股票代码 2. 从网易财经中可以直接下载csv格式文件,地址类似于http: ...

  7. 用Python爬取股票数据,绘制K线和均线并用机器学习预测股价(来自我出的书)(转载)

    最近我出了一本书,<基于股票大数据分析的Python入门实战 视频教学版>,京东链接:https://item.jd.com/69241653952.html,在其中用股票范例讲述Pyth ...

  8. MATLAB爬虫爬取股票数据

    近年来,大数据盛行,有关爬虫的教程层次不穷.那么,爬虫到底是什么呢? 什么是爬虫? 百度百科是这样定义的: 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种 ...

  9. 用 Python selenium爬取股票新闻并存入mysql数据库中带翻页功能demo可下载

    用 Python selenium爬取实时股票新闻并存入mysql数据库中 1.分析需求 2.创建表 3.分析需要爬取的网页内容 4.python里面selenium进行爬虫操作 1.添加包 2.连接 ...

最新文章

  1. ucontext实现的用户级多线程框架3(实现echo服务器)
  2. Houdini Krakatoa Render Plugin
  3. 一步一步解决 kernel 2.6 usb host driver
  4. windows系统如何添加ssh key到github
  5. springboot拦截器拦截提示_Springboot拦截器使用及其底层源码剖析
  6. 035 函数和代码复用小结
  7. 【Python进阶】带你使用Matplotlib进行可视化
  8. __stdcall、__cdecl 、CALLBACK 几种函数修饰符
  9. macOS下Apache+nginx+mysql+php多版本切换的配置
  10. VS2008水晶报表发布部署总结
  11. 孜然网址导航系统源码v1.0
  12. 实体框架(Entity Framework)简介
  13. 简单的签到代码_签到功能,用 MySQL 还是 Redis ?
  14. c++11 多线程编程(二)------ 线程类构造函数深入理解
  15. 用位运算实现内存对齐
  16. 【保姆级教学】Landsat遥感影像下载
  17. 动态规划_数字的划分
  18. How to change the implementation (detour) of an externally declared function
  19. c语言 绝对值比较大小,c语言-求绝对值最大值
  20. Unity Shader 之 环境光

热门文章

  1. 第五篇 《小强升职记》
  2. SVG滤镜、阴影、渐变
  3. java体重指数计算器程序_写出身高体重计算器的java源代码
  4. 视频教程-老司机讲前端之微信小程序开发成语消消乐游戏视频课程-微信开发
  5. 【ireport工具制作报表使用流程】
  6. 人工智能医学影像行业背景,智能医疗的发展背景
  7. 【优化预测】粒子群算法优化BP神经网络预测温度matlab源码
  8. SequoiaDB版本在线升级介绍说明
  9. Uncaught ReferenceError: web3 is not defined | 通过CDN方式导入Web3.js,提示web3未定义
  10. java.lang.NoSuchMethodError: com.sun.jna.Native.load(Ljava/lang/String;(我的情况是maven依赖冲突导致)