开发者

Python/feedparser script won't display on CGI/ character coding

#!/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 -- i开发者_JS百科s not working and I m running Ubuntu server.

Thank you in advance for help.

Regards.


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
        ....


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')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜