为什么在进行网页抓取时会出错

时间:2019-09-30 04:39:51

标签: python web-scraping

我目前正在制作一个Web抓取项目,并按照教程进行操作。我正在使用python 3.7.4,并已使用'pip install --user <pakage_name>'安装beautifulsoup4,请求和熊猫。我的文件名为try.py,代码为:

import requests
from bs4 import BeautifulSoup

page = requests.get('https://forecast.weather.gov/MapClick.php?lat=40.940780000000075&lon=-87.45408999999995#.XZGB-XQzbIU')
soup = BeautifulSoup(page.content, 'html.parser')
print(soup)

当我运行命令时,我得到的错误是:

"C:\\Program Files\\Python37\\python.exe" c:\\Users\\alip\\.vscode\\extensions\\ms-python.python-2019.9.34911\\pythonFiles\\ptv
sd_launcher.py --default --nodebug --client --host localhost --port 57662 c:\\xampp\\htdocs\\alipscrap\\try.py
Traceback (most recent call last):
  File "C:\Users\alip\AppData\Roaming\Python\Python37\site-packages\urllib3\connectionpool.py", line 672, in urlopen
    chunked=chunked,
  File "C:\Users\alip\AppData\Roaming\Python\Python37\site-packages\urllib3\connectionpool.py", line 376, in _make_request
    self._validate_conn(conn)
  File "C:\Users\alip\AppData\Roaming\Python\Python37\site-packages\urllib3\connectionpool.py", line 994, in _validate_conn
    conn.connect()
  File "C:\Users\alip\AppData\Roaming\Python\Python37\site-packages\urllib3\connection.py", line 394, in connect
    ssl_context=context,
  File "C:\Users\alip\AppData\Roaming\Python\Python37\site-packages\urllib3\util\ssl_.py", line 370, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\Program Files\Python37\lib\ssl.py", line 423, in wrap_socket
    session=session
  File "C:\Program Files\Python37\lib\ssl.py", line 870, in _create
    self.do_handshake()
  File "C:\Program Files\Python37\lib\ssl.py", line 1139, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\alip\AppData\Roaming\Python\Python37\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "C:\Users\alip\AppData\Roaming\Python\Python37\site-packages\urllib3\connectionpool.py", line 720, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "C:\Users\alip\AppData\Roaming\Python\Python37\site-packages\urllib3\util\retry.py", line 436, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='forecast.weather.gov', port=443): Max retries exceeded with url: /MapClick.php?lat=40.940780000000075&lon=-87.45408999999995 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\alip\.vscode\extensions\ms-python.python-2019.9.34911\pythonFiles\ptvsd_launcher.py", line 43, in <module>
    main(ptvsdArgs)
  File "c:\Users\alip\.vscode\extensions\ms-python.python-2019.9.34911\pythonFiles\lib\python\ptvsd\__main__.py", line 432, in main
    run()
  File "c:\Users\alip\.vscode\extensions\ms-python.python-2019.9.34911\pythonFiles\lib\python\ptvsd\__main__.py", line 316, in run_file
    runpy.run_path(target, run_name='__main__')
  File "C:\Program Files\Python37\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Program Files\Python37\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Program Files\Python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\xampp\htdocs\alipscrap\try.py", line 4, in <module>
    page = requests.get('https://forecast.weather.gov/MapClick.php?lat=40.940780000000075&lon=-87.45408999999995#.XZGB-XQzbIU')
  File "C:\Users\alip\AppData\Roaming\Python\Python37\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\alip\AppData\Roaming\Python\Python37\site-packages\requests\api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\alip\AppData\Roaming\Python\Python37\site-packages\requests\sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\alip\AppData\Roaming\Python\Python37\site-packages\requests\sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\alip\AppData\Roaming\Python\Python37\site-packages\requests\adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='forecast.weather.gov', port=443): Max retries exceeded with url: /MapClick.php?lat=40.940780000000075&lon=-87.45408999999995 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)')))

为什么会出现此错误?

2 个答案:

答案 0 :(得分:0)

您可能没有安装基本证书。如果您使用的是Mac,请访问:

Macintosh HD > Applications > Python3.x

并运行Install Certificates.command

答案 1 :(得分:0)

我在Ubuntu上很好地运行了代码。

您可以使用chrome浏览该网站,以了解IP是否被禁止。 如果没有,您可以尝试选项verify = False:
page = requests.get('https://forecast.weather.gov/MapClick.php?lat=40.940780000000075&lon=-87.45408999999995#.XZGB-XQzbIU', verify=False)