python3.8爬⾍_pytho爬⾍(1)-初体验Mac安装Python3与简
单爬⾍代码
python request本⼈使⽤的是Mac系统 ,Mac本⾝⾃带Python2.7,所以本⼈在通过brew去进⾏安装的过程中出现了问题,最后选择了⽐较简单的从官⽹直接下载后进⾏安装并且修改默认的python运⾏环境的⽅式。
ca是常量元素还是微量元素nullexception安装完成后修改默认运⾏版本:
#查python3安装路径
brew info python3
#修改 Mac 系统配置⽂件
vi ~/.bash_profile
#添加配置信息
alias python="/usr/local/bin/python3"
#编译系统配置⽂件
source ~/.bash_profile
#系统当前的python版本。
python -V
如此便完成了python环境的准备,接下来我们进⾏代码编程。
接下来我们可以进⾏编码了,可以在应⽤程序中到“IDEL”引⽤打开,或者创建⽂本后将⽂件修改成py后缀。
写如下代码:
我将⽂件保存到到了/Users/zhengbozheng/Documents/python  下接下来通过命令符进⾏操作,进⼊到当前⽬录下执⾏该代码:python ⽂件夹名称.py 进⾏执⾏。
会发现出现如下的错误:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 1319, i
n do_open
<_method(), req.selector, req.data, headers,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1230, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1276, in _send_request
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1225, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1004, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 944, in send
self.sock = self._context.wrap_socket(self.sock,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1040, in _create
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "python-pc1-fb.py", line 9, in
response = quest.urlopen(url)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 525, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 542, in _open
oc元素随机生成器result = self._call_chain(self.handle_open, protocol, protocol +
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 502, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 1362, in https_open
return self.do_open(http.client.HTTPSConnection, req,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 1322, in do_open
raise URLError(err)
这个错误是ssl证书验证导致的,我们需要取消证书验证即可:
执⾏⽂件出现了如下错误:
count统计出来为啥是零0
Traceback (most recent call last):
python安装教程macFile "python-pc1-fb.py", line 14, in
response = quest.urlopen(url)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 640, in http_response response = (
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 569, in error
return self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 502, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 649, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp)
看报错是到⽹站403通讯出现了问题,这个可能是因为⽹站做了反爬⾍处理,查看了官⽹对代码进⾏了调整。
代码修改如下:
再次执⾏后就能获取到你想要的信息了。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。