Spaces:
Runtime error
Runtime error
import requests | |
import json | |
import sys | |
import sys | |
import io | |
import sys | |
sys.stdout.reconfigure(encoding='utf-8') | |
def search_bing(key_word, bing_api_key): | |
headers = { | |
"Ocp-Apim-Subscription-Key": bing_api_key, | |
"X-MSEdge-ClientID": "00B4230B74496E7A13CC2C1475056FF4", | |
"X-MSEdge-ClientIP": "11.22.33.44", | |
"X-Search-Location": "lat:55;long:-111;re:22", | |
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.65 Safari/537.36" | |
} | |
url = "https://api.bing.microsoft.com/v7.0/search" | |
params = {"q": key_word, "mkt": "zh-CN", "count": "2"} | |
# proxies = { | |
# "http": "http://127.0.0.1:10809", | |
# "https": "http://127.0.0.1:10809" | |
# } | |
response = requests.get(url, headers=headers, params=params) | |
json_data = response.json() | |
search_result = "" | |
for item in json_data.get("webPages", {}).get("value", []): | |
name = item.get("name", "") | |
snippet = item.get("snippet", "") | |
search_result += f"Name: {name}\nSnippet: {snippet}\n\n" | |
return search_result | |
if __name__ == "__main__": | |
key_words_str = sys.argv[1] # 将关键字作为一个字符串获取 | |
bing_api_key = sys.argv[2] | |
api_key = sys.argv[3] | |
api_url_base = sys.argv[4] | |
key_words = key_words_str.split(',') # 在代码中手动拆分关键字 | |
search_result = "" | |
for key_word in key_words: | |
search_result += search_bing(key_word, bing_api_key) | |
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace') | |
# 直接调用qa.py,并将search_result作为参数传递 | |
# import qa | |
# qa.main(search_result, api_key, api_url_base) | |
print(search_result) |