爱玺玺

爱玺玺的生活日记本。wx:lb87626

Python的BeautifulSoup理解,理解解析器

BeautifulSoup可以从html或xml中提取数据的Python库。

通过BeautifulSoup解析html可以得到一个BeautifuSoup对象。


获得一个BeautifulSoup对象:

from selenium import webdriver

from bs4 import BeautifulSoup as bs

driver = webdriver.PhantomJS()

driver.get("https://www.baidu.com/")

soup = bs(driver.page_source, "lxml")


通过这个soup对象,可以可以获得页面标签的数据,比如获取title

soup.title

soup.title.name

soup.title.string


理解解析器,比如lxml,html是python的默认解析器

什么是解析器,就是对html标签进行解释和分类,把这些标签整理成soup对象的属性




发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5