Fixed JSON search function to improve link extraction by targeting specific result containers and retrieving all relevant text, enhancing the accuracy of search results.

This commit is contained in:
Don-Swanson 2025-09-30 20:12:26 -05:00
parent 0fe29daaf1
commit 442060b2ef
No known key found for this signature in database
GPG key ID: C6A6ACD574A005E5

View file

@ -407,15 +407,30 @@ def search():
json_soup = bsoup(str(response), 'html.parser')
results = []
seen = set()
for a in json_soup.find_all('a', href=True):
href = a['href']
if not href.startswith('http'):
# Find all result containers (using known result classes)
result_divs = json_soup.find_all('div', class_=['ZINbbc', 'ezO2md'])
for div in result_divs:
# Find the first valid link in this result container
link = None
for a in div.find_all('a', href=True):
if a['href'].startswith('http'):
link = a
break
if not link:
continue
href = link['href']
if href in seen:
continue
text = a.get_text(strip=True)
# Get all text from the result container, not just the link
text = div.get_text(separator=' ', strip=True)
if not text:
continue
seen.add(href)
results.append({'href': href, 'text': text})