views:

44

answers:

2
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import cgi
import string
import feedparser

count = 0
print "Content-Type: text/html\n\n"
print """<PRE><B>WORK MAINTENANCE/B></PRE>"""


d = feedparser.parse("http://www.hep.hr/ods/rss/radovi.aspx?dp=zagreb")


for opis in d:
    try:
          print """<B>Place/Time:</B> %s<br>""" % d.entries[count].title
          print """<B>Streets:</B> %s<br>""" % d.entries[count].description
          print """<B>Published:</B> %s<br>""" % d.entries[count].date
          print "<br>"
          count+= 1
    except:
        pass

I have a problem with CGI and paython script. Under the terminal script runs just fine except "IndexError: list index out of range", and I put pass for that. But when I run script through CGI I only get WORK MAINTENANCE line and first line from d.entries[count].title repeated 9 times? So confusing...

Also how can I setup support in feedparser for Croation(balkan) letters; č,ć,š,ž,đ ? # -- coding: utf-8 -- is not working and I m running Ubuntu server.

Thank you in advance for help.

Regards.

A: 
for opis in d:
    try:
          print """<B>Place/Time:</B> %s<br>""" % d.entries[count].title

You're not using 'opis' in your output.

Try something like this:

for entry in d.entries:
    try:
        print """<B>Place/Time:</B> %s<br>""" % entry.title
        ....
sarnold
I have put as you sad, but still it doesn't work, script works fine alone (without errors with your modification:))but when used through CGI it displays only first line?Any suggestion?
Oke I think I found the problem, CGI can't print my croatian letters ć,č,š,ž,đ, and that is the reason why script breaks.How can set up support for those letters in python/ubuntu?
I think the issue is: `# -*- coding: utf-8 -*-` only lets you use UTF-8 inside your program source code: http://evanjones.ca/python-utf8.html If you want to _output_ UTF-8 as well, you need to tell the web browser to expect UTF-8, perhaps using a `<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />` tag. (_Maybe_ you can just add `Charset: UTF-8` header alongside your existing `Content-Type: text/html` header. I'm not sure.)
sarnold
Thank you sarnold, that worked.print "Content-Type: text/html; charset=utf-8\n\n"
Great! Glad to hear it. Thanks for the report.
sarnold
A: 

Oke had another problem, text that I manualy entered would show on CGI but RSS web pages wouldnt. So you need to encode before you write:

# -*- coding: utf-8 -*-
import sys, os, string
import cgi
import feedparser
import codecs

d = blablablabla

print "Content-Type: text/html; charset=utf-8\n\n"
print

for entry in d.entries:
    print """%s""" % entry.title.encode('utf-8')