Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.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 beautifulsoup add<;部门>;类位于html的末尾_Python_Beautifulsoup - Fatal编程技术网

Python beautifulsoup add<;部门>;类位于html的末尾

Python beautifulsoup add<;部门>;类位于html的末尾,python,beautifulsoup,Python,Beautifulsoup,我有一个带有多个标记的HTML文件(div中也有多个div)。我想在HTML末尾的特定位置添加一个新的标记和类。我尝试了append、insert和insert\u after/insert\u before,但是,它并没有像我预期的那样工作 我的html输入是: <div id="page"> <div id="records"> <div class="record"> &

我有一个带有多个标记的HTML文件(div中也有多个div)。我想在HTML末尾的特定位置添加一个新的标记和类。我尝试了append、insert和insert\u after/insert\u before,但是,它并没有像我预期的那样工作

我的html输入是:

   <div id="page">
   <div id="records">
   <div class="record">
   <div class="header">
   <div class="title">
    Something here to display
   </div>
   </div>
   <div class="disclaimer">
   <p>Here i want to print content</p>
   </div>
   </div>
   <div class="record">
   <div class="header">
   <div class="title">
    Something here to display again once
   </div>
   </div>
   <div class="disclaimer">
   <p>Here i want to print content again once</p>
   </div>
   </div>
   <div class="record">
   <div class="header">
   <div class="title">
    Something here to display second time
   </div>
   </div>
   <div class="disclaimer">
   <p>Here i want to print content second time</p>
   </div>
   </div>
</div>
</div>
在我的例子中,
的数字不是固定的,数字可能总是不同的


我想使用python中的BeautifulSoup获得此问题的解决方案/建议。

您可以在
汤中最后一项之后使用
insert\u。查找所有('div',class='record')

使用
.append()
,它需要选择父元素或

newRecord=''
这里有第三次展示的东西
这里我想第三次打印内容

''' soup=BeautifulSoup(sourceHTML,'html.parser') 页面=汤。选择一个(“#页面”) append(美化组(newRecord,'html.parser')) 打印(soup.prettify())
这正是我想要的,非常感谢您提供了最简单的方法,可以提取最后一个元素,然后插入到一行中。
   <div id="page">
   <div id="records">
   <div class="record">
   <div class="header">
   <div class="title">
    Something here to display
   </div>
   </div>
   <div class="disclaimer">
   <p>Here i want to print content</p>
   </div>
   </div>
   <div class="record">
   <div class="header">
   <div class="title">
    Something here to display again once
   </div>
   </div>
   <div class="disclaimer">
   <p>Here i want to print content again once</p>
   </div>
   </div>
   <div class="record">
   <div class="header">
   <div class="title">
    Something here to display second time
   </div>
   </div>
   <div class="disclaimer">
   <p>Here i want to print content second time</p>
   </div>
   </div>
   <div class="record">
   <div class="header">
   <div class="title">
    Something here to display 3rd time
   </div>
   </div>
   <div class="disclaimer">
   <p>Here i want to print content 3rd time</p>
   </div>
   </div>
</div>
</div>
from bs4 import BeautifulSoup

html = '<div id="records"> <div class="record"> <div class="header"> <div class="title"> Something here to display </div> </div> <div class="disclaimer"> <p>Here i want to print content</p> </div> </div> <div class="record"> <div class="header"> <div class="title"> Something here to display again once </div> </div> <div class="disclaimer"> <p>Here i want to print content again once</p> </div> </div> <div class="record"> <div class="header"> <div class="title"> Something here to display second time </div> </div> <div class="disclaimer"> <p>Here i want to print content second time</p> </div> </div> </div>'

soup = BeautifulSoup(html, 'html.parser')

extra_html = '''
<div class="record">
    <div class="header">
        <div class="title">
            Something here to display 3rd time
        </div>
    </div>
    <div class="disclaimer">
        <p>Here i want to print content 3rd time</p>
    </div>
</div>'''

soup.find_all('div', class_='record')[-1].insert_after(BeautifulSoup(extra_html, 'html.parser')) # [-1] selects the last item
<div id="records">
 <div class="record">
  <div class="header">
   <div class="title">
    Something here to display
   </div>
  </div>
  <div class="disclaimer">
   <p>
    Here i want to print content
   </p>
  </div>
 </div>
 <div class="record">
  <div class="header">
   <div class="title">
    Something here to display again once
   </div>
  </div>
  <div class="disclaimer">
   <p>
    Here i want to print content again once
   </p>
  </div>
 </div>
 <div class="record">
  <div class="header">
   <div class="title">
    Something here to display second time
   </div>
  </div>
  <div class="disclaimer">
   <p>
    Here i want to print content second time
   </p>
  </div>
 </div>
 <div class="record">
  <div class="header">
   <div class="title">
    Something here to display 3rd time
   </div>
  </div>
  <div class="disclaimer">
   <p>
    Here i want to print content 3rd time
   </p>
  </div>
 </div>
</div>
newRecord = '''
<div class="record">
  <div class="header">
    <div class="title">
      Something here to display 3rd time
    </div>
  </div>
  <div class="disclaimer">
    <p>Here i want to print content 3rd time</p>
  </div>
</div>
'''

soup = BeautifulSoup(sourceHTML, 'html.parser')
page = soup.select_one('#page')
page.append(BeautifulSoup(newRecord, 'html.parser'))
print(soup.prettify())