大家好,我很努力尝试解决此unicode代码\ u200e在我的网页中显示的问题,感到沮丧/疲惫。我尝试了所有我能想到的。这是我的页面的样子,其数据是从news.google.com抓取的文章,并与时间提交一起显示在我的页面上(时间提交是\ u200e随处可见的位置)
http://i.imgur.com/lrqmvWG.jpg
我将提供我的views.py,我的article.html(图片中设置为显示所有内容的页面)和header.html(无论出于何种原因。但这是article.html的父模板)。 CSS继承)。另外,我研究并知道\ u200e是从左到右的标记,当我检查news.google.com中的来源时,它会在时间提交元素中弹出,显示为
‎
像这样:
<span class="al-attribution-timestamp">‎51 minutes ago‎</span>
我尝试编辑views.py以使用.encode(encoding ='ascii','ignore')或utf-8或iso-8859-8以及其他几行代码在Google进行深入研究,但仍对其进行编码在各处显示\ u200e。我把它放在了我的views.py的许多不同部分中,甚至在for循环之后(也就是在+之后在它作为数据存储在变量“ b”中并且恰好不会消失的情况下)之前。我该怎么办?
Views.py
def articles(request):
""" Grabs the most recent articles from the main news page """
import bs4, requests
list = []
list2 = []
url = 'https://news.google.com/'
r = requests.get(url)
sta = "‎"
try:
r.raise_for_status() == True
except ValueError:
print('Something went wrong.')
soup = bs4.BeautifulSoup(r.text, 'html.parser')
for listarticles in soup.find_all('h2', 'esc-lead-article-title'):
a = listarticles.text
list.append(a)
for articles_times in soup.find_all('span','al-attribution-timestamp'):
b = articles_times.text
list2.append(b)
list = zip(list,list2)
context = {'list':list, 'list2':list2}
return render(request, 'newz/articles.html', context)
article.html
{% extends "newz/header.html" %}
{% block content %}
<script>
.firstfont (
font-family: serif;
}
</script>
<div class ="row">
<h3 class="btn-primary">These articles are scraped from <strong>news.google.com</strong></h3><br>
<ul class="list-group">
{% for thefinallist in list %}
<div class="col-md-15">
<li class="list-group-item">{{ thefinallist }}
</li>
</div>
{% endfor %}
</div></ul>
{{ list }}
{% endblock %}
header.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Sacred Page</title>
<meta charset="utf-8" />
{% load staticfiles %}
<meta name="viewport" content = "width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static 'newz/css/bootstrap.min.css' %}" type = "text/css"/>
<style type="text/css">
html,
body {
height:100%
}
</style>
</head>
<body class="body" style="background-color:#EEEDFA">
<div class="container-fluid" style="min-height:95%; ">
<div class="row">
<div class="col-sm-2">
<br>
<center>
<img src="{% static 'newz/img/profile.jpg' %}" class="responsive-img" style='max-height:100px;' alt="face">
</center>
</div>
<div class="col-sm-10">
<br>
<center>
<h3><font color="007385">The sacred database</font></h3>
</center>
</div>
</div><hr>
<div class="row">
<div class="col-sm-2">
<br>
<br>
<!-- Great, til you resize. -->
<!--<div class="well bs-sidebar affix" id="sidebar" style="background-color:#E77200">-->
<div class="well bs-sidebar" id="sidebar" style="background-color:#E1DCF5">
<ul class="nav nav-pills nav-stacked">
<li><a href='/'>Home</a></li>
<li><a href='/newz/'>News database</a></li>
<li><a href='/blog/'>Blog</a></li>
<li><a href='/contact/'>Contact</a></li>
</ul>
</div> <!--well bs-sidebar affix-->
</div> <!--col-sm-2-->
<div class="col-sm-10">
<div class='container-fluid'>
<br><br>
<font color="#2E2C2B">
{% block content %}
{% endblock %}
{% block fool %}
{% endblock fool %}
</font>
</div>
</div>
</div>
</div>
<footer>
<div class="container-fluid" style='margin-left:15px'>
<p><a href="#" target="blank">Contact</a> | <a href="#" target="blank">LinkedIn</a> | <a href="#" target="blank">Twitter</a> | <a href="#" target="blank">Google+</a></p>
</div>
</footer>
</body>
</html>
最佳答案
如果需要,可以使用replace()
从字符串中去除字符。
b = articles_times.text.replace('\u200E', '')
在呈现的html中看到的是
\u200E
而不是‎
的原因是,模板中包含了元组{{ thefinallist }}
。这意味着Python在元组上调用repr()
,您会看到\u200E
。这也意味着您会看到括号,例如('headline' '\u200e1 hour ago')
如果分别显示元组的元素,则模板中将显示
‎
。例如,您可以这样做:{% for headline, timeago in list %}
<div class="col-md-15">
<li class="list-group-item">{{ headline }} {{ timeago }}
</li>
</div>
{% endfor %}
关于javascript - 显示\u200e代码的Django模板,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39771702/