Extract Numbers from Web Pages with Python: A Quick Regex Guide
This article demonstrates how to use Python's requests and lxml libraries to scrape web pages and apply regular expressions for extracting both integer and floating‑point numbers, providing clear code examples and step‑by‑step explanations.
Introduction
A user asked about a Python web‑scraping problem; the answer includes a complete example that fetches a page, parses it with lxml.etree, and extracts price data.
url = "http://zw.hainan.gov.cn/wssc/ec/jlyhnkj.html"
resp = requests.get(url)
text = resp.text
parse = etree.HTML(text)
price = parse.xpath("//div[@class='productlist']/ul/li/div[4]/text()")
price = [i.strip() for i in price if i.strip()]
print(price)The code shows how to clean the extracted list by removing empty strings.
Regex Extraction
To capture numeric values such as "身高180.3cm", a simple regular expression can be used:
\d+\.\d+For a more flexible solution that matches both integers and decimals, the following pattern is applied to a list of sample strings:
d = ["身高180.3cm", "身高180.3", "身高180.3厘米", "higt180.3cm", "higt180.3厘米", "身高180.3cm", "higt180cm"]
for s in d:
r = re.findall(r'\d+\.\d+|\d+', s)
print(r)This loop prints the numeric part of each string, handling both floating‑point numbers and whole numbers.
Conclusion
The provided examples illustrate how to combine HTTP requests, HTML parsing, and regular expressions in Python to efficiently extract numeric data from web content.
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.
