日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python beautifulsoup4_Python之Beautiful Soup 4使用实例

發(fā)布時間:2024/9/27 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python beautifulsoup4_Python之Beautiful Soup 4使用实例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Beautiful Soup 是一個可以從HTML或XML文件中提取數(shù)據(jù)的Python庫,它能夠通過你喜歡的轉(zhuǎn)換器實現(xiàn)慣用的文檔導(dǎo)航、查找、修改文檔的方式。

Beautiful Soup 4 官方文檔:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/

一、安裝Beautiful Soup

運(yùn)行cmd,cd切換到python安裝目錄下的Scripts目錄,執(zhí)行命令

pip3 install beautifulsoup4

二、安裝解析器lxml

Beautiful Soup支持Python標(biāo)準(zhǔn)庫中的HTML解析器,也支持一些第三方的解析器,推薦用lxml,速度快,文檔容錯能力強(qiáng),需要安裝。

pip3 install lxml

三、使用實例

下面測試抓取博客園首頁一些信息

1、簡單信息的抓取

importreimportrequestsfrom bs4 importBeautifulSoup

r= requests.get('https://www.cnblogs.com')

soup= BeautifulSoup(r.text, 'lxml') #lxml為解析器

print(soup.title, soup.title.string) #獲取指定標(biāo)簽,獲取指定標(biāo)簽里面的內(nèi)容

print(soup('title'), soup('title')[0].string) #獲取指定標(biāo)簽也可以寫成這樣

print(soup.meta.get('charset')) #獲取指定標(biāo)簽的屬性

print(soup.meta['charset']) #獲取指定標(biāo)簽的屬性也可寫成這樣

print(soup.meta) #獲取第一個標(biāo)簽(多個只取第一個)

print(soup.find('meta')) #獲取第一個標(biāo)簽,結(jié)果和上面一樣

print(soup.find('meta', attrs={'name':'viewport'})) #獲取第一個標(biāo)簽,根據(jù)屬性過濾獲取

print(soup.find_all('meta', attrs={'charset':True})) #獲取所有標(biāo)簽的列表,同時根據(jù)是否含有屬性charset過濾獲取

運(yùn)行結(jié)果:

博客園 - 開發(fā)者的網(wǎng)上家園 博客園 - 開發(fā)者的網(wǎng)上家園

[

博客園 - 開發(fā)者的網(wǎng)上家園] 博客園 - 開發(fā)者的網(wǎng)上家園

utf-8

utf-8

[]

2、抓取首頁的導(dǎo)航條信息

print('抓取導(dǎo)航,實現(xiàn)方法1')for item in soup.select('div#nav_menu a'):print(item.get('href'), item.string)print('抓取導(dǎo)航,實現(xiàn)方法2')for item in soup.find('div', {'id':'nav_menu'}).children:print(item['href'], item.string)

運(yùn)行結(jié)果:

抓取導(dǎo)航,實現(xiàn)方法1

https://home.cnblogs.com/ 園子

https://news.cnblogs.com 新聞

https://q.cnblogs.com/ 博問

https://ing.cnblogs.com/ 閃存

https://group.cnblogs.com/ 小組

https://wz.cnblogs.com/ 收藏

https://job.cnblogs.com/ 招聘

https://edu.cnblogs.com/ 班級

http://zzk.cnblogs.com/ 找找看

抓取導(dǎo)航,實現(xiàn)方法2

https://home.cnblogs.com/ 園子

https://news.cnblogs.com 新聞

https://q.cnblogs.com/ 博問

https://ing.cnblogs.com/ 閃存

https://group.cnblogs.com/ 小組

https://wz.cnblogs.com/ 收藏

https://job.cnblogs.com/ 招聘

https://edu.cnblogs.com/ 班級

http://zzk.cnblogs.com/ 找找看

3、抓取網(wǎng)站分類

print('抓取網(wǎng)站分類,實現(xiàn)方法1')for item in soup.select('ul#cate_item li'):print(item.find('a').get('href'),item.find('a').string)print('抓取網(wǎng)站分類,實現(xiàn)方法2')for item in soup.find_all(id=re.compile('^cate_item_')):print(item.find('a').get('href'),item.find('a').string)

運(yùn)行結(jié)果:

抓取網(wǎng)站分類,實現(xiàn)方法1

/cate/108698/ .NET技術(shù)(8)

/cate/2/ 編程語言(41)

/cate/108701/ 軟件設(shè)計(0)

/cate/108703/ Web前端(10)

/cate/108704/ 企業(yè)信息化(0)

/cate/108705/ 手機(jī)開發(fā)(3)

/cate/108709/ 軟件工程(0)

/cate/108712/ 數(shù)據(jù)庫技術(shù)(9)

/cate/108724/ 操作系統(tǒng)(9)

/cate/4/ 其他分類(16)

/cate/all/ 所有隨筆(1571)

/comment/ 所有評論(491)

抓取網(wǎng)站分類,實現(xiàn)方法2

/cate/108698/ .NET技術(shù)(8)

/cate/2/ 編程語言(41)

/cate/108701/ 軟件設(shè)計(0)

/cate/108703/ Web前端(10)

/cate/108704/ 企業(yè)信息化(0)

/cate/108705/ 手機(jī)開發(fā)(3)

/cate/108709/ 軟件工程(0)

/cate/108712/ 數(shù)據(jù)庫技術(shù)(9)

/cate/108724/ 操作系統(tǒng)(9)

/cate/4/ 其他分類(16)

/cate/all/ 所有隨筆(1571)

/comment/ 所有評論(491)

4、抓取首頁的所有隨筆信息

print('抓取隨筆信息')

post_item_body= soup.find_all('div', 'post_item_body')for item inpost_item_body:print(item.h3.a['href'])print(item.h3.a.string)print(item.p.get_text().strip())print(item.div.a.string)print(item.div.a.next_sibling.replace('發(fā)布于','').strip())

運(yùn)行結(jié)果:

抓取隨筆信息

https://www.cnblogs.com/chq1234/p/11400367.html

js全選與取消全選

實現(xiàn)全選與取消全選的效果 要求1(將軍影響士兵):點(diǎn)擊全選按鈕,下面的復(fù)選框全部選中,取消全選按鈕,下面的復(fù)選框全部取消 思路:復(fù)選框是否被選中,取決于check屬性,將全選按鈕的check屬性值賦值給下面所有復(fù)選框的check值 要求2(士兵影響將軍): 當(dāng)下面的某個復(fù)選框沒有被選中時,全選按鈕自 ...

源氏西格瑪

2019-08-23 15:36

https://www.cnblogs.com/lenve/p/11400056.html

40 篇原創(chuàng)干貨,帶你進(jìn)入 Spring Boot 殿堂!

兩個月前,松哥總結(jié)過一次已經(jīng)完成的 Spring Boot 教程,當(dāng)時感受到了小伙伴們巨大的熱情。 兩個月過去了,松哥的 Spring Boot 教程又更新了不少,為了方便小伙伴們查找,這里再給大家做一個索引參考。 需要再次說明的是,這一系列教程不是終點(diǎn),而是一個起點(diǎn),松哥后期還會不斷完善這個教程, ...

江南一點(diǎn)雨

2019-08-23 14:58

.....................后面內(nèi)容太長了省略....................

總結(jié)

以上是生活随笔為你收集整理的python beautifulsoup4_Python之Beautiful Soup 4使用实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。