从flas中的python输出字符串获取部分

2024-09-29 20:22:34 发布

您现在位置:Python中文网/ 问答频道 /正文

加入这个社区。我有生成html字符串的python脚本:

from shelljob import proc
import flask
import os
import eventlet

eventlet.monkey_patch()
app = flask.Flask(__name__)

@app.route('/create')
def create():
    g = proc.Group()
    p = g.run("timeout 30s mycommand | grep -Eo 'https?://\S+'")

    def read_process():
        while g.is_pending():
            lines = g.readlines()
            for proc, line in lines:
                yield "data:" + line + "\n\n"

    return flask.Response( read_process(), mimetype= 'text/event-stream' )

@app.route('/')
def get_page():
    return flask.send_file('page.html')

if __name__ == '__main__':
    app.run(debug=True)

该代码将产生类似于:

^{pr2}$

我如何才能像这样只获取url并且只显示第一个结果:

https://www.example.com/click?nonce=1a2b3c4d

因为如果我输入“timeout 30s mycommand | grep-Eo'https?://\S+'“我可以得到准确的url。 我可以用javascript替换文本,但是我仍然得到列表结果而不是一行。这是我的html“页面.html““

<!DOCTYPE html>
<html>
<head>
    <style type="text/css">.urldb{color: #ffffff;padding: 10px;background: red;text-decoration: none;}</style>
</head>
<body>
<div id="output"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jQuery-linkify/2.1.4/linkify.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jQuery-linkify/2.1.4/linkify-string.min.js"></script>
<script>
var source = new EventSource("/create");
var options = {className: 'urldb',target: {url: '_blank'}};
source.onmessage = function(event) {
    document.getElementById("output").innerHTML += event.data.replace(/(\bhttps?:\/\/\S+)|[^]/g, '$1').linkify(options) + "<br/>";
    if (event.data.indexOf('Welcome') > -1) {
        $("#output").hide();
        window.location.href = "https://example.com";
    }
};
</script>
</body>
</html>

用这个脚本我得到的结果是:

https://www.example.com/click?nonce=1a2b3c4d
https://www.example.com/click?nonce=1a2b3c4d
https://www.example.com/click?nonce=1a2b3c4d

如果我的问题有任何问题,谢谢和抱歉。在


编辑添加更多的python或javascript错误的选项,并删除“e”以澄清。在


Tags: httpsimportcomeventappflaskexamplehtml
1条回答
网友
1楼 · 发布于 2024-09-29 20:22:34

可能是你的排字错误。将https?://e\S+更改为https?://\S+(removee),这样就可以工作了。在

对于只输出第一个匹配,可以使用-m1。有关详细信息,请参阅man page。在

相关问题 更多 >

    热门问题