Python学习笔记之抓取某只基金历史净值数据实战案例
作者:学习笔记666 时间:2021-08-14 20:28:13
本文实例讲述了Python抓取某只基金历史净值数据。分享给大家供大家参考,具体如下:
http://fund.eastmoney.com/f10/jjjz_519961.html
1、接下来,我们需要动手把这些html抓取下来(这部分知识我们之前已经学过,现在不妨重温)
# coding: utf-8
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from bs4 import BeautifulSoup
from threading import Thread,Lock
import os
import csv
# 下面是利用 selenium 抓取html页面的代码
# 初始化函数
def initSpider():
driver = webdriver.PhantomJS(executable_path=r"你phantomjs可执行文件的绝对路径")
driver.get("http://fund.eastmoney.com/f10/jjjz_519961.html") # 要抓取的网页地址
# 找到"下一页"按钮,就可以得到它前面的一个label,就是总页数
getPage_text = driver.find_element_by_id("pagebar").find_element_by_xpath(
"div[@class='pagebtns']/label[text()='下一页']/preceding-sibling::label[1]").get_attribute("innerHTML")
# 得到总共有多少页
total_page = int("".join(filter(str.isdigit, getPage_text)))
# 返回
return (driver,total_page)
# 获取html内容
def getData(myrange,driver,lock):
for x in myrange:
# 锁住
lock.acquire()
tonum = driver.find_element_by_id("pagebar").find_element_by_xpath(
"div[@class='pagebtns']/input[@class='pnum']") # 得到 页码文本框
jumpbtn = driver.find_element_by_id("pagebar").find_element_by_xpath(
"div[@class='pagebtns']/input[@class='pgo']") # 跳转到按钮
tonum.clear() # 第x页 输入框
tonum.send_keys(str(x)) # 去第x页
jumpbtn.click() # 点击按钮
# 抓取
WebDriverWait(driver, 20).until(lambda driver: driver.find_element_by_id("pagebar").find_element_by_xpath("div[@class='pagebtns']/label[@value={0} and @class='cur']".format(x)) != None)
# 保存到项目中
with open("../htmls/details/{0}.txt".format(x), 'wb') as f:
f.write(driver.find_element_by_id("jztable").get_attribute("innerHTML").encode('utf-8'))
f.close()
# 解锁
lock.release()
# 开始抓取函数
def beginSpider():
# 初始化爬虫
(driver, total_page) = initSpider()
# 创建锁
lock = Lock()
r = range(1, int(total_page)+1)
step = 10
range_list = [r[x:x + step] for x in range(0, len(r), step)] #把页码分段
thread_list = []
for r in range_list:
t = Thread(target=getData, args=(r,driver,lock))
thread_list.append(t)
t.start()
for t in thread_list:
t.join() # 这一步是需要的,等待线程全部执行完成
print("抓取完成")
# #################上面代码就完成了 抓取远程网站html内容并保存到项目中的 过程
需要说明一下这3个函数:
initSpider
函数,初始化了selenium的webdriver
对象,并且先获取到我们需要抓取页面的总页码数。getData
函数,有3个参数,myrange我们还是要分段抓取,之前我们学过多进程抓取,这里我们是多线程抓取;lock参数用来锁住线程的,防止线程冲突;driver就是我们在initSpider
函数中初始化的webdriver
对象。
在getData
函数中,我们循环myrange,把抓取到的html内容保存到了项目目录中。beginSpider
函数,我们在此函数中给总页码分段,并且创建线程,调用getData
。
所以最后执行:
beginSpider()
就开始抓取 http://fund.eastmoney.com/f10/jjjz_519961.html 这个基金的”历史净值明细”,共有31个页面。
2、根据已经学过的python和mysql交互的知识,我们也可以再把这些数据 写入到数据表中。
这里就不再赘述,给出基金详细表结构:
CREATE TABLE `fund_detail` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`fcode` varchar(10) NOT NULL DEFAULT '' COMMENT '基金代码',
`fdate` datetime DEFAULT NULL COMMENT '基金日期',
`NAV` decimal(10,4) DEFAULT NULL COMMENT '单位净值',
`ACCNAV` decimal(10,4) DEFAULT NULL COMMENT '累计净值',
`DGR` varchar(20) DEFAULT NULL COMMENT '日增长率',
`pstate` varchar(20) DEFAULT NULL COMMENT '申购状态',
`rstate` varchar(20) DEFAULT NULL COMMENT '赎回状态',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='基金详细数据表';
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/github_26672553/article/details/78662563
标签:Python,抓取,数据
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL优化之数据表的处理
2008-12-22 14:45:00
利用Python生成随机验证码详解
2021-10-04 19:55:50
![](https://img.aspxhome.com/file/2023/1/89041_0s.jpg)
按钮表状态还是表动作?
2009-03-23 18:21:00
![](https://img.aspxhome.com/file/UploadPic/20093/23/1-300x272-42s.jpg)
【MongoDB for Java】Java操作MongoDB数据库
2023-07-14 01:10:43
![](https://img.aspxhome.com/file/2023/4/59714_0s.jpg)
python使用Matplotlib绘制分段函数
2022-04-17 16:07:17
![](https://img.aspxhome.com/file/2023/2/76012_0s.jpg)
如何利用SQL Server数据库快照形成报表
2009-01-15 11:55:00
让Python代码更快运行的5种方法
2023-08-25 08:35:15
Python中eval()函数的详细使用教程
2023-11-22 15:51:39
![](https://img.aspxhome.com/file/2023/8/78578_0s.png)
asp解决防止表单重复提交的方法
2007-10-19 18:40:00
PHP中使用Memache作为进程锁的操作类分享
2023-11-22 18:10:02
python实现整数序列求和
2023-12-14 06:53:10
![](https://img.aspxhome.com/file/2023/9/93619_0s.png)
黄相如:如何做好用户体验
2008-06-04 17:34:00
教你四种方法用来查看mysql版本
2009-06-28 11:13:00
Python 树表查找(二叉排序树、平衡二叉树)
2021-01-27 03:06:20
![](https://img.aspxhome.com/file/2023/8/64318_0s.png)
Python教程按照字典的键或值进行排序方法解析
2023-01-12 09:10:26
《JavaScript语言精粹》译者序及样章试读
2009-04-03 11:38:00
python中list循环语句用法实例
2022-09-12 15:08:05
那些被我遗忘掉的XHTML标签们
2008-06-07 14:27:00
树莓派4B+opencv4+python 打开摄像头的实现方法
2021-05-04 12:09:37
![](https://img.aspxhome.com/file/2023/3/91993_0s.jpg)
python GUI编程(Tkinter) 创建子窗口及在窗口上用图片绘图实例
2021-12-21 19:37:29
![](https://img.aspxhome.com/file/2023/6/77646_0s.jpg)