Python/feedparser脚本在CGI上无法显示/字符编码问题
#!/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
我在使用CGI和Python脚本时遇到了问题。在终端下运行脚本一切正常,除了出现了“IndexError: list index out of range”的错误,我对此做了处理。但是当我通过CGI运行脚本时,只能看到“WORK MAINTENANCE”这一行,以及d.entries[count].title的第一行重复了9次?这让我很困惑……
另外,我该如何在feedparser中设置对克罗地亚(巴尔干)字母的支持,比如:č、ć、š、ž、đ? # -- coding: utf-8 -- 这个设置没有效果,我是在运行Ubuntu服务器。
提前感谢你的帮助。
祝好。
相关问题:
- 如何正确编写一个接收JSON数据的Python CGI脚本?
- 在Python中从CSV文件获取并显示UTF-8内容
- 将Unicode转换为UTF8以处理CSV文件 - Python使用xlrd
- 为什么这个Python脚本只读取最后一个RSS帖子到文件中?
- 这是EBCDIC (CP500)到Latin-1转换的合理方法吗?
- 将多个HTML文件抓取到CSV
- 在Python中使用表达式填充多维numpy数组的列表时出现IndexError
- 运行时警告:在类型转换中遇到无效值 quantized_data = ((data - min_value) / quantization_step).astype(int)
2 个回答
0
Oke遇到了另一个问题,就是他手动输入的文本在CGI上能显示,但在RSS网页上却不行。所以在写之前,你需要进行编码:
# -*- 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
for opis in d: try: print """<B>Place/Time:</B> %s<br>""" % d.entries[count].title
你在输出中没有使用'opis'。
试试这样做:
for entry in d.entries:
try:
print """<B>Place/Time:</B> %s<br>""" % entry.title
....