[英]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. 我对CGI和paython脚本有疑问。 Under the terminal script runs just fine except "IndexError: list index out of range", and I put pass for that. 在终端脚本下运行得很好,除了“ IndexError:列表索引超出范围”外,我为此设置了传递。 But when I run script through CGI I only get WORK MAINTENANCE line and first line from d.entries[count].title repeated 9 times? 但是,当我通过CGI运行脚本时,我只能从d.entries [count] .title中获得WORK MAINTENANCE行和第一行,重复9次吗? So confusing... 太令人困惑了...
Also how can I setup support in feedparser for Croation(balkan) letters; 另外我如何在feedparser中设置对Croation(巴尔干)字母的支持; č,ć,š,ž,đ ? č,ć,š,ž,đ? # - - coding: utf-8 - - is not working and I m running Ubuntu server. #-- 编码:utf-8--无法正常运行,我正在运行Ubuntu服务器。
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. 您没有在输出中使用“ opis”。
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. Oke还有另一个问题,我手动输入的文本会显示在CGI上,但RSS网页却不会。 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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.