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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 爬虫-beautifulsoup4

發布時間:2023/12/10 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 爬虫-beautifulsoup4 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

利用它可以不用編寫正則表達式即可方便的實現網頁信息的提取。

pip3 install beautifulsoup4

用法講解:
常用解析庫:

lxml HTML解析器?

lxml XML解析器

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

print(soup.prettify())? #格式化代碼

print(soup.title.string)

------------------------------------------------------------------

#標簽選擇器:

##選擇元素:

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

print(soup.title)

print(type(soup.title))

print(soup.head)

print(soup.p)

如果有很多滿足條件,只會輸出第一個。

##獲取名稱:

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

print(soup.title.name)

##獲取屬性:

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

print(soup.p.attrs['name'])#獲取name屬性的值

print(soup.p['name'])

##獲取內容:

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

print(soup.p.string)

##嵌套選擇:

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

print(soup.head.title.string)


##1、子節點和子孫節點:

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

print(soup.p.children)#迭代器

for i,child in enumerate(soup.p.children):

? ? print(i,child)

##2

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

print(soup.p.contents)#獲取p內的所有內容,列表

##3

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

print(soup.p.descendants) #迭代器

for i,child in enumerate(soup.p.descendants):

? ? print(i,child)

##1、父節點和祖先節點:

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

print(soup.p.parent) #迭代器

##2

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

print(enumerate(list(soup.p.parents))) #迭代器


##兄弟節點

from bst import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

print(list(enumerate(soup.a.next_siblings)))

print(list(enumerate(soup.a.privious_siblings)))

------------------------------------------------------------------

#標準選擇器

find_all(name,attrs,recursive,text,**kwargs)

可根據標簽名、屬性、內容查找文檔

##name

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'html')

print(soup.find_all(''ul))

print(type(soup.find_all('ul')[0]))


from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

for ul in soup.find_all('ul'):

????print(ul.find_all('li'))


##attrs

from bs4? import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

print(soup.find_all(attrs={'id':'list-1'}))

print(soup.find_all(attrs = {'name':'element'}))

print(soup.find_all(id = 'list-1'))

print(soup.find_all(class_='element')) ##以上四句的輸出一樣


##text

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

print(soup.find_all(text='Foo'))


#find(name,attrs,recursive,text,**kwargs)

返回單個元素.

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

print(soup.find('ul'))

print(type(soup.find('ul')))

print(soup.find('page'))


#find_parents(),find_parent()

find_next_siblings() find_next_sibling()

find_all_next()? find_next()

find_all_previous() find_previous()


#CSS選擇器??

通過select()直接傳入CSS選擇器即可完成選擇

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

pirnt(soup.select('.panel.panel-heading'))# class用.

print(soup.select('ul li'))

print(soup.select('#list-2.element'))

print(type(soup.select('ul')[0]))#


from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

for ul in soup.select('ul'):

? ? print(ul.select('li'))

##獲取屬性

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

for ul in soup.select('ul'):

? ? print(u['id'])

? ? print(ul.attrs['id'])

##獲取內容

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,'lxml')

for ul in soup.select('li'):

? ? print(li.get_text())


#標簽選擇器

#標準選擇器

#CSS選擇器

總結:

?* 推薦使用lxml解析庫,必要使用html.parser

?* 標簽選擇篩選功能弱但是速度快。

?* 建議使用find(),find_all()查詢匹配單個結果或者多個結果。

?* 如果對CSS選擇器熟悉可以使用select()

?* 記住常用的獲取屬性和文本值的方法。

總結

以上是生活随笔為你收集整理的python 爬虫-beautifulsoup4的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。