How to Fix Common Python Web‑Scraping Issues with Ready‑to‑Use Code
This article walks through a real Python web‑scraping problem, explains why missing request headers cause failures, and provides complete, runnable code snippets—including header setup, cookie handling, and request loops—to retrieve stock data from Xueqiu, followed by a concise summary of the solution.
Introduction
Hello, I’m Pi Pi. Recently a question about Python web crawling was raised in a group, and I’m sharing the solution here.
Implementation Process
The issue can be solved with a simple for‑loop, but the original code lacked necessary request headers, resulting in no data being returned. Below are the detailed code examples.
import requests
import time
headers = {
"authority": "stock.xueqiu.com",
"accept": "*/*",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
"origin": "https://xueqiu.com",
"referer": "https://xueqiu.com/S/SH600600",
"sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Microsoft Edge\";v=\"114\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.51"
}
cookies = {
"xq_a_token": "57b2a0b86ca3e943ee1ffc69509952639be342b9",
"xqat": "57b2a0b86ca3e943ee1ffc69509952639be342b9",
"xq_r_token": "59c1392434fb1959820e4323bb26fa31dd012ea4",
"xq_id_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...",
"u": "561687871945884",
"device_id": "27b6ec56b772ea40c8582168f00a7604",
"Hm_lvt_1db88642e346389874251b5a1eded6e3": "1687871949",
"s": "ci1eygzbit",
"is_overseas": "0",
"Hm_lpvt_1db88642e346389874251b5a1eded6e3": "1687872001"
}
url = "https://stock.xueqiu.com/v5/stock/quote.json"
symbols = ['SH600600', 'SH600519', 'SH301183']
for symbol in symbols:
params = {
"symbol": f"{symbol}",
"extend": "detail"
}
response = requests.get(url, headers=headers, cookies=cookies, params=params)
time.sleep(3)
print(response.text)
print(response)Running the code yields the expected JSON responses, as shown in the following screenshot.
Another contributor added a more concise version that directly builds the request URL for each symbol:
for i in ['SH600519', 'SZ300600', 'SZ301183']:
url = f"https://stock.xueqiu.com/v5/stock/quote.json?symbol={i}&extend=detail"
response = requests.get(url=url, headers=headers, cookies=cookies)
json_data = response.json()
print(json_data)The problem was successfully resolved, demonstrating that multiple approaches can work as long as the request headers and cookies are correctly set.
Conclusion
This article presented a specific Python web‑scraping issue, explained the root cause, and provided complete code solutions to help readers overcome similar obstacles.
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.
