返回提交历史
Modified
g4f/Provider/search/GoogleSearch.py
+27
-27
XFEstudio/gpt4free
Refactor _read_search_results method to yield SearchResults directly and improve code clarity
002de937
代码差异
1 个文件
+27
-27
@@ -52,34 +52,34 @@ class GoogleSearch(AsyncGeneratorProvider, ProviderModelMixin):
52
52
continue
53
53
54
54
# Extract search results from the DOM
55
yield await cls._read_search_results(session)
55
yield SearchResults(await cls._read_search_results(session))
56
56
break
57
57
finally:
58
58
await session.close()
59
59
60
async def _read_search_results(session: CDPSession) -> SearchResults:
61
return SearchResults(await session.evaluate_js("""
62
(() => {
63
const results = [];
64
const items = document.querySelectorAll('h3');
65
items.forEach(item => {
66
const linkEl = item.parentElement;
67
const title = item.innerText || '';
68
const link = linkEl.href ? new URL(linkEl.href || '/') : null;
69
if (link) link.searchParams.delete("srsltid")
70
let parentEl = linkEl.parentElement.parentElement.parentElement;
71
let snippetEl = null;
72
while (parentEl) {
73
if (parentEl.nextElementSibling)
74
snippetEl = parentEl.nextElementSibling.querySelector("div div:not(:has(a, svg)) span:not(:has(div, span, a, svg)):not(:empty)");
75
if (snippetEl) break;
76
parentEl = parentEl.parentElement;
77
}
78
const snippet = snippetEl ? snippetEl.innerText : '';
79
if (title && link) {
80
results.push({ title, link: link.toString(), snippet });
81
}
82
});
83
return results;
84
})()
85
"""))
60
async def _read_search_results(session: CDPSession) -> list:
61
return await session.evaluate_js("""
62
(() => {
63
const results = [];
64
const items = document.querySelectorAll('h3');
65
items.forEach(item => {
66
const linkEl = item.parentElement;
67
const title = item.innerText || '';
68
const link = linkEl.href ? new URL(linkEl.href || '/') : null;
69
if (link) link.searchParams.delete("srsltid")
70
let parentEl = linkEl.parentElement.parentElement.parentElement;
71
let snippetEl = null;
72
while (parentEl) {
73
if (parentEl.nextElementSibling)
74
snippetEl = parentEl.nextElementSibling.querySelector("div div:not(:has(a, svg)) span:not(:has(div, span, a, svg)):not(:empty)");
75
if (snippetEl) break;
76
parentEl = parentEl.parentElement;
77
}
78
const snippet = snippetEl ? snippetEl.innerText : '';
79
if (title && link) {
80
results.push({ title, link: link.toString(), snippet });
81
}
82
});
83
return results;
84
})()
85
""")