python 3利用BeautifulSoup抓取div標(biāo)簽的方法示例
前言
本文主要介紹的是關(guān)于python 3用BeautifulSoup抓取div標(biāo)簽的方法示例,分享出來(lái)供大家參考學(xué)習(xí),下面來(lái)看看詳細(xì)的介紹:
示例代碼:
# -*- coding:utf-8 -*-
#python 2.7
#XiaoDeng
#http://tieba.baidu.com/p/2460150866
#標(biāo)簽操作
from bs4 import BeautifulSoup
import urllib.request
import re
#如果是網(wǎng)址,可以用這個(gè)辦法來(lái)讀取網(wǎng)頁(yè)
#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 rel="external nofollow" class="sister" id="xiaodeng"><!-- Elsie --></a>,
<a rel="external nofollow" rel="external nofollow" class="sister" id="link2">Lacie</a> and
<a rel="external nofollow" class="sister" id="link3">Tillie</a>;
<a 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">加載中…</div>
<div class="SG_connHead">
<span class="title" comp_title="個(gè)人資料">個(gè)人資料</span>
<span class="edit">
</span>
<div class="info_list">
<ul class="info_list1">
<li><span class="SG_txtc">博客等級(jí):</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">博客訪問(wèn):</span><span id="comp_901_pv"><strong>3,971</strong></span></li>
<li><span class="SG_txtc">關(guān)注人氣:</span><span id="comp_901_attention"><strong>0</strong></span></li>
<li><span class="SG_txtc">獲贈(zèng)金筆:</span><strong id="comp_901_d_goldpen">0支</strong></li>
<li><span class="SG_txtc">贈(zèng)出金筆:</span><strong id="comp_901_r_goldpen">0支</strong></li>
<li class="lisp" id="comp_901_badge"><span class="SG_txtc">榮譽(yù)徽章:</span></li>
</ul>
</div>
<div class="atcTit_more"><span class="SG_more"><a rel="external nofollow" rel="external nofollow" target="_blank">更多>></a></span></div>
<p class="story">...</p>
"""
soup = BeautifulSoup(html, 'html.parser') #文檔對(duì)象
# 類名為xxx而且文本內(nèi)容為hahaha的div
for k in soup.find_all('div',class_='atcTit_more'):#,string='更多'
print(k)
#<div class="atcTit_more"><span class="SG_more"><a rel="external nofollow" rel="external nofollow" target="_blank">更多>></a></span></div>
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家腳本之家的支持。
- Python爬蟲(chóng)包 BeautifulSoup 遞歸抓取實(shí)例詳解
- python基于BeautifulSoup實(shí)現(xiàn)抓取網(wǎng)頁(yè)指定內(nèi)容的方法
- python使用beautifulsoup從愛(ài)奇藝網(wǎng)抓取視頻播放
- 使用python BeautifulSoup庫(kù)抓取58手機(jī)維修信息
- python BeautifulSoup使用方法詳解
- python 解析html之BeautifulSoup
- Python使用BeautifulSoup庫(kù)解析HTML基本使用教程
- python利用beautifulSoup實(shí)現(xiàn)爬蟲(chóng)
- python使用BeautifulSoup分頁(yè)網(wǎng)頁(yè)中超鏈接的方法
- Python爬蟲(chóng)beautifulsoup4常用的解析方法總結(jié)
- python3 BeautifulSoup模塊使用字典的方法抓取a標(biāo)簽內(nèi)的數(shù)據(jù)示例
相關(guān)文章
python利用appium實(shí)現(xiàn)手機(jī)APP自動(dòng)化的示例
這篇文章主要介紹了python利用appium實(shí)現(xiàn)手機(jī)APP自動(dòng)化的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
python-nmap實(shí)現(xiàn)python利用nmap進(jìn)行掃描分析
Nmap是一個(gè)非常用的網(wǎng)絡(luò)/端口掃描工具,如果想將nmap集成進(jìn)你的工具里,可以使用python-nmap這個(gè)python庫(kù),它提供了一個(gè)簡(jiǎn)單的接口來(lái)使用nmap進(jìn)行掃描,下面我們來(lái)看看python-nmap的具體應(yīng)用吧2024-12-12
Python模型聚合查詢\Q查詢\F查詢\分組查詢操作技巧解析
這篇文章主要介紹了模型查詢中的一些操作技巧,主要包括模型聚合查詢,Q查詢,F(xiàn)查詢,分組查詢,有需要的朋友可以借鑒參考下,希望可以有所幫助2021-09-09
利用Python中SocketServer 實(shí)現(xiàn)客戶端與服務(wù)器間非阻塞通信
本文主要介紹了利用Python中SocketServer 實(shí)現(xiàn)客戶端與服務(wù)器間非阻塞通信示例代碼,具有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧2016-12-12
Python實(shí)現(xiàn)多進(jìn)程的四種方式
今天小編就為大家分享一篇關(guān)于Python實(shí)現(xiàn)多進(jìn)程的四種方式,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02
python標(biāo)準(zhǔn)庫(kù)模塊之json庫(kù)的基礎(chǔ)用法
json庫(kù)是處理JSON格式的Python標(biāo)準(zhǔn)庫(kù),json庫(kù)主要包括兩類函數(shù),操作函數(shù)和解析函數(shù),下面這篇文章主要給大家介紹了關(guān)于python標(biāo)準(zhǔn)庫(kù)模塊之json庫(kù)的基礎(chǔ)用法,需要的朋友可以參考下2022-06-06
Django Rest framework之權(quán)限的實(shí)現(xiàn)示例
這篇文章主要介紹了Django Rest framework之權(quán)限的實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
解決更改AUTH_USER_MODEL后出現(xiàn)的問(wèn)題
這篇文章主要介紹了解決更改AUTH_USER_MODEL后出現(xiàn)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05

