美文网首页我爱编程
python爬虫popen命令行调用phantomjs执行js

python爬虫popen命令行调用phantomjs执行js

作者: comboo | 来源:发表于2017-04-13 11:05 被阅读170次

    文档地址

    popen

    这个库研究不怎么深入,因为没有很深的需求。

    class subprocess.Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=(), *, encoding=None, errors=None)
    

    args
    表示执行的shell命令。一般情况下,是列表的形式,通过python自身的类似shell功能的方法来执行,避免shell注入。

    shell

    • true shell执行,args直接为shell脚本
    • false python执行
    args = ["ls", "-l"]   #shell = false
    args =  "ls -l "       #shell = true
    

    推荐使用第一种,但是我用的是第二种。

    stdout=None
    输出,一般制定一个pipe管道

    代码

    #调用phantomjs执行js方法,并且返回结果。爬虫中可能用到
    import subprocess
    args = 'phantomjs /home/comboo/Desktop/zjjs.js 有数'
    p = subprocess.Popen(args,shell=True,stdout=subprocess.PIPE)
    keyword = p.communicate()[0].strip()
    print('\n')
    print(keyword)
    

    相关文章

      网友评论

        本文标题:python爬虫popen命令行调用phantomjs执行js

        本文链接:https://www.haomeiwen.com/subject/fofbattx.html