Browse Source

Fix baidusearch and duckduckgosearch (#1488)

### What problem does this PR solve?



### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
tags/v0.9.0
H 1 year ago
parent
commit
d5618749c9
No account linked to committer's email address
2 changed files with 11 additions and 9 deletions
  1. 5
    4
      graph/component/baidu.py
  2. 6
    5
      graph/component/duckduckgosearch.py

+ 5
- 4
graph/component/baidu.py View File

url_res = re.findall(r"'url': \\\"(.*?)\\\"}", response.text) url_res = re.findall(r"'url': \\\"(.*?)\\\"}", response.text)
title_res = re.findall(r"'title': \\\"(.*?)\\\",\\n", response.text) title_res = re.findall(r"'title': \\\"(.*?)\\\",\\n", response.text)
body_res = re.findall(r"\"contentText\":\"(.*?)\"", response.text) body_res = re.findall(r"\"contentText\":\"(.*?)\"", response.text)
baidu_res = [re.sub('<em>|</em>', '', '<a href="' + url + '">' + title + '</a> ' + body) for url, title, body
in zip(url_res, title_res, body_res)]
baidu_res = [{"content": re.sub('<em>|</em>', '', '<a href="' + url + '">' + title + '</a> ' + body)} for url, title, body in zip(url_res, title_res, body_res)]
del body_res, url_res, title_res del body_res, url_res, title_res
print(baidu_res, ":::::::::::::::::::::::::::::::::")
return Baidu.be_output(baidu_res)
df = pd.DataFrame(baidu_res)
print(df, ":::::::::::::::::::::::::::::::::")
return df

+ 6
- 5
graph/component/duckduckgosearch.py View File

ans = self.get_input() ans = self.get_input()
ans = " - ".join(ans["content"]) if "content" in ans else "" ans = " - ".join(ans["content"]) if "content" in ans else ""
if not ans: if not ans:
return Baidu.be_output(self._param.no)
return DuckDuckGoSearch.be_output(self._param.no)
if self.channel == "text": if self.channel == "text":
with DDGS() as ddgs: with DDGS() as ddgs:
# {'title': '', 'href': '', 'body': ''} # {'title': '', 'href': '', 'body': ''}
duck_res = ['<a href="' + i["href"] + '">' + i["title"] + '</a> ' + i["body"] for i in
duck_res = [{"content": '<a href="' + i["href"] + '">' + i["title"] + '</a> ' + i["body"]} for i in
ddgs.text(ans, max_results=self._param.top_n)] ddgs.text(ans, max_results=self._param.top_n)]
elif self.channel == "news": elif self.channel == "news":
with DDGS() as ddgs: with DDGS() as ddgs:
# {'date': '', 'title': '', 'body': '', 'url': '', 'image': '', 'source': ''} # {'date': '', 'title': '', 'body': '', 'url': '', 'image': '', 'source': ''}
duck_res = ['<a href="' + i["url"] + '">' + i["title"] + '</a> ' + i["body"] for i in
duck_res = [{"content": '<a href="' + i["url"] + '">' + i["title"] + '</a> ' + i["body"]} for i in
ddgs.news(ans, max_results=self._param.top_n)] ddgs.news(ans, max_results=self._param.top_n)]
print(duck_res, ":::::::::::::::::::::::::::::::::")
return DuckDuckGoSearch.be_output(duck_res)
df = pd.DataFrame(duck_res)
print(df, ":::::::::::::::::::::::::::::::::")
return df

Loading…
Cancel
Save