最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實戰(zhàn)快速上手BeautifulSoup庫爬取專欄標(biāo)題和地址

 更新時間:2021年10月20日 14:48:50   作者:小旺不正經(jīng)  
BeautifulSoup是爬蟲必學(xué)的技能,BeautifulSoup最主要的功能是從網(wǎng)頁抓取數(shù)據(jù),Beautiful Soup自動將輸入文檔轉(zhuǎn)換為Unicode編碼,輸出文檔轉(zhuǎn)換為utf-8編碼

BeautifulSoup庫快速上手

安裝

pip install beautifulsoup4
# 上面的安裝失敗使用下面的 使用鏡像
pip install beautifulsoup4 -i https://pypi.tuna.tsinghua.edu.cn/simple

使用PyCharm的命令行

image-20211019110243706

解析標(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)簽

image-20211019142518434

解析屬性

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)

image-20211019142955305

根據(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)

image-20211019143400421

多層篩選

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'])

image-20211019144002419

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

image-20211019184236143

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'])

image-20211019184252204

到此這篇關(guān)于Python實戰(zhàn)快速上手BeautifulSoup庫爬取專欄標(biāo)題和地址的文章就介紹到這了,更多相關(guān)Python BeautifulSoup庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python中的日志文件按天分割

    python中的日志文件按天分割

    這篇文章主要介紹了python中的日志文件按天分割方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 一文教你用python編寫Dijkstra算法進行機器人路徑規(guī)劃

    一文教你用python編寫Dijkstra算法進行機器人路徑規(guī)劃

    迪杰斯特拉(Dijkstra)算法是典型最短路徑算法,用于計算一個節(jié)點到其他節(jié)點的最短路徑,這篇文章主要給大家介紹了關(guān)于利用python編寫Dijkstra算法進行機器人路徑規(guī)劃的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • Python數(shù)據(jù)處理篇之Sympy系列(五)---解方程

    Python數(shù)據(jù)處理篇之Sympy系列(五)---解方程

    這篇文章主要介紹了Python數(shù)據(jù)處理篇之Sympy系列(五)---解方程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10
  • Python3中數(shù)據(jù)校驗機制詳解

    Python3中數(shù)據(jù)校驗機制詳解

    在日常編碼環(huán)節(jié),很大比例的錯誤處理工作和參數(shù)的輸入有關(guān),所以這篇文章主要來和大家介紹一下Python3中的數(shù)據(jù)校驗機制,感興趣的可以了解下
    2024-04-04
  • 簡單談?wù)凱ython的pycurl模塊

    簡單談?wù)凱ython的pycurl模塊

    PycURl是一個C語言寫的libcurl的python綁定庫。libcurl 是一個自由的,并且容易使用的用在客戶端的 URL 傳輸庫。它的功能很強大,PycURL 是一個非??焖伲▍⒖级嗖l(fā)操作)和豐富完整特性的,但是有點復(fù)雜的接口。
    2018-04-04
  • Python編程實現(xiàn)及時獲取新郵件的方法示例

    Python編程實現(xiàn)及時獲取新郵件的方法示例

    這篇文章主要介紹了Python編程實現(xiàn)及時獲取新郵件的方法,涉及Python實時查詢郵箱及郵件獲取相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • Python?Pandas輕松實現(xiàn)數(shù)據(jù)清理

    Python?Pandas輕松實現(xiàn)數(shù)據(jù)清理

    在當(dāng)今的數(shù)據(jù)驅(qū)動時代,數(shù)據(jù)清理是數(shù)據(jù)分析、機器學(xué)習(xí)項目中至關(guān)重要的一步,本文將帶大家輕松上手使用Python和Pandas進行數(shù)據(jù)清理,希望對大家有所幫助
    2024-12-12
  • Python實現(xiàn)移動指定圖片到指定目錄

    Python實現(xiàn)移動指定圖片到指定目錄

    這篇文章主要為大家詳細(xì)介紹了如何使用Python的os和shutil庫實現(xiàn)自動化查找和移動圖片功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下
    2025-02-02
  • python使用celery實現(xiàn)異步任務(wù)執(zhí)行的例子

    python使用celery實現(xiàn)異步任務(wù)執(zhí)行的例子

    今天小編就為大家分享一篇python使用celery實現(xiàn)異步任務(wù)執(zhí)行的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Python面向?qū)ο缶幊剃P(guān)鍵深度探索類與對象

    Python面向?qū)ο缶幊剃P(guān)鍵深度探索類與對象

    這篇文章主要為大家介紹了Python面向?qū)ο缶幊剃P(guān)鍵深度探索類與對象示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05

最新評論

丽水市| 嘉黎县| 隆安县| 东明县| 瑞丽市| 额尔古纳市| 潼关县| 娱乐| 汽车| 定兴县| 汪清县| 西丰县| 辽宁省| 永丰县| 图们市| 乌拉特前旗| 都匀市| 萨迦县| 枝江市| 高邮市| 乐亭县| 宁河县| 新津县| 苍山县| 五原县| 车险| 定边县| 分宜县| 怀宁县| 漾濞| 阿坝县| 洮南市| 颍上县| 怀化市| 营口市| 汝南县| 平泉县| 讷河市| 周口市| 盐池县| 南京市|