Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pythoncgi脚本(使用XML和mindom)返回意外的结果_Python_Xml_Cgi_Minidom_Blekko - Fatal编程技术网

pythoncgi脚本(使用XML和mindom)返回意外的结果

pythoncgi脚本(使用XML和mindom)返回意外的结果,python,xml,cgi,minidom,blekko,Python,Xml,Cgi,Minidom,Blekko,我试图解析搜索引擎API(Bing、Yahoo和Blekko)返回的XML。从Blekko返回的XML(用于示例搜索查询“sushi”)的形式如下: <rss version="2.0"> <channel> <title>blekko | rss for &quot;sushi/rss /ps=100&quot;</title> <link>http://blekko.com/?q=sushi%2Frs

我试图解析搜索引擎API(Bing、Yahoo和Blekko)返回的XML。从Blekko返回的XML(用于示例搜索查询“sushi”)的形式如下:

<rss version="2.0">
<channel>
    <title>blekko | rss for &quot;sushi/rss /ps=100&quot;</title>
    <link>http://blekko.com/?q=sushi%2Frss+%2Fps%3D100</link>
    <description>Blekko search for &quot;sushi/rss /ps=100&quot;</description>
    <language>en-us</language>
    <copyright>Copyright 2011 Blekko, Inc.</copyright>
    <docs>http://cyber.law.harvard.edu/rss/rss.html</docs>
    <webMaster>webmaster@blekko.com</webMaster>
    <rescount>3M</rescount>
    <item>
        <title>Sushi - Wikipedia</title>
        <link>http://en.wikipedia.org/wiki/Sushi</link>
        <guid>http://en.wikipedia.org/wiki/Sushi</guid>
        <description>Article about sushi, a food made of vinegared rice combined with various toppings or fillings.  Sushi ( &#x3059;&#x3057;&#x3001;&#x5bff;&#x53f8;, &#x9ba8;, &#x9b93;, &#x5bff;&#x6597;, &#x5bff;&#x3057;, &#x58fd;&#x53f8;.</description>
        </item>
</channel>
</rss>

blekko | rss代表“寿司/rss/ps=100”
http://blekko.com/?q=sushi%2Frss+%2Fps%3D100
Blekko搜索“sushi/rss/ps=100”
美国英语
版权所有2011 Blekko,Inc。
http://cyber.law.harvard.edu/rss/rss.html
webmaster@blekko.com
3米
寿司-维基百科
http://en.wikipedia.org/wiki/Sushi
http://en.wikipedia.org/wiki/Sushi
关于寿司的文章,寿司是一种由醋米饭和各种配料或馅料混合而成的食物。寿司(す;し;、;寿;司;鮨;鮓;寿;斗;&#bfx5f;し;壽;。
提取所需搜索结果数据的python代码部分如下:

for counter in range(100):
    try:
        for item in BlekkoSearchResultsXML.getElementsByTagName('item'):
            Blekko_PageTitle = item.getElementsByTagName('title')[counter].toxml(encoding="utf-8")
            Blekko_PageDesc = item.getElementsByTagName('description')[counter].toxml(encoding="utf-8")
            Blekko_DisplayURL = item.getElementsByTagName('guid')[counter].toxml(encoding="utf-8")
            Blekko_URL = item.getElementsByTagName('link')[counter].toxml(encoding="utf-8")
            print "<h2>" + Blekko_PageTitle + "</h2><br />"
            print Blekko_PageDesc + "<br />"
            print Blekko_DisplayURL + "<br />"
            print Blekko_URL + "<br />"
    except IndexError:
        break
适用于范围(100)内的计数器:
尝试:
对于BlekkoSearchResultsXML.getElementsByTagName('item')中的项:
Blekko_PageTitle=item.getElementsByTagName('title')[计数器].toxml(encoding=“utf-8”)
Blekko_PageDesc=item.getElementsByTagName('description')[计数器].toxml(encoding=“utf-8”)
Blekko_DisplayURL=item.getElementsByTagName('guid')[计数器].toxml(encoding=“utf-8”)
Blekko_URL=item.getElementsByTagName('link')[计数器].toxml(encoding=“utf-8”)
打印“+Blekko_页面标题+”
打印Blekko_PageDesc+“
” 打印Blekko_DisplayURL+“
” 打印Blekko_URL+“
” 除索引器外: 打破
代码不会提取返回的每个搜索结果的页面标题,但会提取其余信息

此外,如果我没有代码:

print "<title>Page title to appear on browser tab</title>"
打印“要显示在浏览器选项卡上的页面标题”
在脚本中的某个地方,第一个搜索结果的标题作为页面标题(即,页面在浏览器中显示标题“Sushi-Wikipedia”)。如果我确实有页面标题,代码仍然不会从搜索结果中提取页面标题

相同的代码(具有不同的标记名等)在Yahoo search API中也存在同样的问题,但在Bing search API中效果良好。

我猜.toxml()方法会返回元素的XML,包括其分隔标记,然后您会得到如下结果:

<h2><title>...</title></h2><br />
<description>...</description><br />
<guid>...</guid><br />




因此,
title
元素被解释为页面的标题,除非您事先指定了自己的标题。浏览器不知道其他元素,它只是按原样显示它们的内容。

toxml()方法将xml作为字符串返回,不带标记,因此当表示调用.toxml()的节点的变量打印出来后,您就可以得到标签中包含的内容。Sushi-Wikipedia
关于sus的文章………
en.Wikipedia.org/wiki/Sushi en.Wikipedia.org/wiki/Sushi这正是我所说的。在我的测试中,
toxml
也会返回分隔符标签。尝试使用
firstChild.toxml()
而不是普通的
toxml()
,因为它应该为您提供元素内部的文本节点。这非常好,它完全去除了生成的html中的XML标记。非常感谢。