Python/feedparser 脚本不会在 CGI/字符编码上显示

发布于 2024-09-13 07:00:34 字数 1006 浏览 3 评论 0原文

#!/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 和 paython 脚本有问题。在终端下,脚本运行得很好,除了“IndexError:列表索引超出范围”,我对此设置了pass。但是当我通过 CGI 运行脚本时,我只得到 WORK MAINTENANCE 行和 d.entries[count].title 中的第一行重复 9 次?太令人困惑了...

另外,我如何在 feedparser 中设置对 Croation(balkan) 字母的支持; č,ć,š,ž,đ ? # -- 编码:utf-8 -- 不起作用,我正在运行 Ubuntu 服务器。

预先感谢您的帮助。

问候。

#!/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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听,心雨的声音 2024-09-20 07:00:34
对于 d 中的 opis:
    尝试:
          print """地点/时间: %s
""" % d.entries[count].title

您没有在输出中使用“opis”。

尝试这样的事情:

for entry in d.entries:
    try:
        print """<B>Place/Time:</B> %s<br>""" % entry.title
        ....
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
        ....
寻找一个思念的角度 2024-09-20 07:00:34

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

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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文