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

自助免费建网站百度排名优化

自助免费建网站,百度排名优化,如何查找网站建设时间,photoshop怎么修改图片上的文字本篇将会介绍beutifulsoup4模块,可以用于网络爬虫、解析HTML和XML,对于没有接触过前端,不了解HTML是如何工作的,需要先解释一下什么事HTML。 1. HTML 网页中的各种布局等的背后都是非常简单的纯文本格式,那种格式称为…

本篇将会介绍beutifulsoup4模块,可以用于网络爬虫、解析HTML和XML,对于没有接触过前端,不了解HTML是如何工作的,需要先解释一下什么事HTML。

1. HTML

网页中的各种布局等的背后都是非常简单的纯文本格式,那种格式称为HTML
关于HTML不用刻意的去学习,所谓的HTML就是一堆<>括起来的符合或单词,不同的单词就是标签,其对应了不同的作用。

如果在网络上进行通信,获取网页,实际上不会得到我们打开的网页的界面,得到的就是html的代码,而我们关心的可能就是HTML中的一部内容,就需要对HTTML也就是字符串进行解析,找出我们需要的部分。通过python的字符串来进行处理也是可行的,但是考虑到处理的效率,也有相应的开发的模块。

2. 安装bs4

pip install beutifulsoup4

官网文档(中文版):
https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/

3. 使用BeautifulSoup解析HTML实例

使用的HTML代码如下:来自于官方文档中的范例:ap均为标签

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>
"""

将其拷贝到一个txt文件,改后缀为html,利用浏览器打开就是一个网页如下:
在这里插入图片描述

  • bs4中提供了BeautifulSoup的方法,它可以将html字符串,转化为一个soup对象。
  • soup对象中提供了各种属性方法,对应了htm文档,使得我们可以很方便地提取相关信息

以下演示如何进行安装、导入模块、进行HTML的缩进美化

C:\Users\>pip install beautifulsoup4
C:\Users\>ipython
In [1]: from bs4 import BeautifulSoup
In [2]: 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>...: """In [3]: soup = BeautifulSoup(html_doc, 'html.parser') #转变为soup对象In [4]: print(soup.prettify()) #把原有HTML源码进行缩进美化
<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 class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>and<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p><p class="story">...</p></body>
</html>

构造得到的soup对象中提供了各种操作的方法。

find_all:找到所有的标签,返回一个list,list中的每个元素,是标签对象。

In [5]: soup.find_all("a")
Out[5]:
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]In [6]: for i in soup.find_all("a"):...:     print(i)...:
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>In [7]: mylist = soup.find_all("a")In [8]: tag0 = mylist[0]In [9]: tag0
Out[9]: <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>In [10]: tag0['href'] #标签类似dict的封装,得到href的value
Out[10]: 'http://example.com/elsie'
In [11]: for item in mylist:...:     print(item["href"])...:
http://example.com/elsie
http://example.com/lacie
http://example.com/tillie

4.学习视频地址:使用python解析网页HTML

http://www.ds6.com.cn/news/57534.html

相关文章:

  • 制作b2c网站多少钱长春网站建设定制
  • 网站怎么优化关键词搜索引擎外部优化有哪些渠道
  • 企业网站管理系统 免费赛雷猴是什么意思
  • b2c商城网站运营策划方案网站搜索优化公司
  • wordpress js调用合肥网站seo公司
  • 网站锚点怎么做成都百度推广联系方式
  • 网址建站厦门百度推广开户
  • html5 视频网站 模板抖音关键词排名优化
  • 新开最好的传奇网站什么是网络营销策划
  • 一般在百度做网站多少钱谷歌搜索引擎怎么才能用
  • 网站开发的过程步骤阿里大数据平台
  • qq群推广网站免费秒进自动app优化下载
  • 济南网站建设 历山北路高端网站建设报价
  • 云南曲靖疫情最新情况江苏seo外包
  • 保定市做网站流量主广告点击自助平台
  • php动态网站开发 模版seo技术蜘蛛屯
  • 南宁老牌网站建设公司市场营销
  • 北京大型网站优化站长统计app
  • linux系统网站架构网站查询访问
  • 无锡网站建设哪家做的比较好北京网站建设公司案例
  • 做流量网站要做哪一种网络运营主要做什么工作
  • 自适应网站做多大尺寸的地推的方法和技巧
  • 青岛专业网站制作设计企业seo推广外包
  • 加强心理咨询网站的建设方案深圳平台推广
  • 网站建设具体步骤百度网页版链接
  • 宝鸡外贸网站建设关键词排名监控
  • 三创大赛网站建设百度云盘登录入口
  • 广州哪里有做网站谷歌seo是什么
  • 怀化网站优化多少钱黑帽seo优化软件
  • 做网站设计好的公司百度关键词搜索排名多少钱