网站建设的步骤图珠海网站推广

当前位置: 首页 > news >正文

网站建设的步骤图,珠海网站推广,学校网站建设的意见,wordpress在线安装目录 介绍 安装 初始化 解析器 使用方法 优势 Python标准库 lxml HTML lxml XML html5lib 格式化输出 对象 tag Name 多值属性 其他方法 NavigableString BeautifulSoup Comment 遍历 子节点 父节点 兄弟节点 回退和前进 搜索 过滤器 字符串 正则表达…目录 介绍 安装 初始化 解析器 使用方法 优势 Python标准库 lxml HTML lxml XML html5lib 格式化输出 对象 tag Name 多值属性 其他方法 NavigableString BeautifulSoup Comment 遍历 子节点 父节点 兄弟节点 回退和前进 搜索 过滤器 字符串 正则表达式 列表 方法 find 和 find_all 像调用find_all()一样调用tag 其他搜索方法 CSS选择器 介绍 bs4 全名 BeautifulSoup是编写 python 爬虫常用库之一主要用来解析 html 标签。 安装 pip install bs4 初始化 from bs4 import BeautifulSoupsoup BeautifulSoup(htmlA Html Text/html, html.parser)两个参数第一个参数是要解析的html文本第二个参数是使用那种解析器对于HTML来讲就是html.parser这个是bs4自带的解析器。 如果一段HTML或XML文档格式不正确的话那么在不同的解析器中返回的结果可能是不一样的。 解析器 使用方法 优势 Python标准库 BeautifulSoup(html, html.parser)1、Python的内置标准库2、执行速度适中3、文档容错能力强 lxml HTML BeautifulSoup(html, lxml)1、速度快2、文档容错能力强 lxml XML BeautifulSoup(html, [lxml, xml])BeautifulSoup(html, xml)1、速度快2、唯一支持XML的解析器 html5lib BeautifulSoup(html, html5lib)1、最好的容错性2、以浏览器的方式解析文档3、生成HTML5格式的文档 格式化输出 soup.prettify() # prettify 有括号和没括号都可以 对象 Beautfiful Soup将复杂HTML文档转换成一个复杂的树形结构每个节点都是Python对象所有对象可以归纳为4种tagNavigableStringBeautifulSoupComment。 tag Tag对象与 xml 或 html 原生文档中的 tag 相同。 soup BeautifulSoup(b classboldestExtremely bold/b)tag soup.btype(tag)# class bs4.element.Tag如果不存在则返回 None如果存在多个则返回第一个。 Name 每个 tag 都有自己的名字 tag.name

b

Attributestag 的属性是一个字典tag[class]

boldesttag.attrs

{class: boldest}type(tag.attrs)

class dict多值属性

最常见的多值属性是class多值属性的返回 list。 soup BeautifulSoup(p classbody strikeout/p)print(soup.p[class]) # [body, strikeout]print(soup.p.attrs) # {class: [body, strikeout]}如果某个属性看起来好像有多个值,但在任何版本的HTML定义中都没有被定义为多值属性那么Beautiful Soup会将这个属性作为字符串返回。 soup BeautifulSoup(p idmy id/p, html.parser) print(soup.p[id]) # my id Texttext 属性返回 tag 的所有字符串连成的字符串。 其他方法 tag.has_attr(id) # 返回 tag 是否包含 id 属性当然以上代码还可以写成 ‘id’ in tag.attrs之前说过tag 的属性是一个字典。顺便提一下has_key是老旧遗留的api为了支持2.2之前的代码留下的。Python3已经删除了该函数。 NavigableString 字符串常被包含在 tag 内Beautiful Soup 用 NavigableString 类来包装 tag 中的字符串。但是字符串中不能包含其他 tag。 soup BeautifulSoup(‘Extremely bold’) s soup.b.stringprint(s) # Extremely boldprint(type(s)) # class bs4.element.NavigableStringBeautifulSoup BeautifulSoup 对象表示的是一个文档的全部内容。大部分时候可以把它当作 Tag 对象。但是 BeautifulSoup 对象并不是真正的 HTM L或 XML 的 tag它没有attribute属性name 属性是一个值为“[document]”的特殊属性。 Comment Comment 一般表示文档的注释部分。 soup BeautifulSoup(b!–This is a comment–/b)comment soup.b.stringprint(comment) # This is a commentprint(type(comment)) # class bs4.element.Comment遍历 子节点 contents 属性 contents 属性返回所有子节点的列表包括 NavigableString 类型节点。如果节点当中有换行符会被当做是 NavigableString 类型节点而作为一个子节点。 NavigableString 类型节点没有 contents 属性因为没有子节点。 soup BeautifulSoup(div spantest/span /div )element soup.div.contentsprint(element) # [\n, spantest/span, \n]children 属性 children 属性跟 contents 属性基本一样只不过返回的不是子节点列表而是子节点的可迭代对象。 descendants 属性 descendants 属性返回 tag 的所有子孙节点。 string 属性 如果 tag 只有一个 NavigableString 类型子节点那么这个 tag 可以使用 .string 得到子节点。 如果一个 tag 仅有一个子节点那么这个 tag 也可以使用 .string 方法输出结果与当前唯一子节点的 .string 结果相同。 如果 tag 包含了多个子节点tag 就无法确定 .string 方法应该调用哪个子节点的内容, .string 的输出结果是 None。 soup BeautifulSoup(divpspanbtest/b/span/p /div )element soup.p.stringprint(element) # testprint(type(element)) # class bs4.element.NavigableString特别注意为了清楚显示一般我们会将 html 节点换行缩进显示而在BeautifulSoup 中会被认为是一个 NavigableString 类型子节点导致出错。上例中如果改成 element soup.div.string 就会出错。 strings 和 stripped_strings 属性如果 tag 中包含多个字符串可以用 strings 属性来获取。如果返回结果中要去除空行则可以用 stripped_strings 属性。 soup BeautifulSoup(divp /pptest 1/pptest 2/p /div , html.parser)element soup.div.stripped_stringsprint(list(element)) # [test 1, test 2]父节点 parent 属性 parent 属性返回某个元素tag、NavigableString的父节点文档的顶层节点的父节点是 BeautifulSoup 对象BeautifulSoup 对象的父节点是 None。 parent 属性递归得到元素的所有父辈节点包括 BeautifulSoup 对象。 兄弟节点 next_sibling 和 previous_sibling next_sibling 返回后一个兄弟节点previous_sibling 返回前一个兄弟节点。直接看个例子注意别被换行缩进搅了局。 soup BeautifulSoup(divptest 1/pbtest 2/bhtest 3/h/div , html.parser)print(soup.b.next_sibling) # htest 3/hprint(soup.b.previous_sibling) # ptest 1/pprint(soup.h.next_sibling) # Nonenext_siblings 和 previous_siblingsnext_siblings 返回后面的兄弟节点previous_siblings  返回前面的兄弟节点回退和前进 把html解析看成依次解析标签的一连串事件BeautifulSoup 提供了重现解析器初始化过程的方法。 next_element 属性指向解析过程中下一个被解析的对象tag 或 NavigableString。 previous_element 属性指向解析过程中前一个被解析的对象。 另外还有next_elements 和 previous_elements 属性不赘述了。 搜索 过滤器 介绍 find_all() 方法前先介绍一下过滤器的类型这些过滤器贯穿整个搜索的API。过滤器可以被用在tag的name中节点的属性中字符串中或他们的混合中。 示例使用的 html 文档如下 html
divp classtitlebThe Dormouses story/b/pp classstoryOnce upon a time there were three little sisters; and their names werea hrefhttp://example.com/elsie classsister idlink1Elsie/a,a hrefhttp://example.com/lacie classsister idlink2Lacie/a anda hrefhttp://example.com/tillie classsister idlink3Tillie/a/p /div soup BeautifulSoup(html, html.parser)字符串 查找所有的标签 soup.find_all(‘b’) # [The Dormouse’s story] 正则表达式 传入正则表达式作为参数返回满足正则表达式的标签。下面例子中找出所有以b开头的标签。 soup.find_all(re.compile(^b)) # [bThe Dormouses story/b]列表 传入列表参数将返回与列表中任一元素匹配的内容。下面例子中找出所有标签和标签。 soup.find_all([a, b]) TrueTrue可以匹配任何值下面的代码查找到所有的tag但是不会返回字符串节点。 soup.find_all(True)方法 如果没有合适过滤器那么还可以自定义一个方法方法只接受一个元素参数如果这个方法返回True表示当前元素匹配被找到。下面示例返回所有包含 class 属性但不包含 id 属性的标签。 def has_class_but_no_id(tag):return tag.has_attr(class) and not tag.has_attr(id)print(soup.find_all(has_class_but_no_id))返回结果 [p classtitlebThe Dormouses story/b/p, p classstoryOnce upon a time there were three little sisters; and their names werea classsister hrefhttp://example.com/elsie idlink1Elsie/a,a classsister hrefhttp://example.com/lacie idlink2Lacie/a anda classsister hrefhttp://example.com/tillie idlink3Tillie/a/p]这个结果乍一看不对标签含有 id 属性其实返回的 list 中只有2个元素都是 标签标签是 标签的子节点。 find 和 find_all 搜索当前 tag 的所有 tag 子节点并判断是否符合过滤器的条件 语法 find(nameNone, attrs{}, recursiveTrue, textNone, *kwargs)findall(nameNone, attrs{}, recursiveTrue, textNone, limitNone, **kwargs) 参数 name查找所有名字为 name 的 tag字符串对象会被自动忽略掉。上面过滤器示例中的参数都是 name 参数。当然其他参数中也可以使用过滤器。attrs按属性名和值查找。传入字典key 为属性名value 为属性值。recursive是否递归遍历所有子孙节点默认 True。text用于搜索字符串会找到 .string 方法与 text 参数值相符的tag通常配合正则表达式使用。也就是说虽然参数名是 text但实际上搜索的是 string 属性。limit限定返回列表的最大个数。kwargs如果一个指定名字的参数不是搜索内置的参数名搜索时会把该参数当作 tag 的属性来搜索。这里注意如果要按 class 属性搜索因为 class 是 python 的保留字需要写作 class。Tag 的有些属性在搜索中不能作为 kwargs 参数使用比如 html5 中的 data- 属性。 data_soup BeautifulSoup(div data-foovaluefoo!/div)print(data_soup.find_all(data-foovalue))# SyntaxError: keyword cant be an expression 但是可以通过 attrs 参数传递 data_soup BeautifulSoup(div data-foovaluefoo!/div)print(data_soup.findall(attrs{data-foo: value}))# [div data-foovaluefoo!/div]而按 class 查找时只要一个CSS类名满足即可如果写了多个CSS名称那么顺序必须一致而且不能跳跃。以下示例中前三个可以查找到元素后两个不可以。 css_soup BeautifulSoup(p classbody bold strikeout/p)print(css_soup.find_all(p, class_strikeout))print(css_soup.find_all(p, class_body))print(css_soup.find_all(p, class_body bold strikeout))# [p classbody strikeout/p]print(css_soup.find_all(p, class_body strikeout))print(css_soup.find_all(p, class_strikeout body))# []像调用find_all()一样调用tag find_all() 几乎是 BeautifulSoup 中最常用的搜索方法所以我们定义了它的简写方法。BeautifulSoup 对象和 tag 对象可以被当作一个方法来使用这个方法的执行结果与调用这个对象的 find_all() 方法相同下面两行代码是等价的: soup.find_all(b)soup(b)其他搜索方法 find_parents()      返回所有祖先节点find_parent()      返回直接父节点find_next_siblings()   返回后面所有的兄弟节点find_next_sibling()   返回后面的第一个兄弟节点find_previous_siblings() 返回前面所有的兄弟节点find_previous_sibling() 返回前面第一个兄弟节点find_all_next()     返回节点后所有符合条件的节点find_next()       返回节点后第一个符合条件的节点find_all_previous()   返回节点前所有符合条件的节点find_previous()     返回节点前所有符合条件的节点CSS选择器 BeautifulSoup支持大部分的CSS选择器这里直接用代码来演示。 from bs4 import BeautifulSouphtml
html headtitle标题/title/head bodyp classtitle namedromouseb标题/b/pdiv namedivlinkpa hrefhttp://example.com/1 classsister idlink1链接1/aa hrefhttp://example.com/2 classsister idlink2链接2/aa hrefhttp://example.com/3 classsister idlink3链接3/a/p/divp/pdiv namedv2/div /body /html soup BeautifulSoup(html, lxml)# 通过tag查找 print(soup.select(title)) # [title标题/title]# 通过tag逐层查找 print(soup.select(html head title)) # [title标题/title]# 通过class查找 print(soup.select(.sister))

[a classsister hrefhttp://example.com/1 idlink1链接1/a,

a classsister hrefhttp://example.com/2 idlink2链接2/a,

a classsister hrefhttp://example.com/3 idlink3链接3/a]# 通过id查找

print(soup.select(#link1, #link2))

[a classsister hrefhttp://example.com/1 idlink1链接1/a,

a classsister hrefhttp://example.com/2 idlink2链接2/a]# 组合查找

print(soup.select(p #link1))    # [a classsister hrefhttp://example.com/1 idlink1链接1/a]# 查找直接子标签 print(soup.select(head title))  # [title标题/title]print(soup.select(p #link1))   # [a classsister hrefhttp://example.com/1 idlink1链接1/a]print(soup.select(p a:nth-of-type(2)))  # [a classsister hrefhttp://example.com/2 idlink2链接2/a]

nth-of-type 是CSS选择器# 查找兄弟节点向后查找

print(soup.select(#link1 ~ .sister))

[a classsister hrefhttp://example.com/2 idlink2链接2/a,

a classsister hrefhttp://example.com/3 idlink3链接3/a]print(soup.select(#link1 .sister))

[a classsister hrefhttp://example.com/2 idlink2链接2/a]# 通过属性查找

print(soup.select(a[hrefhttp://example.com/1]))# ^ 以XX开头 print(soup.select(a[href^http://example.com/]))# * 包含 print(soup.select(a[href*.com/]))# 查找包含指定属性的标签 print(soup.select([name]))# 查找第一个元素 print(soup.select_one(.sister))