Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.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_Django_Api_Twitter_Hyperlink - Fatal编程技术网

Python 使链接在检索到的推文中可点击

Python 使链接在检索到的推文中可点击,python,django,api,twitter,hyperlink,Python,Django,Api,Twitter,Hyperlink,在views.py中,我从一个特定的用户那里检索tweet,然后在模板中显示出来,这是有效的 然而,它只是原始文本,即链接不可点击,问题是什么是使其可点击的最佳方法 注1:我指的链接可能是任何链接,但最有可能是Instagram链接 注2:如果可能的话,我甚至希望标签是可点击的 视图中的代码.py user = twitter.User tweets = [] statuses = t.GetUserTimeline(user) for s in statuses:

在views.py中,我从一个特定的用户那里检索tweet,然后在模板中显示出来,这是有效的

然而,它只是原始文本,即链接不可点击,问题是什么是使其可点击的最佳方法

注1:我指的链接可能是任何链接,但最有可能是Instagram链接

注2:如果可能的话,我甚至希望标签是可点击的

视图中的代码.py

user = twitter.User
    tweets = []
    statuses = t.GetUserTimeline(user)

    for s in statuses:
        tweets.append(s.text)
html:

<div class="col2">
    <ol class="ol_list">
        <h4>Twitter</h4>
        {% for tweet in tweets %}
        <li>
            <p>{{tweet}}</p>
        </li>
        {% endfor %}
    </ol>
</div>

啁啾
{tweets%中的tweet为%1}
  • {{tweet}

  • {%endfor%}
    您的问题不太清楚您指的是哪个链接

    如果链接在tweet中,如tweet中所示:

    你应该访问这个网站:example.com

    然后,您很可能希望使用正则表达式来识别链接,然后在传递到模板之前将HTML拼接到tweet本身中

    转动此按钮:
    您应该访问此网站:example.com

    进入此网站:
    您应该访问此网站:

    哈希标记也可以用同样的方式完成

    转这个:
    沿着这条街走#约洛


    走进这个:
    沿着街道走

    我使用这样的代码来做类似的事情:

    def linkify(raw_message):
        message = raw_message
        for url in url_regex.findall(raw_message):
            if url.endswith('.'):
                url = url[:-1]
            if 'http://' not in url:
                href = 'http://' + url
            else:
                href = url
            message = message.replace(url, '<a href="%s">%s</a>' % (href, url))
    
        return message
    
    这个正则表达式更喜欢误报,而不是遗漏一些边缘。它还匹配尾随的
    。不过,我只是稍后删除它。哈希标记将需要另一个正则表达式来匹配它们

    比如:

    hashtag_re = re.compile(r"""
           \#                # a hashmark
           [^\s]*            # not whitespace repeated""", re.VERBOSE)
    

    s.text中有HTML吗?还是只是文字?@RobL:只是原始文字。。。
    hashtag_re = re.compile(r"""
           \#                # a hashmark
           [^\s]*            # not whitespace repeated""", re.VERBOSE)