Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.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/15.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
Python XML解析挂起?_Python_Xml_Parsing - Fatal编程技术网

Python XML解析挂起?

Python XML解析挂起?,python,xml,parsing,Python,Xml,Parsing,编辑: 我有一个脚本,它解析站点地图xml并将第一次传递存储在一个数组中。然后设置它,以便它刷新、解析并将所需的xml标记存储到另一个数组中,以检查是否存在任何差异。第二个数组在xmls刷新时每3秒不断更新一次。然而,它似乎挂断了,我想知道是什么问题 import urllib,time from time import gmtime, strftime from xml.dom import minidom url='http://kutoa.com/sitemap_products_1.xm

编辑:

我有一个脚本,它解析站点地图xml并将第一次传递存储在一个数组中。然后设置它,以便它刷新、解析并将所需的xml标记存储到另一个数组中,以检查是否存在任何差异。第二个数组在xmls刷新时每3秒不断更新一次。然而,它似乎挂断了,我想知道是什么问题

import urllib,time
from time import gmtime, strftime
from xml.dom import minidom
url='http://kutoa.com/sitemap_products_1.xml?from=1&to=999999999'
def main():
    primList=[]
    secList=[]
    xml = urllib.urlopen(url).read()
    xmldoc = minidom.parseString(xml)
    loc_values = xmldoc.getElementsByTagName('loc')
    for loc_val in loc_values:
        item=(loc_val.firstChild.nodeValue)
        primList.append(item)
    for i in primList:
        secList.append(item)
    while len(secList)==len(primList):
        print str(strftime("%Y-%m-%d %H:%M:%S", gmtime()))+' :: '+str(len(secList)) +' items indexed...'
        print 'destruct list'
        secList=[]
        print 'empty list/reading url'
        xml = urllib.urlopen(url).read()
        print 'url read/parsing'
        xmldoc = minidom.parseString(xml)
        print 'parsed going for tags'
            loc_values = xmldoc.getElementsByTagName('loc')
        print 'adding tags'
        for loc_val in loc_values:
            item=(loc_val.firstChild.nodeValue)
            secList.append(item)
        print 'tags added to list'
        time.sleep(3)
        print 'sleep for 3\n'
    if len(primList)>len(secList):
            print 'items removed'
            main()
    elif len(secList)>len(primList):
            print 'items added'
            main()
main()
使用print语句进行故障排除时,我看到它在打开url时挂起。以下是一些最新的输出:

2015-12-26 18:30:21 :: 7 items indexed...
destruct list
empty list/reading url
url read/parsing
parsed going for tags
adding tags
tags added to list
sleep for 3

2015-12-26 18:30:24 :: 7 items indexed...
destruct list
empty list/reading url
url read/parsing
parsed going for tags
adding tags
tags added to list
sleep for 3

2015-12-26 18:30:27 :: 7 items indexed...
destruct list
empty list/reading url

然后再也不会输出任何东西,我的程序将挂起,在最后一次解析输出下未终止。这个网络相关吗?如有任何想法/补救措施,将不胜感激

在函数开始时,在调用
urlopen
之前,您可能需要设置套接字超时,以防止调用可能永远挂起。此代码段将超时设置为3秒,以与睡眠值保持一致:

import socket

def main():
    socket.setdefaulttimeout(3)
    ...
然后,将调用包装到
urlopen
以捕获
socket.timeout
异常。此代码段仅打印一个字符串并继续循环:

try:
    xml = urllib.urlopen(url).read()
except socket.timeout as e:
    print 'timeout reading url: %s' % e
    continue
print 'url read/parsing'
...

我还没有测试过,所以请告诉我您的情况。

添加更多输出,以便您可以知道哪一行挂起。此外,这是一个外部网站吗?如果是这样的话,它可能有一些逻辑来避免像这样的重复请求过载。尽量每分钟只进行一次投票。@MartinBonner谢谢你的回复。通过添加一些用于故障排除的基本打印语句,我发现它在这里卡住了:xml=urllib.urlopen(url).read()对
urlopen
的调用似乎最有可能挂起,因此,您可能需要使用超时值调用
socket.setdefaulttimeout
,然后可以选择捕获
socket.timeout
异常。谢谢@cr3。我将如何着手实施这一点?另外,更改sleep方法中的轮询时间会带来不同吗?谢谢!我在这些方面没有太多的经验,但有一些像这样的异常处理,我可以继续。我尝试了第一个数组的第一次解析,并在while循环中实现了一个IOError异常。由于您在处理异常方面没有太多经验,我在第二个代码片段中进行了一次气味编辑,将异常分配给变量
e
,然后将该变量包含在打印的字符串中。您可能会发现它在解决此类问题时很有用。