🐛 修复 google 无法使用的问题

This commit is contained in:
xtaodada 2020-10-01 17:59:52 +08:00
parent 634acda30e
commit 321021efe4
No known key found for this signature in database
GPG Key ID: EE4DC37B55E24736
2 changed files with 11 additions and 31 deletions

View File

@ -2,10 +2,7 @@
from googletrans import Translator, LANGUAGES
from os import remove
from requests import get
from time import sleep
from threading import Thread
from bs4 import BeautifulSoup
from magic_google import MagicGoogle
from gtts import gTTS
from re import compile as regex_compile
from pagermaid import log
@ -103,11 +100,9 @@ async def tts(context):
parameters="<query>")
async def googletest(context):
""" Searches Google for a string. """
USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:65.0) Gecko/20100101 Firefox/65.0"
headers = {"user-agent": USER_AGENT}
mg = MagicGoogle()
reply = await context.get_reply_message()
query = context.arguments
lang = config['application_language']
if query:
pass
elif reply:
@ -117,31 +112,16 @@ async def googletest(context):
return
query = query.replace(' ', '+')
URL = [('https://google.com.hk/search?q=' + query),('https://google.com/search?q=' + query)]
await context.edit("正在拉取结果 . . .")
count = 0
for g in URL:
count += 1
resp = get(g, headers=headers)
if resp.status_code == 200:
break
elif count == 2 and not resp.status_code == 200:
results = ""
for i in mg.search(query=query, num=int(config['result_length'])):
try:
title = i['text'][0:30] + '...'
link = i['url']
results += f"\n[{title}]({link}) \n"
except:
await context.edit("连接到 google服务器 失败")
return
else:
pass
soup = BeautifulSoup(resp.content, "html.parser")
results = ""
count = 0
for g in soup.find_all('div', class_='r'):
if count == int(config['result_length']):
break
count += 1
anchors = g.find_all('a')
if anchors:
title = g.find('h3').text
link = anchors[0]['href']
results += f"\n[{title}]({link}) \n"
await context.edit(f"**Google** |`{query}`| 🎙 🔍 \n"
f"{results}",
link_preview=False)

View File

@ -12,7 +12,6 @@ requests
pytz
cowpy
googletrans
beautifulsoup4
gtts
gtts-token
wordcloud
@ -33,4 +32,5 @@ flask_wtf
wtforms
cheroot
PySocks
certifi
certifi
magic_google