How to Scrape Fund Data with Python: A Step‑by‑Step Guide
This article walks you through using Python's requests and lxml libraries to crawl a fund website, extract stock IDs, fetch detailed fund information, process the data, and save it to CSV for further analysis, complete with code snippets and screenshots.
Introduction
Hello, I am a Python enthusiast sharing a practical example of using a web crawler to obtain fund data from a public website. The target site does not encrypt its data, so the information is directly visible in the page source.
Data Acquisition
The goal is to scrape a fund website that lists fund codes. By clicking any code you can reach a detail page whose URL follows a predictable pattern based on the code.
Using the browser's network inspector you can see that the request only varies by a single parameter pi, which corresponds to the page number. Constructing a request with this parameter allows you to retrieve the data programmatically.
Implementation
Below are the key code snippets used to fetch the stock IDs and then retrieve detailed fund information.
Fetching Stock IDs
response = requests.get(url, headers=headers, params=params, verify=False)
pattern = re.compile(r'.*?"(?P<items>.*?)".*?', re.S)
result = re.finditer(pattern, response.text)
ids = []
for item in result:
# print(item.group('items'))
gp_id = item.group('items').split(',')[0]Fetching Fund Details
response = requests.get(url, headers=headers)
response.encoding = response.apparent_encoding
selectors = etree.HTML(response.text)
unit_value1 = selectors.xpath('//dl[@class="dataItem02"]/dd[1]/span[1]/text()')[0]
unit_value2 = selectors.xpath('//dl[@class="dataItem02"]/dd[1]/span[2]/text()')[0]
total_value = selectors.xpath('//dl[@class="dataItem03"]/dd[1]/span/text()')[0]
info_list = selectors.xpath('//div[@class="infoOfFund"]/table//text()')The extracted strings are then cleaned and written to a csv file, as shown in the following screenshot.
Conclusion
This tutorial demonstrates that scraping fund data with Python is straightforward when the website does not employ obfuscation. After obtaining the data you can perform further statistical analysis or visualisation. The same approach can be adapted to other fund categories by adjusting the request parameters.
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.
