1 # -*- coding:utf-8 -*- 2 #python 2.7 3 #XiaoDeng 4 #http://tieba.baidu.com/p/2460150866 5 #标签操作 6 7 8 from bs4 import BeautifulSoup 9 import urllib.request 10 import re 11 12 13 #如果是网址,可以用这个办法来读取网页 14 #html_doc = "http://tieba.baidu.com/p/2460150866" 15 #req = urllib.request.Request(html_doc) 16 #webpage = urllib.request.urlopen(req) 17 #html = webpage.read() 18 19 20 21 html=""" 22 <html><head><title>The Dormouse's story</title></head> 23 <body> 24 <p class="title" name="dromouse"><b>The Dormouse's story</b></p> 25 <p class="story">Once upon a time there were three little sisters; and their names were 26 <a href="http://example.com/elsie" class="sister" ><!-- Elsie --></a>, 27 <a href="http://example.com/lacie" class="sister" >Lacie</a> and 28 <a href="http://example.com/tillie" class="sister" >Tillie</a>; 29 <a href="http://example.com/lacie" class="sister" >Lacie</a> 30 and they lived at the bottom of a well.</p> 31 <div class="ntopbar_loading"><img src="http://simg.sinajs.cn/blog7style/images/common/loading.gif">加载中…</div> 32 33 <div class="SG_connHead"> 34 <span class="title" comp_title="个人资料">个人资料</span> 35 <span class="edit"> 36 </span> 37 <div class="info_list"> 38 <ul class="info_list1"> 39 <li><span class="SG_txtc">博客等级:</span><span /></span></li> 40 <li><span class="SG_txtc">博客积分:</span><span ><strong>0</strong></span></li> 41 </ul> 42 <ul class="info_list2"> 43 <li><span class="SG_txtc">博客访问:</span><span ><strong>3,971</strong></span></li> 44 <li><span class="SG_txtc">关注人气:</span><span ><strong>0</strong></span></li> 45 <li><span class="SG_txtc">获赠金笔:</span><strong >0支</strong></li> 46 <li><span class="SG_txtc">赠出金笔:</span><strong >0支</strong></li> 47 <li class="lisp" >荣誉徽章:</span></li> 48 </ul> 49 </div> 50 <div class="atcTit_more"><span class="SG_more"><a href="http://blog.sina.com.cn/" target="_blank">更多>></a></span></div> 51 <p class="story">...</p> 52 """ 53 soup = BeautifulSoup(html, 'html.parser') #文档对象 54 55 56 57 # 类名为xxx而且文本内容为hahaha的div 58 for k in soup.find_all('div',class_='atcTit_more'):#,string='更多' 59 print(k) 60 #<div class="atcTit_more"><span class="SG_more"><a href="http://blog.sina.com.cn/" target="_blank">更多>></a></span></div>
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python爬虫:用BeautifulSoup抓取div标签 - Python技术站