Python3 使用googletrans报错,‘NoneType‘ object has no attribute ‘group‘

Python3 使用googletrans报错

项目场景:

提示:将爬取的英文资讯通过googletrans翻译成中文。

问题描述:

提示:通过googletrans将外文翻译成中文,原本好好的,突然报错了。
代码部分:

def googletr(source):
	from googletrans import Translator 
	translator = Translator(service_urls=['translate.google.cn']) 
	# source = '我还是不开心!' 
	text = translator.translate(source,src='en',dest='zh-cn').text 
	print(text)
	return text

错误描述:

title, timeUpdate, content无效
主程序,异常!!! 'NoneType' object has no attribute 'group'

Traceback (most recent call last):
  File "F:/robot/cryptonews/cryptonews.py", line 192, in <module>
    title, timeUpdate, content = yd.googleTr(titles), yd.googleTr(timeUpdates), yd.googleTr(contents)
  File "F:/robot/cryptonews/cryptonews.py", line 172, in googleTr
    text = translator.translate(source, src=src, dest=dest).text
  File "F:\robot\lib\site-packages\googletrans\client.py", line 182, in translate
    data = self._translate(text, dest, src, kwargs)
  File "F:\robot\lib\site-packages\googletrans\client.py", line 78, in _translate
    token = self.token_acquirer.do(text)
  File "F:\robot\lib\site-packages\googletrans\gtoken.py", line 194, in do
    self._update()
  File "F:\robot\lib\site-packages\googletrans\gtoken.py", line 62, in _update
    code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
AttributeError: 'NoneType' object has no attribute 'group'

’‘’

打印出来的描述是:主程序,异常!!! ‘NoneType’ object has no attribute ‘group’
缺少group类型导致的程序崩溃。


原因分析:

一开始以为是Chromedriver的问题,重新安装了Chrome以及Chromedriver都没有用,T.T


解决方案:

提示:找了好多教程都没有解决,包括重装googletrans等等
后来终于从大佬处找到了问题:问题在于Google修改了api,所以需要先卸载之前的包。在重新安装一个googletrans-temp
,而源代码是不需要修改的哦!

pip uninstall googletrans googletrans-temp
pip install googletrans-temp

安装googletrans-temp成功,重新运行程序,正常即可。

新问题:

但有童鞋可能还会出现,连接超时的问题:

Traceback (most recent call last):
  File "F:\robot\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "F:\robot\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
    raise err
  File "F:\robot\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:\robot\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen
    chunked=chunked,
  File "F:\robot\lib\site-packages\urllib3\connectionpool.py", line 392, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "E:\python3.7\lib\http\client.py", line 1252, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "E:\python3.7\lib\http\client.py", line 1298, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "E:\python3.7\lib\http\client.py", line 1247, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "E:\python3.7\lib\http\client.py", line 1026, in _send_output
    self.send(msg)
  File "E:\python3.7\lib\http\client.py", line 966, in send
    self.connect()
  File "F:\robot\lib\site-packages\urllib3\connection.py", line 187, in connect
    conn = self._new_conn()
  File "F:\robot\lib\site-packages\urllib3\connection.py", line 172, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x0000018ACB136B08>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:/robot/cryptonews/cryptonews.py", line 299, in <module>
    yd.quit()
  File "F:/robot/cryptonews/cryptonews.py", line 177, in quit
    cookies = self.dr.get_cookies()
  File "F:\robot\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 841, in get_cookies
    return self.execute(Command.GET_ALL_COOKIES)['value']
  File "F:\robot\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 319, in execute
    response = self.command_executor.execute(driver_command, params)
  File "F:\robot\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 374, in execute
    return self._request(command_info[0], url, body=data)
  File "F:\robot\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 397, in _request
    resp = self._conn.request(method, url, body=body, headers=headers)
  File "F:\robot\lib\site-packages\urllib3\request.py", line 76, in request
    method, url, fields=fields, headers=headers, **urlopen_kw
  File "F:\robot\lib\site-packages\urllib3\request.py", line 97, in request_encode_url
    return self.urlopen(method, url, **extra_kw)
  File "F:\robot\lib\site-packages\urllib3\poolmanager.py", line 336, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "F:\robot\lib\site-packages\urllib3\connectionpool.py", line 765, in urlopen
    **response_kw
  File "F:\robot\lib\site-packages\urllib3\connectionpool.py", line 765, in urlopen
    **response_kw
  File "F:\robot\lib\site-packages\urllib3\connectionpool.py", line 765, in urlopen
    **response_kw
  File "F:\robot\lib\site-packages\urllib3\connectionpool.py", line 725, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "F:\robot\lib\site-packages\urllib3\util\retry.py", line 439, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=60552): Max retries exceeded with url: /session/5eb933f353eb3553425e44a68f8fc25b/cookie (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))

新问题解决方案:

由于国内对Google的封禁,所以google.com的都无法访问,这时需要大家从源码中修改,首先在googletrans中找到gtoken.py,urls.py 和client.py这三个文件。将这三个文件中的

translate.google.com

替换成

translate.google.cn

终于成功了,我太难了!!!

参考链接:https://blog.csdn.net/qq_15557299/article/details/109317481?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160387037719724842924784%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=160387037719724842924784&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v28-2-109317481.first_rank_ecpm _v3_pc_rank_v2&utm_term=googletrans&spm=1018.2118.3001.4187

你可能感兴趣的:(python,爬虫,研发管理,python,爬虫)