forked from binjie09/duckduckgo-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request binjie09#11 from qi-mooo/main
Fix Internal Server Error
- Loading branch information
Showing
2 changed files
with
19 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,27 @@ | ||
from flask import Flask, request | ||
from duckduckgo_search import ddg | ||
app = Flask(__name__) | ||
from duckduckgo_search import DDGS | ||
from itertools import islice | ||
|
||
app = Flask(__name__) | ||
|
||
@app.route('/search') | ||
def search(): # put application's code here | ||
def search(): | ||
# 从请求参数中获取关键词 | ||
keywords = request.args.get('q') | ||
print(request.args.get('max_results')) | ||
max_results = int(request.args.get('max_results') or "3") | ||
results = ddg(keywords, region='wt-wt', max_results=max_results) | ||
print(results) | ||
return results | ||
# 从请求参数中获取最大结果数,如果未指定,则默认为10 | ||
max_results = int(request.args.get('max_results', 10)) | ||
results = [] | ||
|
||
with DDGS() as ddgs: | ||
# 使用DuckDuckGo搜索关键词 | ||
ddgs_gen = ddgs.text(keywords, safesearch='Off', timelimit='y', backend="lite") | ||
# 从搜索结果中获取最大结果数 | ||
for r in islice(ddgs_gen, max_results): | ||
results.append(r) | ||
|
||
# 返回一个json响应,包含搜索结果 | ||
return {'results': results} | ||
|
||
if __name__ == '__main__': | ||
app.run(host='0.0.0.0') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters