如何使用python跳过存储库中不存在文件的文件?

时间:2017-03-29 03:57:58

标签: python xml

我想逐个从问题跟踪系统下载xml文件。当存储库中不存在文件时,会产生错误消息。我包含python脚本以更好地理解我的问题。

我的代码:

import urllib.request
for i in range(0,1000):
    issue_id1='DERBY-'+str(i)
    url ="https://issues.apache.org/jira/si/jira.issueviews:issue-xml/"+issue_id1+'/'+issue_id1+'.xml'
    s=urllib.request.urlopen(url)
    contents = s.read()
    file = open(issue_id1+'.xml', 'wb')
    file.write(contents)

file.close()

Stack Track:

Traceback (most recent call last):
  File "/PhP/Learning/xmldownlaod.py", line 10, in <module>
    s=urllib.request.urlopen(url)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 161, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 469, in open
    response = meth(req, response)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 579, in http_response
    'http', request, response, code, msg, hdrs)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 507, in error
    return self._call_chain(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 441, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 587, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found

2 个答案:

答案 0 :(得分:2)

问题发生在循环的第一次迭代中,url值变为:

https://issues.apache.org/jira/si/jira.issueviews:issue-xml/DERBY-0/DERBY-0.xml

这是一个“死链接”并请求它导致404 Not Found错误。

使用1

开始循环
for i in range(1, 1000):

答案 1 :(得分:2)

Python使用"try except"块进行错误处理

    $(document).ready(function() {
        var total_input = 1;
        var temp_arr = [];  

        function calculateFinal(){
            var final_val = 0;
            $('.total').each(function(){
                final_val += parseFloat($(this).val());                                    
            });
            $('#final_total').val(final_val);            
        }

        jQuery(document).ready(function($){

            temp_arr[0] = total_input;
            $('.my-form .add-box').click(function(){
                var n = $('.text-box').length + 1;
                if( 10 < n ) {
                    alert('Stop it!');
                    return false;
                }
                ++total_input;
                temp_arr.push(total_input);
                var box_html = $('<p class="text-box"> <input type="text" name="size['+total_input+']"  class="add" value="" id="box1' + total_input + '" /> <input type="text" name="qty['+total_input+']" class="add" value="" id="box2' + total_input + '" /> Sub-Total: <input name="sum['+total_input+']" class="total" value=""/> <a href="#" rel="'+total_input+'" class="remove-box">Remove</a></p>');
                jQuery('#my-form').append(box_html);
                box_html.hide();
                $('.my-form p.text-box:last').after(box_html);
                box_html.fadeIn('slow');
                return false;
            });

            $('.my-form').on('click', '.remove-box', function(){
                var this_rel = $(this).attr('rel');
                $(this).parent().css( 'background-color', '#FF6C6C' );
                $(this).parent().fadeOut("slow", function() {
                    var len = temp_arr.length;
                    for(i=0;i<len;i++){  
                        if(temp_arr[i] == this_rel){
                            temp_arr.splice(i,1);                                 
                        }
                    }
                    $(this).remove();
                    calculateFinal();
                    $('.box-number').each(function(index){
                        $(this).text( index + 1 );
                    });
                });
                $('span.total').html( $('input.add').sumValues() );

                return false;
            });
        });

        $.fn.sumValues = function(i) {
            var sum = 0; 
            this.each(function() {
                if ( $(this).parent().find('input.add') ) {
                    var obj = $(this).parent().find('input.add');
                    if(obj.val()!='' || typeof obj != "undefined"){
                        var val = 0;
                        $(obj).each(function(){
                            val = parseFloat($("#box1").val()*$("#box2").val());  

                        });
                        sum += parseFloat( ('0' + val).replace(/[^0-9-\.]/g, ''), 10 ); 
                        $('input[name="sum['+i+']"]').val(sum);                        
                    }

                }


            });


            return sum;
        };

        $(document).ready(function() {
            $(document).on('keyup','input.add', function() {
                var len = temp_arr.length;
                for(i=0;i<len;i++){  
                    $('input.total').html( $('input[name="sum['+temp_arr[i]+']"]').sumValues(temp_arr[i]) );
                    calculateFinal();
                }
            });

        });
    });
相关问题