Custom indent width for BeautifulSoup .prettify()

后端 未结 4 1609
一生所求
一生所求 2020-12-15 17:29

Is there any way to define custom indent width for .prettify() function? From what I can get from it\'s source -

def prettify(self, encoding=No         


        
4条回答
  •  我在风中等你
    2020-12-15 17:45

    As far as I can tell, this feature is not built in, as there are a handful of solutions out there for this problem.

    Assuming you are using BeautifulSoup 4, here are the solutions I came up with

    Hardcode it in. This requires minimal changes, this is fine if you don't need the indent to be different in different circumstances:

    myTab = 4 # add this
    if pretty_print:
       # space = (' ' * (indent_level - 1))
        space = (' ' * (indent_level - myTab))
        #indent_contents = indent_level + 1
        indent_contents = indent_level + myTab 
    

    Another problem with the previous solution is that the text content wont be indented entirely consistently, but attractively, still. If you need a more flexible/consistent solution, you can just modify the class.

    Find the prettify function and modify it as such (it is located in the Tag class in element.py):

    #Add the myTab keyword to the functions parameters (or whatever you want to call it), set it to your preferred default.
    def prettify(self, encoding=None, formatter="minimal", myTab=2): 
        Tag.myTab= myTab # add a reference to it in the Tag class
        if encoding is None:
            return self.decode(True, formatter=formatter)
        else:
            return self.encode(encoding, True, formatter=formatter)
    

    And then scroll up to the decode method in the Tag class and make the following changes:

    if pretty_print:
        #space = (' ' * (indent_level - 1))
        space = (' ' * (indent_level - Tag.myTab))
        #indent_contents = indent_level + Tag.myTab 
        indent_contents = indent_level + Tag.myTab
    

    Then go to the decode_contents method in the Tag class and make these changes:

    #s.append(" " * (indent_level - 1))
    s.append(" " * (indent_level - Tag.myTab))
    

    Now BeautifulSoup('Text').prettify(myTab=4) will return:

    
        
            
                Text
            
        
    
    

    **No need to patch BeautifulSoup class as it inherits the Tag class. Patching Tag class is sufficient enough to achieve the goal.

提交回复
热议问题