我正在使用请求,Beautifulsoup4和django。结果工作正常,除了它们在括号中是封闭的。我如何在没有括号的情况下返回它们。继承我的代码:
我的观点 url =('http://www.vladtv.com/')
google = requests.get(url)
bs = BeautifulSoup(google.content, 'html.parser')
d = bs.title.string
a_links = bs.findAll('div', 'entry')
links = []
for link in a_links:
links.append((
link.text,
)
)
# samples = soup.find_all("a", "item-title")
myset = set(links)
context = {
"links": links,
}
return render(request, 'index.html', context)
我的模板
{% for l in links %}
{{l}}<br><hr>
{% endfor %}
我的结果
('251Drake Dethrones 50 Cent on Forbes Top Five Richest Hip-Hop Stars of 20168,787 views9 Hrs Ago',)
('175Kanye Claims Kim Kardashian Broke Boundaries for Biracial Couples5,923 views15 Hrs Ago',)
('94Stevie Wonder: If Michael Jackson is King of Pop, Prince is the Emperor4,482 views16 Hrs Ago',)
我想像这样显示它
'251Drake Dethrones 50 Cent on Forbes Top Five Richest Hip-Hop Stars of 20168,787 views9 Hrs Ago',
'175Kanye Claims Kim Kardashian Broke Boundaries for Biracial Couples5,923 views15 Hrs Ago',
'94Stevie Wonder: If Michael Jackson is King of Pop, Prince is the Emperor4,482 views16 Hrs Ago',
如何做到这一点
答案 0 :(得分:2)
那是因为你实际上是将元组附加到列表中。替换:
links = []
for link in a_links:
links.append((
link.text,
)
)
使用:
links = [link.text for link in a_links]
答案 1 :(得分:1)
或者,如果您希望输出字面上(如有问题,包括单引号和尾随逗号),则可以跳过第一个和最后一个字符以排除括号:
links = [str((link.text,))[1:-1] for link in a_links]