2011-07-18 27 views
2

尝试使用API​​打印雅虎搜索的结果,For循环计数器将不打印其递增的值。 XML被解析并打印,但计数器一遍又一遍地打印“1”。Python CGI脚本(使用XML和Mindom)不打印FOR循环计数器

相同的代码适用于必应API。

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 "[email protected] handler" 
     break 
    Yahoo_Score = counter1 + 1 

建议感激,现在看这段代码太长了。

+0

为什么你认为柜台的价值应该改变? –

+0

你的前两行包含一个IndentationError。 – ThiefMaster

+0

@ThiefMaster:我修好了缩进。它正在等待同行评审。 –

回答

0

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

另外,try不应缩进?(这是固定的)

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

试试这个(注意identation变化):

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 "[email protected] handler" 
     break 
    Yahoo_Score = counter1 + 1 
+0

缩进打印语句导致循环仅迭代一次,打印一个搜索结果。然后,由于IndexError处理程序,它突破了循环。 – user725236

+0

将'print'添加到每个'except'块,更新代码并输出,请 –

+0

它是否适用于我的更改? –

0

你确定你有超过1 Title/Summary/DisplayUrl /每ResultClickUrl?当计数器的值达到1时,如果在XML中没有第二个/ etc元素,那么您将跳转到IndexError处理程序并跳出循环。

+0

每个Result标签都包含一个Title/Summary/DisplayURL/ClickURL标签集。 – user725236

+0

@ user725236:您的AttributeError异常处理程序看起来很可疑,但是:如果没有提供页面标题,我会忽略getElementsByTagName('Title')以返回一个空列表,因此您将得到一个IndexError而不是一个AttributeError。您应该在IndexError处理程序中添加一条打印语句,以检查您是否未意外退出。 –

+0

@ gurney alex:正如我所说的,它适用于大型API,如果没有页面标题,它会显示“对不起,没有提供页面标题...”。我会尝试按照您的建议插入打印语句。 试过,如预期的那样,由于IndexError退出,完全遍历XML。 – user725236