BeautifulSoupを使用してPythonでdivタグを取得するサンプルチュートリアル

零下一度
リリース: 2017-06-01 10:23:21
オリジナル
1757 人が閲覧しました

この記事では主に、Python 3 で BeautifulSoup を使用して p タグをキャプチャする方法を紹介します。この記事には、参考と学習のための詳細なサンプルコードが記載されています。必要な方は以下をご覧ください。 。

はじめに

この記事では主に、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 の詳細な説明クローラーはプロキシ プロキシを使用して Web ページをクロールします

以上がBeautifulSoupを使用してPythonでdivタグを取得するサンプルチュートリアルの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!