1

I need to download the headlines from BBC and using Ajax and jquery in Django. Im currently trying to use Urllib3, to create a request to get the RSS/XML Top News data from the BBC website found at the following:

'http://feeds.bbci.co.uk/news/rss.xml'

I have created the request I believe, but when the returned object pass it back to my HTML/Javascript it doesn't work and I get the following error.

the object I pass through the window.onload method is: upon_success({{ AllNews }}) url object

and gives me the error 'Uncaught SyntaxError: Unexpected token &'

My HTML and Views.py:

from django.shortcuts import render
import urllib3
import urllib3.request
import json


def index(request):
    http = urllib3.PoolManager()
    r = http.request('GET', 'http://feeds.bbci.co.uk/news/rss.xml')
    xml_news = r.data
    context = {'AllNews': xml_news}
    return render(request, 'home/NewsHome.html', context)
<!DOCTYPE html>
<html>
	<head>
        <meta charset="utf-8">
		<script type="application/javascript">
            function upon_success (xml) {
                alert('ok');
                xml.find('item').each(function(){
                    var title = $(this).find('title').text();
                    msg =+ "<li> " + title +  " </li>"
                        $("#AllNews ul").append(msg)
                }
                )};
            {% if AllNews %}
                window.onload = upon_success({{ AllNews | safe  }});
            {% endif %}
        </script>
	</head>
	<body>
		<h1>Top News: BBC versus CNN</h1>
           <ul id="AllNews"></ul>
	</body>
</html>

i don't understand how to pass the response object back to the Javascript so that i can try to extract the News titles! any information or advice would be much appreciated !

1 Answer 1

1

request method won't return just plain text response, but python object containing response, status code, headers and some other data. It can't be passed directly to JavaScript.

If you want to pass just plain text response, change:

    xml_news = r

to:

    xml_news = r.data

It will contain plain XML response from RSS URL. You can parse it either in Python (and pass to javascript only meaningful values, in JSON format) or just pass it to javascript as is, handling it's parsing on javascript side.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your feedback, I added that to my code, from the XML data, I want to extract the data between the title tags(which are stored in item tags for each story). do you have an idea how I could go about doing this?
but still when i pass it back to the Javscript it comes up i get an error saying "Uncaught SyntaxError: Unexpected token &"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.