Spaces:
Sleeping
Sleeping
fix the classficiations retrieval
Browse files
scrap.py
CHANGED
|
@@ -75,21 +75,24 @@ async def scrap_patent_async(client: AsyncClient, patent_url: str) -> PatentScra
|
|
| 75 |
# pub_num = soup.select_one("h2#pubnum").get_text(strip=True)
|
| 76 |
# get the h2 with id ="pubnum" and extract the text
|
| 77 |
|
| 78 |
-
# Classification codes (CPC + IPC, flat list, deduplicated by code)
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
classifications = []
|
| 81 |
seen_codes: set[str] = set()
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
| 93 |
|
| 94 |
return PatentScrapResult(
|
| 95 |
# publication_number=pub_num,
|
|
|
|
| 75 |
# pub_num = soup.select_one("h2#pubnum").get_text(strip=True)
|
| 76 |
# get the h2 with id ="pubnum" and extract the text
|
| 77 |
|
| 78 |
+
# Classification codes (CPC + IPC, flat list, deduplicated by code).
|
| 79 |
+
# Google Patents renders each code in a <span> inside a <li>. Leaf entries
|
| 80 |
+
# (no nested <ul>) carry exactly one code; parent entries are breadcrumbs.
|
| 81 |
+
leaf_code_re = re.compile(r'^[A-Z]\d{2}[A-Z]\d+/\d+$')
|
| 82 |
classifications = []
|
| 83 |
seen_codes: set[str] = set()
|
| 84 |
+
for li in soup.find_all("li"):
|
| 85 |
+
if li.find("ul"):
|
| 86 |
+
continue
|
| 87 |
+
span = li.find("span")
|
| 88 |
+
if not span:
|
| 89 |
+
continue
|
| 90 |
+
code = span.get_text(strip=True)
|
| 91 |
+
if leaf_code_re.match(code) and code not in seen_codes:
|
| 92 |
+
seen_codes.add(code)
|
| 93 |
+
full_text = li.get_text(separator=" ", strip=True)
|
| 94 |
+
desc = full_text.replace(code, "", 1).strip().lstrip("—").lstrip("-").strip()
|
| 95 |
+
classifications.append(ClassificationCode(code=code, description=desc))
|
| 96 |
|
| 97 |
return PatentScrapResult(
|
| 98 |
# publication_number=pub_num,
|