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

个人网站建设与维护网站网络排名优化方法

个人网站建设与维护,网站网络排名优化方法,如何建立网站站点,免费网站建设有哪些开始之前导入html段落&#xff0c;同时下载好本节将用到的库。下载方式为&#xff1a;pip install beautifulsoup4 一点碎碎念&#xff1a;为什么install后面的不是bs4也不是BeautifulSoup&#xff1f; html_doc """ <html><head><title>The…

 开始之前导入html段落,同时下载好本节将用到的库。下载方式为:pip install beautifulsoup4

一点碎碎念:为什么install后面的不是bs4也不是BeautifulSoup?

html_doc = """
<html><head><title>The Dormouse's story</title></head><body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""

一、html基础与BeautifulSoup解析过程

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')

首先导入BeautifulSoup库并对html文档对象进行解析。

HTML解析器把这段字符串转换成一连串的事件:打开<html>标签,打开<head>标签,打开<title>标签,添加一段字符串,关闭<title>标签等等。

解析后的html文档大体上可分为四类对象:Tag(标签)即 <html> <title> 等,一个Tag可能包含多个字符串或其它的Tag,称为子节点,字符串节点没有子节点。还有NavigableString(标签内文字)、Comment(注释)以及BeautifulSoup(文档整体)。

BeautifulSoup对象本身也是一个特殊的Tag,可视为html标签,只有一个。可以非严格认为soup.html==soup。

二、BeautifulSoup对象方法

前情提要:没有索引到实际段落会返回None,前提是上个节点有索引,因为对None继续索引会报错。此处要结合后续代码理解。

1.子孙节点

print(soup.head)
# <head><title>The Dormouse's story</title></head>
print(soup.head.title)
# <title>The Dormouse's story</title>

首先是通过点取属性的方式获取相应的文档标签,此方式只能获取到匹配到的第一个tag,同时支持链式操作,在输出结果中会保留匹配到的tag(这里与以下contents等方法区分)。

print(soup.head.contents)
# [<title>The Dormouse's story</title>]
print(soup.head.contents[0].contents) 
# ["The Dormouse's story"]

contents方法用于获取某个tag内的所有(直接)子节点,返回列表,在输出结果中不会保留匹配到的tag。

for child in soup.children:print(child)
for child in soup.descendants:print(child)

children方法的作用与结果类似于contents,只不过返回的是生成器对象。descendants 方法用于获取某个tag内的所有子孙节点,同样返回生成器对象。children方法与descendants 方法均不会在输出结果中保留匹配到的tag。

到底什么叫“在输出结果中不保留匹配到的tag”呢?以下例子帮助理解。

for child in soup.head.descendants:print(child)print(child.name)
# <title>The Dormouse's story</title>
# title
# The Dormouse's story
# None

2.字符串节点

print(soup.head.string) 
# The Dormouse's story

如果tag有一个NavigableString类型子节点(重点是只有一个),那么这个tag可以使用string方法得到该子节点(即字符串)。

如果一个tag仅有一个子节点,该子节点只有一个NavigableString 类型子节点,那么这个tag也可以使用string方法直接获取到内部字符串,同理,文档树深度可无限大,但是直到NavigableString子节点结束时只有一条枝干(即宽度始终为1)才能调用。

for string in soup.strings:print(repr(string))
for string in soup.stripped_strings:print(repr(string))

如果tag内包含多个字符串可以使用strings或者stripped_strings方法获取,同样为生成器对象。其中stripped_strings方法会过滤掉字符串中的空白内容:全部是空格的行会被忽略掉,段首和段末的空白会被删除。

3.父节点

大部分tag或字符串都有父节点:被包含在某个tag中,此tag即为其父节点。

title_tag = soup.title
print(title_tag.parent)
# <head><title>The Dormouse's story</title></head>
print(title_tag.string.parent)
# <title>The Dormouse's story</title>

通过parent方法可获取到某个元素的父节点。

<html>的父节点是 BeautifulSoup 对象。以下为树结构辨析,帮助理解:

print(type(soup.html.parent.parent))
# <class 'NoneType'>
print(type(soup.html.parent))
# <class 'bs4.BeautifulSoup'>
print(type(soup.html))
# <class 'bs4.element.Tag'>
print(type(soup))
# <class 'bs4.BeautifulSoup'>

 通过parents方法可以递归得到元素的所有父辈节点,同样是生成器对象。

link = soup.a
for parent in link.parents:print(parent.name)
# p
# body
# html
# [document]
# 其中soup.name结果即为[document]

4.兄弟节点

ibling_soup = BeautifulSoup("<a><b>text1</b><c>text2</c></a>", 'html.parser')
print(sibling_soup.b.next_sibling)
# <c>text2</c>
print(sibling_soup.c.previous_sibling)
# <b>text1</b>
print(sibling_soup.b.previous_sibling)
# None
print(sibling_soup.c.next_sibling)
# None

兄弟节点定义:父节点相同的节点。next_sibling与previous_sibling分别表示“下一个兄弟节点”与“上一个兄弟节点”。从实际情况来说文档中tag的兄弟节点往往会出现字符串或者空白,这是由于解析html时标签之间的空白和换行也会被解析为字符串节点,其父节点与同级标签相同,符合兄弟节点定义。一般来说调用多次后总会得到想要的值,支持链式调用。

for sibling in soup.a.next_siblings:print(repr(sibling))
for sibling in soup.find(id="link3").previous_siblings:print(repr(sibling))

通过next_siblings和previous_siblings属性可以对当前节点的兄弟节点迭代输出,且同样是生成器对象。

last_a_tag = soup.find("a", id="link3")
'''
last_a_tag周围内容如下
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
'''
print(last_a_tag.next_sibling)
# ;
# and they lived at the bottom of a well.
print(last_a_tag.next_element)
# Tillie
# 解释 :解析器先进入<a>标签,然后是字符串“Tillie”,然后关闭</a>标签,然后是分号和剩余部分.所以next_element输出Tillie

next_element与previous_element类似于next_sibling与previous_sibling,不同的是前者是指向解析过程中下一个或上一个被解析的对象,这里需要了解BeautifulSoup的解析过程,前文已有提过不在赘述。

last_a_tag = soup.find("a", id="link3")
for element in last_a_tag.next_elements:print(repr(element))
# 'Tillie'
# ';\nand they lived at the bottom of a well.'
# '\n'
# <p class="story">...</p>
# '...'
# '\n'

next_elements和previous_elements为逐步解析,依旧是生成器对象。

http://www.dinnco.com/news/28013.html

相关文章:

  • 做网站的问题seo的优化技巧有哪些
  • 页面设计素材背景西安seo优化培训机构
  • 个人网站设计htmlseo营销工具
  • 网站权重优化线上线下推广方案
  • 怎么开发一个自己的网站东莞seo关键词
  • 对电子商务网站与建设的心得北京网络优化
  • 网站建设xiu021品牌运营公司
  • 济宁网站建设哪家好内容营销策略有哪些
  • 大陆怎么做香港网站百度关键词排名批量查询工具
  • 新手建站教程视频网站推广营销运营方式
  • 免费代刷网站推广新手怎么做网络推广
  • 网站制作平台公司外贸seo优化公司
  • 呼和浩特城乡建设委员会的网站百度公司网站推广怎么做
  • 网上商城网站建设模板建站代理
  • 无代码软件开发电商seo
  • 北京哪里做网站湖南网站seo找行者seo
  • 做淘宝网站怎么弄网络营销有哪些功能
  • 深圳建设很行住房公积金网站百度推广外包
  • 装修网站平台推荐app营销策略有哪些
  • 东阳厂家高端网站设计平台推广引流怎么做
  • 九口袋网站建设新媒体营销案例
  • 网站如何做流量网站推广的公司
  • 用什么软件做动漫视频网站百度推广上班怎么样
  • 网站从建设到上线流程cps游戏推广平台
  • 外国网站做vr百度登录个人中心
  • 深圳网站制作公司讯息厦门seo厦门起梦
  • 共青团中央网站自我建设网络推广员工资多少钱
  • 网站运营问题关键词全网指数查询
  • 西安专业做网站建设费用赣州seo排名
  • 下载的asp网页模板怎么应用到网站合肥网站建设公司