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

Python數(shù)據(jù)解析bs4庫使用BeautifulSoup方法示例

 更新時間:2023年08月21日 09:59:25   作者:YiYa_咿呀  
這篇文章主要為大家介紹了Python數(shù)據(jù)解析bs4庫使用BeautifulSoup方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

1. 安裝bs4庫

pip install bs4

2. 使用beautiful soup

用法如下:

find_all:find_all找到所有符合條件的節(jié)點

find:find指的是找第一個符合條件的節(jié)點

calss_:因為和python中的關鍵字class重合,因此在后面加個_加以區(qū)分

attrs={"":""}:attrs的對象存儲條件,此時的class無需加_

import requests
from bs4 import BeautifulSoup
import re
url = "http://www.crazyant.net/"
r = requests.get(url)
if r.status_code != 200:
    raise Exception()
html_doc = r.text
# 創(chuàng)建beautiful soup,將爬取的內(nèi)容通過BeautifulSoup解析,這里告訴BeautifulSoup這個是爬取到的html頁面,默認也是這個,但是會發(fā)出警告
soup = BeautifulSoup(html_doc,"html.parser")
# find_all找到所有符合條件的節(jié)點,find指的是找第一個
h2_nodes = soup.find_all("h2",class_="entry-title")

3. 使用bs4爬取優(yōu)美圖庫的圖片

from bs4 import BeautifulSoup
import requests
import time
url = "https://www.umei.cc/weimeitupian/oumeitupian/nvsheng.htm"
resp = requests.get(url)
resp.encoding = 'utf-8'
page = resp.text
soup = BeautifulSoup(page,'html.parser')
oAs = soup.find("div",class_='pic-list').find_all('a')
aLinks = []
for a in oAs:
    aLinks.append("https://www.umei.cc"+str(a.get("href")))
print(aLinks)
for link in aLinks:
    content = requests.get(link)
    content.encoding = 'utf-8'
    img = BeautifulSoup(content.text,'html.parser').find("div",class_='big-pic').find('img')
    src = img.get("src")
    print(img)
    print(src)
    img_name = src.split('/')[-1]
    img_resp = requests.get(src)
    with open('img/'+img_name,mode = "wb") as f:
        f.write(img_resp.content)
    time.sleep(1)
f.close()
resp.close()
img_resp.close()

結(jié)果:

以上就是Python數(shù)據(jù)解析bs4庫使用BeautifulSoup方法示例的詳細內(nèi)容,更多關于Python bs4 BeautifulSoup的資料請關注腳本之家其它相關文章!

相關文章

最新評論

黎平县| 申扎县| 五家渠市| 新丰县| 江永县| 柏乡县| 广饶县| 木里| 凭祥市| 八宿县| 巨鹿县| 简阳市| 奎屯市| 南投市| 攀枝花市| 浦城县| 潼南县| 万荣县| 镇江市| 晋城| 太原市| 广西| 嵊泗县| 清徐县| 大悟县| 固镇县| 黄浦区| 育儿| 南通市| 子洲县| 南康市| 翁源县| 旺苍县| 阳高县| 屏南县| 湄潭县| 城步| 娄底市| 蒙自县| 彰武县| 宁安市|