欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

BeautifulSoup庫(kù)快速上手

安裝

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庫(kù)

第二行代碼:導(dǎo)入requests

第三、四、五行代碼:獲取url的html

第六行代碼:激活BeautifulSoup庫(kù) 'html.parser'設(shè)置解析器為HTML解析器

第七行代碼:選取所有<h2>標(biāo)簽

image-20211019142518434

解析屬性

BeautifulSoup庫(kù) 支持根據(jù)特定屬性解析網(wǎng)頁(yè)元素

根據(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'>
        測(cè)試成功
        </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>測(cè)試成功</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庫(kù)爬取專欄標(biāo)題和地址的文章就介紹到這了,更多相關(guān)Python BeautifulSoup庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python字典“鍵”和“值”的排序5種方法

    Python字典“鍵”和“值”的排序5種方法

    這篇文章主要介紹了5種Python字典“鍵”和“值”的排序方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • 用python 制作圖片轉(zhuǎn)pdf工具

    用python 制作圖片轉(zhuǎn)pdf工具

    這篇文章主要介紹了用python 制作圖片轉(zhuǎn)pdf工具的思路及代碼,非常詳細(xì),有需要的小伙伴參考下
    2015-01-01
  • Python 實(shí)現(xiàn)在文件中的每一行添加一個(gè)逗號(hào)

    Python 實(shí)現(xiàn)在文件中的每一行添加一個(gè)逗號(hào)

    下面小編就為大家分享一篇Python 實(shí)現(xiàn)在文件中的每一行添加一個(gè)逗號(hào),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • pytest中配置文件pytest.ini使用

    pytest中配置文件pytest.ini使用

    本文主要介紹了pytest中配置文件pytest.ini使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • Python如何判斷字符串是否僅包含數(shù)字

    Python如何判斷字符串是否僅包含數(shù)字

    在用Python進(jìn)行數(shù)據(jù)處理的時(shí)候,經(jīng)常會(huì)遇到DataFrame中的某一列本應(yīng)該是數(shù)值類型,但由于數(shù)據(jù)不規(guī)范導(dǎo)致在字段中夾雜了非數(shù)值類型,本文就介紹了Python如何判斷字符串是否僅包含數(shù)字,感興趣的可以了解一下
    2022-03-03
  • Python實(shí)現(xiàn)八大排序算法

    Python實(shí)現(xiàn)八大排序算法

    這篇文章主要介紹了Python實(shí)現(xiàn)八大排序算法,如何用Python實(shí)現(xiàn)八大排序算法,感興趣的小伙伴們可以參考一下
    2016-08-08
  • python打造爬蟲(chóng)代理池過(guò)程解析

    python打造爬蟲(chóng)代理池過(guò)程解析

    這篇文章主要介紹了python打造爬蟲(chóng)代理池過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • python調(diào)用短信貓控件實(shí)現(xiàn)發(fā)短信功能實(shí)例

    python調(diào)用短信貓控件實(shí)現(xiàn)發(fā)短信功能實(shí)例

    這篇文章主要介紹了python調(diào)用短信貓控件實(shí)現(xiàn)發(fā)短信功能實(shí)例,需要的朋友可以參考下
    2014-07-07
  • Python 窗體(tkinter)按鈕 位置實(shí)例

    Python 窗體(tkinter)按鈕 位置實(shí)例

    今天小編就為大家分享一篇Python 窗體(tkinter)按鈕 位置實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • Python 函數(shù)繪圖及函數(shù)圖像微分與積分

    Python 函數(shù)繪圖及函數(shù)圖像微分與積分

    今天小編就為大家分享一篇Python 函數(shù)繪圖及函數(shù)圖像微分與積分,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11

最新評(píng)論