东方财富热榜数据包括人气榜、飙升榜等多种类型,涵盖了 A 股市场、ETF 基金、港股市场和美股市场等。这些数据通常每 5 分钟自动更新一次,能够动态展示最新的市场走势。热榜数据可以帮助投资者了解市场的热点和投资者的情绪倾向。
东方财富网的反爬机制主要包括以下几种:
User-Agent
等请求头信息,识别非浏览器请求。通过设置请求头中的 User-Agent
,伪装成浏览器访问网页,可以有效避免被识别为爬虫。此外,还可以设置其他请求头信息,如 Referer
和 Accept
等,以进一步降低被检测的风险。
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
"Referer": "https://www.eastmoney.com",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
}
使用代理 IP 可以隐藏真实 IP 地址,避免因频繁请求导致 IP 被封禁。可以通过购买代理服务或使用免费代理池来实现。
import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
# 代理服务器信息
proxyHost = "www.16yun.cn"
proxyPort = "5445"
proxyUser = "16QMSOML"
proxyPass = "280651"
# 构建代理参数
proxies = {
"http": f"http://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}",
"https": f"https://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}"
}
def get_eastmoney_hot_list():
url = "https://dq.10jqka.com.cn/fuyao/hot_list_data/out/hot_list/v1/stock?stock_type=a&type=hour&list_type=normal" # 热榜数据接口
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
"Referer": "https://www.eastmoney.com"
}
try:
# 使用代理发送请求
response = requests.get(url, headers=headers, proxies=proxies, timeout=10)
if response.status_code == 200:
data = response.json() # 解析 JSON 数据
hot_list = []
for item in data['data']:
stock_code = item['code']
stock_name = item['name']
rank = item['rank']
hot_list.append({
'stock_code': stock_code,
'stock_name': stock_name,
'rank': rank
})
return pd.DataFrame(hot_list)
else:
print("获取热榜数据失败,状态码:", response.status_code)
return None
except requests.exceptions.RequestException as e:
print("请求异常:", e)
return None
# 获取热榜数据
hot_list_df = get_eastmoney_hot_list()
if hot_list_df is not None:
print(hot_list_df)
else:
print("未能成功获取热榜数据")
在请求之间添加适当的延迟,可以降低请求频率,避免触发反爬机制。例如,每次请求之间暂停 2 秒。
import time
time.sleep(2)
对于动态加载的内容,可以使用 Selenium 模拟浏览器行为,获取完整的页面数据。Selenium 能够模拟人类操作浏览器,从而绕过反爬机制。
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.eastmoney.com")
time.sleep(5) # 等待页面加载
html_content = driver.page_source
driver.quit()
Tushare 提供了获取东方财富热榜数据的接口,可以直接调用获取数据。这种方式简单高效,但需要注册账号并获取积分。
import tushare as ts
ts.set_token('your_token') # 替换为你的 Tushare Token
pro = ts.pro_api()
# 获取热榜数据
df = pro.dc_hot(trade_date='20250704', market='A股市场', hot_type='人气榜', is_new='Y')
print(df)
如果需要通过爬虫获取热榜数据,可以结合上述反爬机制破解方法实现。以下是一个完整的爬虫实现示例
import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
def get_eastmoney_hot_list():
url = "https://dq.10jqka.com.cn/fuyao/hot_list_data/out/hot_list/v1/stock?stock_type=a&type=hour&list_type=normal" # 热榜数据接口[^4^]
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
"Referer": "https://www.eastmoney.com"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json() # 解析 JSON 数据
hot_list = []
for item in data['data']:
stock_code = item['code']
stock_name = item['name']
rank = item['rank']
hot_list.append({
'stock_code': stock_code,
'stock_name': stock_name,
'rank': rank
})
return pd.DataFrame(hot_list)
else:
print("获取热榜数据失败,状态码:", response.status_code)
return None
# 获取热榜数据
hot_list_df = get_eastmoney_hot_list()
if hot_list_df is not None:
print(hot_list_df)
获取到热榜数据后,可以将其存储为 CSV 文件或数据库中,以便后续分析。
# 将数据保存为 CSV 文件
hot_list_df.to_csv('eastmoney_hot_list.csv', index=False, encoding='utf-8-sig')
通过上述方法,我们可以有效地破解东方财富网的反爬机制,获取热榜数据。在实际应用中,建议结合多种方法,如设置请求头、使用代理 IP 和添加延迟,以确保数据获取的稳定性和安全性。此外,使用 Tushare 等专业数据接口可以进一步简化数据获取过程。