Python-如何解析在线PDF文件,即使URL没有 .pdf 扩展名。
Python-如何解析在线PDF文件,即使URL没有 .pdf 扩展名。
我得到了以下错误:
Exception in thread Thread-3: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner self.run() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 763, in run self.__target(*self.__args, **self.__kwargs) File "/Users/Matthew/Desktop/Skypebot 2.0/bot.py", line 271, in process info = urllib2.urlopen(req).read() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen return opener.open(url, data, timeout) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 431, in open response = self._open(req, data) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 449, in _open '_open', req) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain result = func(*args) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1240, in https_open context=self._context) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1197, in do_open raise URLError(err) URLError:
引起此错误的代码如下:
if input.startswith("!web"): input = input.replace("!web ", "") url = "https://domainsearch.p.mashape.com/index.php?name=" + input req = urllib2.Request(url, headers={ 'X-Mashape-Key': 'XXXXXXXXXXXXXXXXXXXX' }) info = urllib2.urlopen(req).read() Message.Chat.SendMessage ("" + info)
我使用的API要求我使用HTTPS。我该如何绕过验证?
Python中解析在线PDF文件时,URL没有.pdf扩展名的问题可能出现的原因是安装的Python版本不兼容或缺少必要的证书。解决方法可以尝试安装缺少的证书或使用命令行来解决问题。
在MacOS Sierra上的Python 3.6.1中,可以通过在bash终端中输入以下命令来解决问题:
pip install certifi /Applications/Python\ 3.6/Install\ Certificates.command
如果权限被拒绝,可以尝试使用以下命令:
sudo /Applications/Python\ 3.6/Install\ Certificates.command
如果命令行操作让你感到困惑,也可以找到Python安装时创建的Applications文件夹(或其他操作系统上的等效文件夹),然后通过双击打开脚本来解决问题。文件夹的具体名称取决于安装的Python版本。
据反馈,这种方法在Python 3.7.3上也适用,并且在其他版本上也能成功解决问题。