Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.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在终端执行命令时遇到问题_Python - Fatal编程技术网

从python在终端执行命令时遇到问题

从python在终端执行命令时遇到问题,python,Python,我有以下命令 /usr/bin/node_modules/phantomjs/bin/phantomjs RequestURL.jshttps://www.ubank.com.au/ >html,它在终端可以很好地执行,但不能从python中执行,如下所示 def get_generated_html(self, url, has_headers): """ Method: Method to get the generated HTML content from

我有以下命令

/usr/bin/node_modules/phantomjs/bin/phantomjs RequestURL.jshttps://www.ubank.com.au/ >html
,它在终端可以很好地执行,但不能从python中执行,如下所示

def get_generated_html(self, url, has_headers):
        """
        Method: Method to get the generated HTML content from Phantomas.

        Args: Takes the url as an argument for which to get the HTML content.
              hasHeaders defaulted to false for no headers.

        Returns: Nothing.
        """
        if not urlparse(url).scheme:
            url = 'http://'+url
        if has_headers == False:
            command = PAGE_SOURCE_CMD % url
            utils.execute_command(command).communicate()
        else:
            command = FEO_PAGE_SOURCE_CMD % url
            print command
            utils.execute_command(command).communicate()
def start_parser(self, analysis_id, url, hasHeaders=False):
        """
        Method: Method to start the parser.

        Args: Analsyis ID and URL as an argument.

        Returns: Nothing.
        """

        feed = None
        path = self.create_analysis_folder(analysis_id, hasHeaders) 
        self.get_generated_html(url, hasHeaders)
        for root, dirs, files in os.walk(path):
            for file in files:
                if file.endswith('.html'):
                    feed = BeautifulSoup(open(path + '/' +file).read())
                    if hasHeaders:
                        os.chdir('..')
                    print "deleting"
                    shutil.rmtree(os.getcwd())
            break
        return feed
print语句打印出确切的命令

下面是execute_command()方法

我调用生成的html,如下所示

def get_generated_html(self, url, has_headers):
        """
        Method: Method to get the generated HTML content from Phantomas.

        Args: Takes the url as an argument for which to get the HTML content.
              hasHeaders defaulted to false for no headers.

        Returns: Nothing.
        """
        if not urlparse(url).scheme:
            url = 'http://'+url
        if has_headers == False:
            command = PAGE_SOURCE_CMD % url
            utils.execute_command(command).communicate()
        else:
            command = FEO_PAGE_SOURCE_CMD % url
            print command
            utils.execute_command(command).communicate()
def start_parser(self, analysis_id, url, hasHeaders=False):
        """
        Method: Method to start the parser.

        Args: Analsyis ID and URL as an argument.

        Returns: Nothing.
        """

        feed = None
        path = self.create_analysis_folder(analysis_id, hasHeaders) 
        self.get_generated_html(url, hasHeaders)
        for root, dirs, files in os.walk(path):
            for file in files:
                if file.endswith('.html'):
                    feed = BeautifulSoup(open(path + '/' +file).read())
                    if hasHeaders:
                        os.chdir('..')
                    print "deleting"
                    shutil.rmtree(os.getcwd())
            break
        return feed

此处返回的提要不是页面源,因为它是从命令行返回的。

我不确定
get\u generated\u html
应该做什么,但它不返回任何内容,在其中一种情况下,它只
print
。我被docstring搞糊涂了,因为它说函数不应该返回任何内容,但它也没有说任何关于输出的内容。“默认值为false”部分在当前代码中不正确

另外,返回一个元组,因此您将只返回返回值的一个元素(可能)

尝试以下方法:

output = utils.execute_command(command).communicate()
return output[0]
如果我们正在进行代码审查,这对我来说更优雅:

if not has_headers:
    command = PAGE_SOURCE_CMD % url
else:
    command = FEO_PAGE_SOURCE_CMD % url
output = utils.execute_command(command).communicate()
return output[0]  # or print it?

也许
PAGE\u SOURCE\u CMD
有问题,或者调用
get\u generated\u html
?“我看不出有什么明显的错误,所以也许有更多的背景?”在问题中补充道,“关心”对否决票发表评论?我想我回答了这个问题(“有帮助吗?”)。从coamnd行执行命令将打印html源代码。从脚本内部它不会打印任何内容,所以我关于不打印或返回任何内容的评论可能有用吗?