Python CGI 脚本(使用XML和mindom)不打印 FOR 循环计数

2024-05-19 12:51:18 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试使用API打印Yahoo搜索的结果时,For循环计数器将不打印其递增的值。XML被解析并打印,但计数器会反复打印“1”。在

同样的代码对于bingapi来说也可以。在

for counter1 in range(50):
    try:
        for Result in YahooSearchResultsXML.getElementsByTagName('Result'):
            try:
                Yahoo_PageTitle = Result.getElementsByTagName('Title')[counter1].firstChild.toxml(encoding="utf-8")
            except AttributeError:
                Yahoo_PageTitle = "Sorry, no page title provided..." 
            try:
                Yahoo_PageDesc = Result.getElementsByTagName('Summary')[counter1].firstChild.toxml(encoding="utf-8")
            except AttributeError:
                Yahoo_PageDesc = "Sorry, no page description provided..."
            Yahoo_DisplayURL = Result.getElementsByTagName('DisplayUrl')[counter1].firstChild.toxml(encoding="utf-8")
            Yahoo_URL = Result.getElementsByTagName('ClickUrl')[counter1].firstChild.toxml(encoding="utf-8")
            ##  Print the output to ensure it's working.
            print counter1+1
            print "<br />"
            print "<h2>" + Yahoo_PageTitle + "</h2>"
            print Yahoo_PageDesc + "<br />"
            print Yahoo_DisplayURL + "<br />"
            print Yahoo_URL + "<br />"
            print "<p> ----------------------------------------------------------------------------------------------------------------- </p>"
    except IndexError:
        print "Exiting@IndexError handler"
        break
    Yahoo_Score = counter1 + 1

感谢您的建议,我看这段代码的时间太长了。在


Tags: br计数器resultyahooutfencodingprinttry
2条回答

如上所述,打印在for Result in..循环中。如果您有多个结果,您将得到具有相同内容的多行。在

另外,try不应该缩进?(已修复)

编辑:根据我们收集到的信息,你需要在内部循环中增加这个计数器。您编写它的方式(for counter1 in range(50))将在外循环中递增它。在

试试这个(注意标识的变化):

    counter1 = 0
    try:
        for Result in YahooSearchResultsXML.getElementsByTagName('Result'):
            try:
                Yahoo_PageTitle = Result.getElementsByTagName('Title')[counter1].firstChild.toxml(encoding="utf-8")
            except AttributeError:
                Yahoo_PageTitle = "Sorry, no page title provided..." 
            try:
                Yahoo_PageDesc = Result.getElementsByTagName('Summary')[counter1].firstChild.toxml(encoding="utf-8")
            except AttributeError:
                Yahoo_PageDesc = "Sorry, no page description provided..."
            Yahoo_DisplayURL = Result.getElementsByTagName('DisplayUrl')[counter1].firstChild.toxml(encoding="utf-8")
            Yahoo_URL = Result.getElementsByTagName('ClickUrl')[counter1].firstChild.toxml(encoding="utf-8")
            ##  Print the output to ensure it's working.
            print counter1+1
            counter1 += 1
            print "<br />"
            print "<h2>" + Yahoo_PageTitle + "</h2>"
            print Yahoo_PageDesc + "<br />"
            print Yahoo_DisplayURL + "<br />"
            print Yahoo_URL + "<br />"
            print "<p>                                                         - </p>"
    except IndexError:
        print "Exiting@IndexError handler"
        break
    Yahoo_Score = counter1 + 1

您确定每个Result中有超过1Title/Summary/DisplayUrl/ClickUrl?当counter的值达到1时,如果XML中的某个地方没有第二个Title/etc元素,您将跳转到IndexError处理程序并跳出循环。在

相关问题 更多 >