UnicodeError: сбой кодирования с кодом idna c - PullRequest
0 голосов
/ 27 апреля 2020

почему эта ошибка происходит? Я занимаюсь разработкой механизма распознавания речи (jarvis) и хочу выполнить поиск в Google с помощью модуля googlesearch ... почему это показывает (UnicodeError: label пуст или слишком длинный), ребята, помогите мне разобраться в этой проблеме ..... вот мой код: -

import pyttsx3
import speech_recognition as sr
import pyaudio
import wikipedia
import webbrowser
import os
from googlesearch import search

speech_engine=pyttsx3.init("sapi5")
voices=speech_engine.getProperty("voices")
rate=speech_engine.getProperty("rate")
speech_engine.setProperty("voice",voices[0].id)
speech_engine.setProperty("rate",rate-80)

def speak(audio):
    speech_engine.say(audio)
    speech_engine.runAndWait()


def take_command():
    command_recognize=sr.Recognizer()
    with sr.Microphone() as source_microphone:
        speak("Listening")
        command_recognize.pause_threshold=1
        commands=command_recognize.listen(source_microphone)

    try:
        speak("Recognizing")
        enquiry=command_recognize.recognize_google(commands,language="en-in")
        print(f"User said :- {enquiry}\n")
        return enquiry
    except:
        audio="Please say that again"
        speak(audio)
        return "None"

if (__name__=="__main__"):
    wishme()
    while True:
        enquiry=take_command().lower()
        # 1- searching on wikipedia 

        if ("wikipedia" in enquiry):
            print("Searching Wikipedia")
            enquiry=enquiry.replace("wikipedia","")
            try:
                search_result=wikipedia.summary(enquiry,sentences=2)
            except wikipedia.exceptions.DisambiguationError as err:
                random_item=random.choice(err.options)
                search_result=wikipedia.summary(random_item,sentences=2)
            print(search_result)
            speak(search_result)
  while True:

        # 2- searching for any website on browser
        elif ("open google" in enquiry):
            speak("what you wanna search on google")
            searching_key=take_command().lower()
            results=search(searching_key,tld=".org",num=10,stop=9,pause=2.0)
            for i in results:
                print(i)


Вот ошибка, с которой я сталкиваюсь: -


Traceback (most recent call last):
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\encodings\idna.py", line 165, in encode
    raise UnicodeError("label empty or too long")
UnicodeError: label empty or too long

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "e:/mayankvscode/projects/freddy.py", line 114, in <module>
    for i in results:
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\site-packages\googlesearch\__init__.py", line 279, in search
    get_page(url_home % vars(), user_agent)
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\site-packages\googlesearch\__init__.py", line 176, in get_page
    response = urlopen(request)
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 525, in open
    response = self._open(req, data)
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 542, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 502, in _call_chain
    result = func(*args)
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 1362, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 1319, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 1230, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 1276, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 1225, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 1004, in _send_output
    self.send(msg)
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 944, in send
    self.connect()
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 1392, in connect
    super().connect()
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 915, in connect
    self.sock = self._create_connection(
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\socket.py", line 787, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Users\mayank\AppData\Local\Programs\Python\Python38-32\lib\socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
UnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or too long)
...