How To Print All The Links Of A Given Url On A Html File Using Django
I have this code in my views.py: from django.http import HttpResponse, Http404 from django.shortcuts import render_to_response from bs4 import BeautifulSoup import urllib def ex
Solution 1:
Try:
{% for final_links in all_links %}
{{ final_links.attrMap.href }}
{% endfor %}
I arrived at that from the following session:
>>>import urllib>>>from BeautifulSoup import BeautifulSoup as BS>>>start = urllib.urlopen('http://blog.tkbe.org')>>>soup = BS(start)>>>all_links = soup.findAll('a', href=True)>>>first = all_links[0]>>>first
<a href="http://blog.tkbe.org/" title="TKBE" rel="home">TKBE</a>
>>>dir(first)
[..., 'attrMap', 'attrs', ...]
>>>first.attrs
[(u'href', u'http://blog.tkbe.org/'), (u'title', u'TKBE'), (u'rel', u'home')]
>>>first.attrMap
{u'href': u'http://blog.tkbe.org/', u'rel': u'home', u'title': u'TKBE'}
If your version of BeautifulSoup has other attributes you can find them similarly.
You might have to extract them in the view though, and not in the template, e.g.:
all_links = [link.attrMap['href'] for link in all_links]
before your return statement (or whichever attribute you need to access in your version of BeautifulSoup).
Solution 2:
If all_links
is a list of dict
s each having key href
then do the following to access the value of href
in the Django template:
{% for final_links in all_links %}
{{ final_links.href }}
{% endfor %}
Post a Comment for "How To Print All The Links Of A Given Url On A Html File Using Django"