Skip to content

Commit 73f1406

Browse files
committed
Updated. html_parsing/ollama_com/dump_models.py. Sorted by title
1 parent 56a8d1f commit 73f1406

2 files changed

Lines changed: 225 additions & 222 deletions

File tree

‎html_parsing/ollama_com/dump_models.py‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,29 @@ def parse_from(cls, item: Tag, base_url: str) -> Self:
5353

5454

5555
url: str = "https://ollama.com/search"
56-
with open(PATH_DUMP, "w", encoding="UTF-8") as f:
57-
while True:
58-
print(f"Load: {url}")
5956

60-
rs = session.get(url)
61-
rs.raise_for_status()
57+
items: list[Model] = []
58+
while True:
59+
print(f"Load: {url}")
6260

63-
soup = BeautifulSoup(rs.content, "html.parser")
61+
rs = session.get(url)
62+
rs.raise_for_status()
6463

65-
for item in soup.select("li[x-test-model] > a:has([x-test-search-response-title])"):
66-
model = Model.parse_from(item, base_url=rs.url)
67-
print(model)
68-
f.write(json.dumps(asdict(model), ensure_ascii=False) + "\n")
64+
soup = BeautifulSoup(rs.content, "html.parser")
6965

70-
f.flush()
66+
for item in soup.select("li[x-test-model] > a:has([x-test-search-response-title])"):
67+
model = Model.parse_from(item, base_url=rs.url)
68+
print(model)
69+
items.append(model)
7170

72-
next_page_el: Tag | None = soup.select_one("[hx-trigger=revealed][hx-get]")
73-
if not next_page_el:
74-
break
71+
next_page_el: Tag | None = soup.select_one("[hx-trigger=revealed][hx-get]")
72+
if not next_page_el:
73+
break
7574

76-
url = urljoin(rs.url, next_page_el["hx-get"])
75+
url = urljoin(rs.url, next_page_el["hx-get"])
76+
time.sleep(1)
7777

78-
time.sleep(1)
78+
print(f"Writing to file: {FILE_NAME}")
79+
with open(PATH_DUMP, "w", encoding="UTF-8") as f:
80+
for model in sorted(items, key=lambda x: x.title):
81+
f.write(json.dumps(asdict(model), ensure_ascii=False) + "\n")

0 commit comments

Comments
 (0)