Python實(shí)戰(zhàn)快速上手BeautifulSoup庫爬取專欄標(biāo)題和地址
BeautifulSoup庫快速上手
安裝
pip install beautifulsoup4 # 上面的安裝失敗使用下面的 使用鏡像 pip install beautifulsoup4 -i https://pypi.tuna.tsinghua.edu.cn/simple
使用PyCharm的命令行

解析標(biāo)簽
from bs4 import BeautifulSoup
import requests
url='https://blog.csdn.net/weixin_42403632/category_11076268.html'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0'}
html=requests.get(url,headers=headers).text
s=BeautifulSoup(html,'html.parser')
title =s.select('h2')
for i in title:
print(i.text)
第一行代碼:導(dǎo)入BeautifulSoup庫
第二行代碼:導(dǎo)入requests
第三、四、五行代碼:獲取url的html
第六行代碼:激活BeautifulSoup庫 'html.parser'設(shè)置解析器為HTML解析器
第七行代碼:選取所有<h2>標(biāo)簽

解析屬性
BeautifulSoup庫 支持根據(jù)特定屬性解析網(wǎng)頁元素
根據(jù)class值解析
from bs4 import BeautifulSoup
import requests
url='https://blog.csdn.net/weixin_42403632/category_11076268.html'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0'}
html=requests.get(url,headers=headers).text
s=BeautifulSoup(html,'html.parser')
title =s.select('.column_article_title')
for i in title:
print(i.text)

根據(jù)ID解析
from bs4 import BeautifulSoup
html='''<div class="crop-img-before">
<img src="" alt="" id="cropImg">
</div>
<div id='title'>
測試成功
</div>
<div class="crop-zoom">
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="bt-reduce">-</a><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="bt-add">+</a>
</div>
<div class="crop-img-after">
<div class="final-img"></div>
</div>'''
s=BeautifulSoup(html,'html.parser')
title =s.select('#title')
for i in title:
print(i.text)

多層篩選
from bs4 import BeautifulSoup
html='''<div class="crop-img-before">
<img src="" alt="" id="cropImg">
</div>
<div id='title'>
456456465
<h1>測試成功</h1>
</div>
<div class="crop-zoom">
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="bt-reduce">-</a><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="bt-add">+</a>
</div>
<div class="crop-img-after">
<div class="final-img"></div>
</div>'''
s=BeautifulSoup(html,'html.parser')
title =s.select('#title')
for i in title:
print(i.text)
title =s.select('#title h1')
for i in title:
print(i.text)
提取a標(biāo)簽中的網(wǎng)址
title =s.select('a')
for i in title:
print(i['href'])

實(shí)戰(zhàn)-獲取博客專欄 標(biāo)題+網(wǎng)址

from bs4 import BeautifulSoup
import requests
import re
url='https://blog.csdn.net/weixin_42403632/category_11298953.html'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0'}
html=requests.get(url,headers=headers).text
s=BeautifulSoup(html,'html.parser')
title =s.select('.column_article_list li a')
for i in title:
print((re.findall('原創(chuàng).*?\n(.*?)\n',i.text))[0].lstrip())
print(i['href'])

到此這篇關(guān)于Python實(shí)戰(zhàn)快速上手BeautifulSoup庫爬取專欄標(biāo)題和地址的文章就介紹到這了,更多相關(guān)Python BeautifulSoup庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python爬取求職網(wǎng)requests庫和BeautifulSoup庫使用詳解
- python爬蟲beautifulsoup庫使用操作教程全解(python爬蟲基礎(chǔ)入門)
- python BeautifulSoup庫的安裝與使用
- Python獲取基金網(wǎng)站網(wǎng)頁內(nèi)容、使用BeautifulSoup庫分析html操作示例
- python用BeautifulSoup庫簡單爬蟲實(shí)例分析
- Python使用BeautifulSoup庫解析HTML基本使用教程
- 使用python BeautifulSoup庫抓取58手機(jī)維修信息
- Python?使用BeautifulSoup庫的方法
相關(guān)文章
一文教你用python編寫Dijkstra算法進(jìn)行機(jī)器人路徑規(guī)劃
迪杰斯特拉(Dijkstra)算法是典型最短路徑算法,用于計(jì)算一個(gè)節(jié)點(diǎn)到其他節(jié)點(diǎn)的最短路徑,這篇文章主要給大家介紹了關(guān)于利用python編寫Dijkstra算法進(jìn)行機(jī)器人路徑規(guī)劃的相關(guān)資料,需要的朋友可以參考下2021-08-08
Python數(shù)據(jù)處理篇之Sympy系列(五)---解方程
這篇文章主要介紹了Python數(shù)據(jù)處理篇之Sympy系列(五)---解方程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Python3中數(shù)據(jù)校驗(yàn)機(jī)制詳解
在日常編碼環(huán)節(jié),很大比例的錯(cuò)誤處理工作和參數(shù)的輸入有關(guān),所以這篇文章主要來和大家介紹一下Python3中的數(shù)據(jù)校驗(yàn)機(jī)制,感興趣的可以了解下2024-04-04
Python編程實(shí)現(xiàn)及時(shí)獲取新郵件的方法示例
這篇文章主要介紹了Python編程實(shí)現(xiàn)及時(shí)獲取新郵件的方法,涉及Python實(shí)時(shí)查詢郵箱及郵件獲取相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Python?Pandas輕松實(shí)現(xiàn)數(shù)據(jù)清理
在當(dāng)今的數(shù)據(jù)驅(qū)動(dòng)時(shí)代,數(shù)據(jù)清理是數(shù)據(jù)分析、機(jī)器學(xué)習(xí)項(xiàng)目中至關(guān)重要的一步,本文將帶大家輕松上手使用Python和Pandas進(jìn)行數(shù)據(jù)清理,希望對大家有所幫助2024-12-12
Python實(shí)現(xiàn)移動(dòng)指定圖片到指定目錄
這篇文章主要為大家詳細(xì)介紹了如何使用Python的os和shutil庫實(shí)現(xiàn)自動(dòng)化查找和移動(dòng)圖片功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2025-02-02
python使用celery實(shí)現(xiàn)異步任務(wù)執(zhí)行的例子
今天小編就為大家分享一篇python使用celery實(shí)現(xiàn)異步任務(wù)執(zhí)行的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python面向?qū)ο缶幊剃P(guān)鍵深度探索類與對象
這篇文章主要為大家介紹了Python面向?qū)ο缶幊剃P(guān)鍵深度探索類與對象示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

