<html> <head> </head> <body class="nodata"> <div class="hdata"> <span class="choose_money " data-id="6">6C币</span> <span class="choose_money " data-id="10">10C币</span> <span class="choose_money " data-id="20">20C币</span> </div> <div class="hdata"> <li><a data-code="html">HTML/XML</a></li> <li><a data-code="ruby">Ruby</a></li> <li><a data-code="php">PHP</a></li> </div> </body> </html>
from bs4 import BeautifulSoup with open ('E:/a.txt','r') as f: text=f.read() soup=BeautifulSoup(text,'html.parser') content=soup.find_all('div',{'class':'hdata'}) print(content[1])
通过print(content[1])得到的是:
<div class="hdata">
<li><a data-code="html">HTML/XML</a></li>
<li><a data-code="ruby">Ruby</a></li>
<li><a data-code="php">PHP</a></li>
</div>