当前位置:网站首页>金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
2022-07-07 10:26:00 【Simon Cao】
目录
1. 谁这么会给我整活儿
什么,新浪的股票历史数据已经不直接提供了!
笔者前几日需要找一些澳洲市场的数据,奈何API没到澳洲落地生根,无奈的我只好寄希望于爬虫。当我轻车熟路的点开新浪财经上相关数据,我惊讶的发现早已空空如也。再看看A股数据,原本熟悉的交易数据也早已不复存在,取而代之的是一个叫数据中心的东西,里面也没有笔者想要的数据。

以前还很容易找到的数据现在人是物非,以前的代码也参考不了了,令人唏嘘。笔者不禁感叹难怪爬数据的越来越少了,毕竟有API这种东西谁还会去干这种费力不讨好的活。
笔者当即换了Yahoo Finance,果不其然找到了想要的数据。于是笔者开心的写了个小爬虫。
import requests
import pandas as pd
headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36 Edg/103.0.1264.37"}
url = "https://finance.yahoo.com/quote/%5EDJI/history?period1=1601510400&period2=1656460800&interval=1d&filter=history&frequency=1d&includeAdjustedClose=true"
re = requests.get(url, headers = headers)
print(pd.read_html(re.text)[0])然而还不待笔者开心,立马发现爬到的数据只有短短100行???

笔者百思不得其解,re明明请求的是三年数据的URL,为何爬下来就只剩100行了。
折腾了好半天,终于发现原来是因为雅虎数据要用鼠标滚轮往下滑才会刷出来,原始请求的网页上只有100行数据。现在排查到问题所在,那么如何解决呢?
2. Selenium模拟网页浏览器爬取
Selenium为我们提供了一个很好的解决方案, 我们传统的requests模块请求只能请求到固定网页所返回的内容,但对于需要进行点击或者像笔者碰到这个用鼠标滚轮滚动才能刷出来的数据便显得苍白无力。
笔者只在以前初学爬虫时用过selenium,毕竟不是经常碰到这么难搞的网页。因此笔者将从0开始出一期针对这种网页爬虫的保姆级教程。
2.1 安装和准备工作
请先装,导入模块
pip install selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
import time接下来正是开始,经过C站搜索,这种网页先要使用selenium模拟的浏览器打开:
url = "https://finance.yahoo.com/quote/%5EDJI/history?period1=1601510400&period2=1656460800&interval=1d&filter=history&frequency=1d&includeAdjustedClose=true"
driver = webdriver.Chrome() #启动模拟浏览器
driver.get(url) # 打开要爬取的网页地址
运行到这里笔者踩第一个雷,报错WebDriverException: Message: 'chromedriver' executable needs to be in PATH:

又是一番折腾笔者找到解决办法:首先要下载一个模拟浏览器的启动文件,地址:ChromeDriver - WebDriver for Chrome - Downloads (chromium.org)
https://chromedriver.chromium.org/downloads
需要注意的是,根据你的Chrome浏览器版本下载:

解压即可,但是安装位置必须在python.exe那个文件的同一个文件夹下:

接着就在环境变量中加入chromedriver.exe文件的地址,笔者是直接放在D盘里:
依次确认后不用重启电脑,直接开CMD启动 chromedriver.exe。如果如下图一样successfuly就代表成功了,之前的代码便可以成功运行

运行代码后会直接打开网页,提示正在受到自动软件的控制:
2.2 用鼠标滑动网页
当然不是真的用鼠标滑动网页,而是通过selenium实现控制,有 to(划到) 和 by(划多少) 两种划动方式,输入xy参数即可实现控制:
driver.execute_script('window.scrollBy(x, y)') #横向滑动x, 纵向滑动y
driver.execute_script('window.scrollTo(x,y)') #滑动到页面的x, y位置通过写循环就可以控制它一直往下划动到底部以达到获取全部数据的目的,下面笔者提供两种划动策略,一种是别人写的,一种是笔者自己写的:
2.2.1 高度判断
思路是获取页面高度——To划动——再次获取页面高度——比较两次高度,如果==证明滑到底部了,结束循环。
while True:
h_before = driver.execute_script('return document.body.scrollHeight;')
time.sleep(2)
driver.execute_script(f'window.scrollTo(0,{h_before})')
time.sleep(2)
h_after = driver.execute_script('return document.body.scrollHeight;')
if h_before == h_after:
break但是!笔者用这种策略发现在Yahoo上不顶用,雅虎不管怎么划动,页面高度永远是一个固定的值。

出于给大家参考的目的笔者还是放上来了,看下面一堆评论说好,说不定别的网页能用上。
2.2.2 顶部距离判断
笔者自己另写了一个,利用到顶部距离判定是否到底:
driver.execute_script('return document.documentElement.scrollTop')其实和刚刚差不多,也是用循环:获取当前位置到页面顶部距离——To划动——再次获取到顶部距离——比较两次距离——固定单位增加,如果==证明到底了,结束循环。
roll = 500
while True:
h_before = driver.execute_script('return document.documentElement.scrollTop')
time.sleep(1)
driver.execute_script(f'window.scrollTo(0,{roll})')
time.sleep(1)
h_after = driver.execute_script('return document.documentElement.scrollTop')
roll += 500
print(h_after, h_before)
if h_before == h_after:
break一次滑500像素可能有点慢,大家可以自行更改每次的划动参数。

这个方案对雅虎有用, 可以看到的确在往下滑了,模拟浏览器上也能看到。至此,不显示数据的问题全部解决。
3: 爬取内容
通过page_source即可将划出来的数据统统导出,返回的数据是str的一堆网页标签。
driver.page_source前面滑动最难的坎都过了,剩下全是基本爬虫操作了,因为这次笔者目标是表格数据,直接pandas read。先把滑动爬到的数据存变量, 然后pandas解析即可,如果是爬取文本数据就需要大家用BeautifulSoup或者正则进一步解析一下了:
content = driver.page_source
data = pd.read_html(content)
table = pd.DataFrame(data[0])4: 完整代码,结果展示
url = # 您需要爬取的网站
driver = webdriver.Chrome()
driver.get(url)
roll = 1000
while True:
h_before = driver.execute_script('return document.documentElement.scrollTop')
time.sleep(1)
driver.execute_script(f'window.scrollTo(0,{roll})')
time.sleep(1)
h_after = driver.execute_script('return document.documentElement.scrollTop')
roll += 1000
print(h_after, h_before)
if h_before == h_after:
break
content = driver.page_source
data = pd.read_html(content)
table = pd.DataFrame(data[0])
print(table)
table.to_csv("market_data.csv")可以看到,笔者已经把道指的近几年数据全部拿到了:

这么简单的网页划动,您,学废了吗?
点赞评论+关注三连,您若不弃,我们风雨共济。
边栏推荐
- 30. Few-shot Named Entity Recognition with Self-describing Networks 阅读笔记
- Sonar:Cognitive Complexity认知复杂度
- Mise en œuvre du codage Huffman et du décodage avec interface graphique par MATLAB
- 111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
- Up meta - Web3.0 world innovative meta universe financial agreement
- 牛客网刷题网址
- Flet教程之 17 Card卡片组件 基础入门(教程含源码)
- 如何理解服装产业链及供应链
- 110.网络安全渗透测试—[权限提升篇8]—[Windows SqlServer xp_cmdshell存储过程提权]
- 人大金仓受邀参加《航天七〇六“我与航天电脑有约”全国合作伙伴大会》
猜你喜欢

Inverted index of ES underlying principle

SwiftUI 教程之如何在 2 秒内实现自动滚动功能

How to connect 5V serial port to 3.3V MCU serial port?

【数据聚类】基于多元宇宙优化DBSCAN实现数据聚类分析附matlab代码

Detailed explanation of debezium architecture of debezium synchronization

Improve application security through nonce field of play integrity API

Superscalar processor design yaoyongbin Chapter 9 instruction execution excerpt

Completion report of communication software development and Application

盘点JS判断空对象的几大方法

ES底层原理之倒排索引
随机推荐
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
千人规模互联网公司研发效能成功之路
DOM parsing XML error: content is not allowed in Prolog
108. Network security penetration test - [privilege escalation 6] - [windows kernel overflow privilege escalation]
Rationaldmis2022 array workpiece measurement
【全栈计划 —— 编程语言之C#】基础入门知识一文懂
Flet教程之 16 Tabs 选项卡控件 基础入门(教程含源码)
College entrance examination composition, high-frequency mention of science and Technology
@What happens if bean and @component are used on the same class?
SwiftUI 教程之如何在 2 秒内实现自动滚动功能
[filter tracking] strapdown inertial navigation simulation based on MATLAB [including Matlab source code 1935]
数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
如何理解服装产业链及供应链
Camera calibration (1): basic principles of monocular camera calibration and Zhang Zhengyou calibration
powershell cs-UTF-16LE编码上线
Tutorial on the principle and application of database system (008) -- exercises on database related concepts
Introduction to three methods of anti red domain name generation
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
Niuke website