Skip to content

Commit fe15df3

Browse files
Update covid_stats_via_xpath.py
1 parent d4f9ca4 commit fe15df3

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

web_programming/covid_stats_via_xpath.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
This is to show simple COVID19 info fetching from worldometers site using lxml
2+
This is to show simple COVID19 info fetching from worldometers archive site using lxml
33
* The main motivation to use lxml in place of bs4 is that it is faster and therefore
44
more convenient to use in Python web projects (e.g. Django or Flask-based)
55
"""
@@ -9,30 +9,23 @@
99
# dependencies = [
1010
# "httpx",
1111
# "lxml",
12-
# "requests_html",
1312
# ]
1413
# ///
1514

1615
from typing import NamedTuple
1716

1817
import httpx
1918
from lxml import html
20-
from requests_html import HTMLSession
2119

2220

2321
class CovidData(NamedTuple):
24-
cases: int
25-
deaths: int
26-
recovered: int
22+
cases: str
23+
deaths: str
24+
recovered: str
2725

2826

29-
def covid_stats(url: str = "https://www.worldometers.info/coronavirus/") -> CovidData:
27+
def covid_stats(url: str = "https://web.archive.org/web/20250825095350/https://www.worldometers.info/coronavirus/") -> CovidData:
3028
xpath_str = '//div[@class = "maincounter-number"]/span/text()'
31-
session = HTMLSession()
32-
r = session.get(url)
33-
print(r.html.html)
34-
r.html.render()
35-
print(r.html.html)
3629
return CovidData(
3730
*html.fromstring(httpx.get(url, timeout=10).content).xpath(xpath_str)
3831
)

0 commit comments

Comments
 (0)