Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是什么原因引起的;urlopen错误[Errno 13]权限被拒绝;错误?_Python_Beautifulsoup - Fatal编程技术网

Python 是什么原因引起的;urlopen错误[Errno 13]权限被拒绝;错误?

Python 是什么原因引起的;urlopen错误[Errno 13]权限被拒绝;错误?,python,beautifulsoup,Python,Beautifulsoup,我正在尝试在Centos7服务器上编写python(版本2.7.5)CGI脚本。 我的脚本试图从librivox的网页下载数据,如https://librivox.org/selections-from-battle-pieces-and-aspects-of-the-war-by-herman-melville/而我的脚本因以下错误而崩溃: <class 'urllib2.URLError'>: <urlopen error [Errno 13] Permission den

我正在尝试在Centos7服务器上编写python(版本2.7.5)CGI脚本。 我的脚本试图从librivox的网页下载数据,如<代码>https://librivox.org/selections-from-battle-pieces-and-aspects-of-the-war-by-herman-melville/而我的脚本因以下错误而崩溃:

<class 'urllib2.URLError'>: <urlopen error [Errno 13] Permission denied> 
      args = (error(13, 'Permission denied'),) 
      errno = None 
      filename = None 
      message = '' 
      reason = error(13, 'Permission denied') 
      strerror = None
更新:感谢Paul和alecxe,我已将代码更新为:

def output_html ( url, appname, doobb ):
        #hdr = {'User-Agent':'Mozilla/5.0'}
        #print "url is %s<br>" % url
        #req = url2lib2.Request(url, headers=hdr)
        # soup = BeautifulSoup(urllib2.urlopen( url ).read())
        headers = {'User-Agent':'Mozilla/5.0'}
        # headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36'}
        response = requests.get( url, headers=headers)

        soup = BeautifulSoup(response.content)
。。。被称为

<class 'requests.exceptions.ConnectionError'>: ('Connection aborted.', error(13, 'Permission denied')) 
      args = (ProtocolError('Connection aborted.', error(13, 'Permission denied')),) 
      errno = None 
      filename = None 
      message = ProtocolError('Connection aborted.', error(13, 'Permission denied')) 
      request = <PreparedRequest [GET]> 
      response = None 
      strerror = None
很奇怪,你不觉得吗

更新: 这个问题可能已经有了答案: urllib2.HTTPError:HTTP错误403:禁止2个答案

不,他们不回答问题

使用并提供
用户代理
标题对我有效:

from bs4 import BeautifulSoup
import requests

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36'}
response = requests.get("https://librivox.org/selections-from-battle-pieces-and-aspects-of-the-war-by-herman-melville/", headers=headers)

soup = BeautifulSoup(response.content)
print soup.title.text  # "prints LibriVox"

终于明白了

# grep python /var/log/audit/audit.log | audit2allow -M mypol
# semodule -i mypol.pp

我们的一台机器也有同样的问题。我们没有创建SELinux模块(如上面的答案中所列),而是对SELinux布尔值进行了以下更改,以防止发生类似错误

#设置电子书httpd_can_network_connect on

正如centos wiki上所解释的那样


httpd\u can\u network\u connect(httpd服务)::允许httpd脚本和模块连接到网络。

是否尝试向请求添加其他头?比如说谢谢,但这只是给了我一个不同版本的错误。这让我走上了正确的道路,帮了我很多。谢谢CentOS 7上的SELinux阻止了来自.py文件的Python调用urllib/urllib2/requests,但没有阻止来自Python命令行的调用,并且错误消息没有帮助。它快把我逼疯了。
def output_html ( url ):
        soup = BeautifulSoup(urllib2.urlopen( url ).read())
from bs4 import BeautifulSoup
import requests

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36'}
response = requests.get("https://librivox.org/selections-from-battle-pieces-and-aspects-of-the-war-by-herman-melville/", headers=headers)

soup = BeautifulSoup(response.content)
print soup.title.text  # "prints LibriVox"
# grep python /var/log/audit/audit.log | audit2allow -M mypol
# semodule -i mypol.pp