Python анализ файла, отличного от HTML, с использованием BeaustifulSoup для добавления уникального атрибута - PullRequest
0 голосов
/ 10 июля 2020

Я пытаюсь добавить новый атрибут (filename + uniqueNo) к моим HTML узлам в файле (не html file), используя Beautifulsoup:

    for root, dirs, files in os.walk(yourpath, topdown=False):
        for name in files:
            with open(os.path.join(root, name), 'r+') as read_file:
                    html_source = read_file.read()
    
                soup = html_parser(html_source)
                print(soup)  
'''when loging soup my button has changed as below,what are those   '="" +="" =="" ? why junk chracters inserted?:
                '''
                               
                                <button '="" +="" =="" classname="{'common-style" common-normal="" onclick="{()" this.checkenable('store')}="" style-element-240x44=""> this.applyStorageType('store')}
                                &gt;
                                    {this.props.gblMessages.name}
                                </button>
                '''
****
    
                html_tags = soup.find_all(['div', 'p', 'span', 'ul', 'li','button'])
                
                for each_tag in html_tags:
                    #print(each_tag)
                    each_tag.attrs['data-myid'] = 'name'+ str(count)
                    count=count+1
    
                 with open(os.path.join(root, name), 'w') as save_file:
                    save_file.write(str(soup))

До и после должно быть, как показано ниже . Могу ли я здесь не использовать BS4? есть ли лучший способ сделать это? Как я могу этого добиться?

                <div className='style-sub-content style-pt-12'>
                    <div className='style-inline-block style-text-left style-mb-8'>
                        <div className='style-text-center style-mr-8'>
                            <button className={'common-style common-normal style-element-240x44 ' + this.checkEnable('store')}
                                onClick={() => this.applyStorageType('store')}
                            >
                                {this.props.name}
                            </button>
                        </div>
                         </div>
                     
                <div className='style-sub-content style-pt-12' data-myID='filename-1'>
                    <div className='style-inline-block style-text-left style-mb-8' data-myID='filename-2'>
                        <div className='style-text-center style-mr-8' data-myID='filename-3'>
                            <button data-myID='filename-4' className={'common-style common-normal style-element-240x44 ' + this.checkEnable('store') }
                                onClick={() => this.applyStorageType('store')}
                            >
                                {this.props.name}
                            </button>
                        </div>
                         </div>

Пожалуйста, помогите.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...