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

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

 更新時(shí)間:2021年10月20日 14:48:50   作者:小旺不正經(jīng)  
BeautifulSoup是爬蟲必學(xué)的技能,BeautifulSoup最主要的功能是從網(wǎng)頁抓取數(shù)據(jù),Beautiful Soup自動(dòng)將輸入文檔轉(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

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

相關(guān)文章

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

    python中的日志文件按天分割

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

    一文教你用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系列(五)---解方程

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

    Python3中數(shù)據(jù)校驗(yàn)機(jī)制詳解

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

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

    PycURl是一個(gè)C語言寫的libcurl的python綁定庫。libcurl 是一個(gè)自由的,并且容易使用的用在客戶端的 URL 傳輸庫。它的功能很強(qiáng)大,PycURL 是一個(gè)非常快速(參考多并發(fā)操作)和豐富完整特性的,但是有點(diǎn)復(fù)雜的接口。
    2018-04-04
  • Python編程實(shí)現(xiàn)及時(shí)獲取新郵件的方法示例

    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ù)清理

    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)指定圖片到指定目錄

    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í)行的例子

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

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

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

最新評論

卢湾区| 珠海市| 民县| 西畴县| 古交市| 星子县| 丰原市| 通江县| 东丽区| 大安市| 光泽县| 交口县| 苏尼特右旗| 紫云| 常州市| 曲沃县| 鄂温| 佳木斯市| 婺源县| 穆棱市| 寿阳县| 固阳县| 文山县| 繁峙县| 左云县| 云和县| 常山县| 平舆县| 定南县| 奉化市| 辽中县| 资阳市| 荆州市| 盐山县| 比如县| 彭州市| 柞水县| 沂南县| 深水埗区| 灌阳县| 敦化市|