iT邦幫忙

0

Python 爬蟲 出現SSLError (Centos7)

最近剛學linux centos

當我使用python爬中央氣象局的資料的時候

都會出現SSLERROR的情況

爬其他網站都不會!!

有邦友遇到相同的問題嗎?


這是我的py檔

import json
import requests
url = 'https://opendata.cwb.gov.tw/api/v1/rest/datastore/F-C0032-001?Authorization=CWB-37A&format=JSON&elementName=PoP'
htmls = requests.get(url)
data = htmls.json()

p.s.
已經試過,一樣給Error

htmls = requests.get(url,verify="False")

執行後出現的錯誤

Traceback (most recent call last):
  File "/home/yen/env/lib/python3.7/site-packages/urllib3/connectionpool.py", line 591, in urlopen
    conn = self._get_conn(timeout=pool_timeout)
  File "/home/yen/env/lib/python3.7/site-packages/urllib3/connectionpool.py", line 249, in _get_conn
    return conn or self._new_conn()
  File "/home/yen/env/lib/python3.7/site-packages/urllib3/connectionpool.py", line 818, in _new_conn
    raise SSLError("Can't connect to HTTPS URL because the SSL "
urllib3.exceptions.SSLError: Can't connect to HTTPS URL because the SSL module is not available.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/yen/env/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/home/yen/env/lib/python3.7/site-packages/urllib3/connectionpool.py", line 641, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/yen/env/lib/python3.7/site-packages/urllib3/util/retry.py", line 399, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='opendata.cwb.gov.tw', port=443): Max retries exceeded with url: /api/v1/rest/datastore/F-C0032-001?Authorization=CWB-3CE2FA7&format=JSON&elementName=PoP (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available."))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/yen/env/lib/python3.7/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/home/yen/env/lib/python3.7/site-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/yen/env/lib/python3.7/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/yen/env/lib/python3.7/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/home/yen/env/lib/python3.7/site-packages/requests/adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='opendata.cwb.gov.tw', port=443): Max retries exceeded with url: /api/v1/rest/datastore/F-C0032-001?Authorization=CWB-NA-F8C-4F1-B32-0225CE2FA7&format=JSON&elementName=PoP (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available."))

感恩

1 個回答

1
黃彥儒
iT邦高手 1 級 ‧ 2019-07-24 23:48:34
最佳解答

改這樣子

htmls = requests.get(url,verify=False)
看更多先前的回應...收起先前的回應...

還是出現一樣的error code/images/emoticon/emoticon02.gif

黃彥儒 iT邦高手 1 級 ‧ 2019-07-26 10:47:44 檢舉

我跑了一下你的Code是回傳401 Forbidden: Authorization key is not correct.

Authorization=CWB-37A
這組KEY我有做刪減的動作
怕有人會拿我的KEY一直要資料
實際的key我私給您

黃彥儒 iT邦高手 1 級 ‧ 2019-07-26 13:15:28 檢舉

https://ithelp.ithome.com.tw/upload/images/20190726/20088395StUhNA5auf.png

感恩!
已解決
我重灌了centos7
好像要透過虛擬環境才不會出現錯誤
參考資訊
https://phoenixnap.com/kb/how-to-install-python-3-centos-7
https://blog.teststation.org/centos/python/2016/05/11/installing-python-virtualenv-centos-7/

我要發表回答

立即登入回答