How to Scrape Real-Time Stock Data from Eastmoney with Python
This article demonstrates two Python approaches—using high-level requests and low-level HTTPConnection—to fetch intraday stock data from Eastmoney, providing complete code examples, URL discovery steps, and practical tips for data‑mining enthusiasts.
1. Introduction
In a Python community, members discussed how to fetch intraday stock data from Eastmoney, showing a sample screenshot of the time‑frequency data.
A previous guide on retrieving other fund information with Python was referenced (link omitted).
A group member shared an initial code snippet.
# -*- coding: utf-8 -*-
# @Time : 2022/6/1 23:10
# @Author : Thresh
# @File : 1.py
import json
import requests
for page in range(5):
params = (
('pagesize', '144'),
('ut', '7eea3edcaed734bea9cbfc24409ed989'),
('dpt', 'wzfscj'),
('cb', 'jQuery1124029337350072397084_1631343037828'),
('pageindex', str(page)),
('id', '6009051'),
('sort', '1'),
('ft', '1'),
('code', '688103'),
('market', '1'),
('_', '1631343037827'),
)
response = requests.get('http://push2ex.eastmoney.com/getStockFenShi', params=params, verify=False)
response = response.text
response = response.split('(')[1]
response = response.split(')')[0]
response = json.loads(response)
data_lis = response["data"]["data"]
for data in data_lis:
print(data)2. Implementation
The goal is to obtain minute‑level stock data, and two methods are presented.
Method 1 discovers the request URL (shown below).
Opening this URL returns dense JSON data.
Method 1 uses urllib and pandas to request the URL and parse the JSON.
import urllib.request
import pandas as pd
url = 'http://16.push2.eastmoney.com/api/qt/stock/details/sse?fields1=f1,f2,f3,f4&fields2=f51,f52,f53,f54,f55&mpi=2000&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&pos=-0&secid=1.688103'
with urllib.request.urlopen(url=url) as r:
data = r.readline().decode().lstrip('data:')
print(data)
df = pd.DataFrame(eval(data)['data'])
print(df)Method 2 employs a low‑level HTTPConnection to request the same endpoint, revealing the underlying IP address.
# -*- coding: utf-8 -*-
from http.client import HTTPConnection
url = 'http://97.push2.eastmoney.com/'
# 119.3.12.115:80
http_conn = HTTPConnection('119.3.12.115', 80)
params = "/api/qt/stock/details/sse?fields1=f1,f2,f3,f4&fields2=f51,f52,f53,f54,f55&mpi=2000&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&pos=-0&secid=0.300059"
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Host": "97.push2.eastmoney.com",
"Pragma": "no-cache",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36"
}
http_conn.request("GET", params, headers=headers)
with http_conn.getresponse() as response:
while not response.closed:
for line in response:
print(line.decode("utf-8"))Method 1 is simpler, while Method 2 offers deeper control; both can be adapted for similar data‑fetching tasks.
3. Conclusion
The article provides two complete Python web‑scraping solutions for retrieving Eastmoney intraday stock data, including URL discovery, code implementation, and execution results, enabling readers to apply these techniques to their own data‑mining projects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
