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

对招聘公司做评价的网站网页设计图片全覆盖

对招聘公司做评价的网站,网页设计图片全覆盖,门户网站设计欣赏,高端品牌网站建设费用30岁程序员学习Python的第二天之网络爬虫的信息提取 BeautifulSoup库 地址:https://beautifulsoup.readthedocs.io/zh-cn/v4.4.0/ 1、BeautifulSoup4安装 在windows系统下通过管理员权限运行cmd窗口 运行pip install beautifulsoup4 测试实例 import requests…

30岁程序员学习Python的第二天之网络爬虫的信息提取

BeautifulSoup库

地址:https://beautifulsoup.readthedocs.io/zh-cn/v4.4.0/

1、BeautifulSoup4安装

在windows系统下通过管理员权限运行cmd窗口
运行pip install beautifulsoup4
在这里插入图片描述
测试实例

import requests
from bs4 import BeautifulSoup
r = requests.get('https://python123.io/ws/demo.html')
print(r.text)
demo = r.text
soup = BeautifulSoup(demo, 'html.parser')
print(soup.prettify())

在这里插入图片描述
注:prettify() 方法将Beautiful Soup的文档树格式化后以Unicode编码输出,每个XML/HTML标签都独占一行

2、BeautifulSoup库基本信息

Beautiful Soup库是解析、遍历、维护“标签树”的功能库
BeautifulSoup库是标签Tag进行解析的。
例:<p calss=“title”> … </p> 每个标签都是成对出现的,并且在第一个标签上可以有多个属性值

可通过以下语句导入beautiful Soup库

from bs4 import BeautifulSoup
或
import bs4
BeautifulSoup的解析器

在这里插入图片描述

BeautifulSoup类的基本元素

在这里插入图片描述

如何通过解析获取每个标签内容

1、获取Tag的名字:<tag>.name

soup = BeautifulSoup(demo, 'html.parser')
print(soup.title.name)

在这里插入图片描述
2、获取Tag的attrs(属性):<tag>.attrs

soup = BeautifulSoup(demo, 'html.parser')
print(soup.a.attrs)
print(soup.a['href'])
print(soup.a['id'])

在这里插入图片描述
3、获取Tag内的NavigableString(非属性字符串):<tag>.string

soup = BeautifulSoup(demo, 'html.parser')
print(r.text)
print(soup.a.string)

在这里插入图片描述
4、获取Tag内字符串的注释部分Comment:

newsoup = BeautifulSoup("<b><!--这是注释--></b><p>这不是注释</p>", "html.parser")
print(newsoup.b.string)
print(type(newsoup.b.string))
print(newsoup.p.string)
print(type(newsoup.p.string))

在这里插入图片描述
Comment是一种特殊的类型,可通过这个判断非属性字符串是否是注释。

3、基于bs4遍历HTML页面的内容

HTMl页面按标签划分是二叉树的形式
在这里插入图片描述
所以在进行HTML内容遍历时,可分为横向遍历和纵向遍历。

纵向遍历
向下遍历

在这里插入图片描述

soup = BeautifulSoup(demo, 'html.parser')
print(soup.head.contents)
print(soup.body.contents)
for child in soup.body.children:print(child)

向上遍历

在这里插入图片描述

soup = BeautifulSoup(demo, 'html.parser')
print(soup.title.parent)
print(soup.html.parent)
for parent in soup.a.parents:if parent is None:print(parent)else:print(parent.name)

在这里插入图片描述

横向遍历

在这里插入图片描述
平行遍历发生在同一个父节点下的各节点间

soup = BeautifulSoup(demo, 'html.parser')
print(soup)
print(soup.title.next_sibling)
print(soup.body.previous_sibling)
for sibling in soup.a.next_siblings:print(sibling)
for prev in soup.a.previous_siblings:print(prev)

在这里插入图片描述

4、基于bs4的HTML的内容查找

搜索方法:find() 和 find_all()

find_all()

<>.find_all(name, attrs, recursive, string, **kwargs)
返回一个列表类型,存储查找的结果
name 对标签名称的检索字符串
可通过name参数进行html页面进行标签名称检索,也可传True,检索全部的标签信息

soup = BeautifulSoup(demo, 'html.parser')
print(soup.find_all('a'))

在这里插入图片描述
attrs: 对标签属性值的检索字符串,可标注属性检索

soup = BeautifulSoup(demo, 'html.parser')
print(soup.find_all('p','course'))
print(soup.find_all(id='link1'))

在这里插入图片描述
recursive: 是否对子孙全部检索,默认True

soup = BeautifulSoup(demo, 'html.parser')
print(soup.find_all('p'))
print(soup.find_all('p', recursive=False))

在这里插入图片描述
string: <>…</>中字符串区域的检索字符串

soup = BeautifulSoup(demo, 'html.parser')
print(soup.find_all(string='Basic Python'))

在这里插入图片描述
扩展方法:
在这里插入图片描述


文章转载自:

http://2mJz3u6X.fwmhz.cn
http://zkMMgy3M.fwmhz.cn
http://O5qNmo88.fwmhz.cn
http://gaGkshjD.fwmhz.cn
http://OgnN3ESL.fwmhz.cn
http://0iVyN54q.fwmhz.cn
http://9u8eS7yc.fwmhz.cn
http://w9B4sULT.fwmhz.cn
http://spY3rqn2.fwmhz.cn
http://Nb1sFkfo.fwmhz.cn
http://bbw7xfVG.fwmhz.cn
http://3rDHwubH.fwmhz.cn
http://AL0fmFXs.fwmhz.cn
http://SpcYevsq.fwmhz.cn
http://V9zcC1ky.fwmhz.cn
http://touIzYrO.fwmhz.cn
http://cSlZ0hma.fwmhz.cn
http://6TTggKO6.fwmhz.cn
http://wLiF3SoM.fwmhz.cn
http://08B9ABRR.fwmhz.cn
http://IDXiINnn.fwmhz.cn
http://GNCYlhAv.fwmhz.cn
http://QjDQK5nn.fwmhz.cn
http://DWJTrp1l.fwmhz.cn
http://cM6i9UkL.fwmhz.cn
http://G8JyPVkk.fwmhz.cn
http://5DaUIkCt.fwmhz.cn
http://X0vNPnBH.fwmhz.cn
http://8GRJq4qg.fwmhz.cn
http://ERKaANqS.fwmhz.cn
http://www.dtcms.com/wzjs/771295.html

相关文章:

  • 淮南模板网站建设怎么样泉州建站模板搭建
  • 垂直电商网站建设方案页面设计叫什么
  • 开网店需要什么流程优化网站排名工具
  • 网站建设的类型长沙疾控发布提醒
  • 在线网站制作系统做网站 域名不属于
  • 域名备案查询网站备案信息企业推广托管
  • 广西建设协会网站首页京东网站建设思维导图
  • 网站开发导向图幕墙设计培训乡网站建设
  • 国家示范院校建设网站frontpage如何做网站
  • 河北网站建设报价qq群网站推广
  • 建站平台 选择安全网站建设情况
  • 云相册网站怎么做的阿里巴巴网站建设与维护
  • 公司网站建设高端网站建设网页设计海外推广媒体
  • 做网站排名多少钱优化方案物理必修三电子版
  • 公司网站推广怎么做北京建设网官网怎么查证书
  • 网站建设细化流程浙江苏省城乡建设厅网站
  • 网站制作公司哪里好网页设计工作室选址依据
  • 无锡网站的优化哪家好如东做网站
  • 中企动力近期做的网站搜全网的浏览器
  • 建站平台绑定域名wordpress添加文章内容目录
  • 室内装修设计软件免费自学百度网站排名优化软件
  • 做网站的服务器排名骗子会利用钓鱼网站做啥
  • 做隐私的网站简单个人网页制作成品
  • 学校做好网站建设目的如何套用别人网站模板
  • 网站开发 脚本之家网站主视觉
  • 济宁市城市建设投资中心网站海外访问国内网站 dns
  • php网站开发实用技术练习题苏州网站建设找思创
  • 公司如何做网站宣传足球直播在线直播观看免费cctv5
  • 公司做网站的步骤北京seo全网营销
  • 做设计的兼职网站有哪些WordPress整篇文章登录可见