>  기사  >  백엔드 개발  >  Python에서 div 태그를 가져오기 위해 BeautifulSoup을 사용하는 예제 튜토리얼

Python에서 div 태그를 가져오기 위해 BeautifulSoup을 사용하는 예제 튜토리얼

零下一度
零下一度원래의
2017-06-01 10:23:211750검색

이 글은 주로 Python 3에서 p 태그를 캡처하기 위해 BeautifulSoup을 사용하는 방법을 소개합니다. 이 글은 여러분의 참고와 학습을 위한 상세한 샘플 코드를 제공합니다. 이 글은 필요한 모든 사람을 위한 특정 참고 자료와 학습 가치를 제공합니다. .

머리말

이 글은 Python 3에서 BeautifulSoup을 사용하여 p 태그를 가져오는 방법에 대한 예제를 주로 소개합니다. 모든 사람의 참조와 학습을 위해 공유됩니다. 자세한 소개를 살펴보겠습니다.

샘플 코드:

# -*- coding:utf-8 -*-
#python 2.7
#XiaoDeng
#http://tieba.baidu.com/p/2460150866
#标签操作


from bs4 import BeautifulSoup
import urllib.request
import re


#如果是网址,可以用这个办法来读取网页
#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&#39;s story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse&#39;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>
<p class="ntopbar_loading"><img src="http://simg.sinajs.cn/blog7style/images/common/loading.gif">加载中…</p>

<p class="SG_connHead">
   <span class="title" comp_title="个人资料">个人资料</span>
   <span class="edit">
      </span>
<p class="info_list">  
         <ul class="info_list1">
     <li><span class="SG_txtc">博客等级:</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">博客积分:</span><span id="comp_901_score"><strong>0</strong></span></li>
     </ul>
     <ul class="info_list2">
     <li><span class="SG_txtc">博客访问:</span><span id="comp_901_pv"><strong>3,971</strong></span></li>
     <li><span class="SG_txtc">关注人气:</span><span id="comp_901_attention"><strong>0</strong></span></li>
     <li><span class="SG_txtc">获赠金笔:</span><strong id="comp_901_d_goldpen">0支</strong></li>
     <li><span class="SG_txtc">赠出金笔:</span><strong id="comp_901_r_goldpen">0支</strong></li>
     <li class="lisp" id="comp_901_badge"><span class="SG_txtc">荣誉徽章:</span></li>
     </ul>
     </p>
<p class="atcTit_more"><span class="SG_more"><a href="http://blog.sina.com.cn/" rel="external nofollow" rel="external nofollow" target="_blank">更多>></a></span></p>     
<p class="story">...</p>
"""
soup = BeautifulSoup(html, &#39;html.parser&#39;) #文档对象



# 类名为xxx而且文本内容为hahaha的p
for k in soup.find_all(&#39;p&#39;,class_=&#39;atcTit_more&#39;):#,string=&#39;更多&#39;
 print(k)
 #<p class="atcTit_more"><span class="SG_more"><a href="http://blog.sina.com.cn/" rel="external nofollow" rel="external nofollow" target="_blank">更多>></a></span></p>

[관련 권장 사항]

1. Python은 beautifulSoup을 사용하여 크롤러를 구현합니다

2. Python을 사용하여 비동기 프록시 크롤러 및 프록시 풀 메서드를 구현합니다

3. 크롤러는 프록시 프록시를 사용하여 웹 페이지를 크롤링합니다

위 내용은 Python에서 div 태그를 가져오기 위해 BeautifulSoup을 사용하는 예제 튜토리얼의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.