Example of python 3 using BeautifulSoup to grab div tags

  • 2020-06-03 07:03:53
  • OfStack

preface

This paper mainly introduces the python 3 using BeautifulSoup to grab the div tag method example, Shared for your reference and learning, here is a look at the detailed introduction:

Sample code:


# -*- coding:utf-8 -*-
#python 2.7
#XiaoDeng
#http://tieba.baidu.com/p/2460150866
# Tags operate 


from bs4 import BeautifulSoup
import urllib.request
import re


# If it's a web address, you can use this method to read a web page 
#html_doc = "http://tieba.baidu.com/p/2460150866"
#req = urllib.request.Request(html_doc) 
#webpage = urllib.request.urlopen(req) 
#html = webpage.read()



html="""
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" rel="external nofollow" class="sister" id="xiaodeng"><!-- Elsie --></a>,
<a href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" rel="external nofollow" class="sister" id="link3">Tillie</a>;
<a href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" class="sister" id="xiaodeng">Lacie</a>
and they lived at the bottom of a well.</p>
<div class="ntopbar_loading"><img src="http://simg.sinajs.cn/blog7style/images/common/loading.gif"> Loading in... </div>

<div class="SG_connHead">
   <span class="title" comp_title=" The personal data "> The personal data </span>
   <span class="edit">
      </span>
<div class="info_list">  
         <ul class="info_list1">
     <li><span class="SG_txtc"> Blog level: </span><span id="comp_901_grade"><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://simg.sinajs.cn/blog7style/images/common/number/9.gif" /></span></li>
     <li><span class="SG_txtc"> Blog Credits: </span><span id="comp_901_score"><strong>0</strong></span></li>
     </ul>
     <ul class="info_list2">
     <li><span class="SG_txtc"> Blog access: </span><span id="comp_901_pv"><strong>3,971</strong></span></li>
     <li><span class="SG_txtc"> Focus on popularity: </span><span id="comp_901_attention"><strong>0</strong></span></li>
     <li><span class="SG_txtc"> Gold pens: </span><strong id="comp_901_d_goldpen">0 the </strong></li>
     <li><span class="SG_txtc"> A golden pen: </span><strong id="comp_901_r_goldpen">0 the </strong></li>
     <li class="lisp" id="comp_901_badge"><span class="SG_txtc"> Badge of Honor: </span></li>
     </ul>
     </div>
<div class="atcTit_more"><span class="SG_more"><a href="http://blog.sina.com.cn/" rel="external nofollow" rel="external nofollow" target="_blank"> More and more &gt;&gt;</a></span></div>     
<p class="story">...</p>
"""
soup = BeautifulSoup(html, 'html.parser') # The document object 



#  Class called xxx And the text content is hahaha the div
for k in soup.find_all('div',class_='atcTit_more'):#,string=' More and more '
 print(k)
 #<div class="atcTit_more"><span class="SG_more"><a href="http://blog.sina.com.cn/" rel="external nofollow" rel="external nofollow" target="_blank"> More and more &gt;&gt;</a></span></div>

conclusion


Related articles: