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

View File

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