bs4介绍遍历文档树/css选择器

find

##  其他用法

## find  本质就是find_all    find的参数,就是find_all的参数,但是find_all比find多
#recursive=True:是否递归查找,默认是True,如果写成false,只找第一层,  limit=None

# a=soup.find_all(name='html',recursive=False)
# 联合遍历文档树使用
# a=soup.html.p.find(name='b',recursive=False)
# print(a)


# limit=None  限制找几条
# a=soup.find_all(name='a',limit=1)
# print(a)

其他 

from bs4 import BeautifulSoup

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story<span>lqz</span></b><b>adfasdf<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>
"""

soup = BeautifulSoup(html_doc, 'lxml')
# print(soup.prettify())


# 遍历     搜索

###遍历文档树
# 遍历文档树:即直接通过标签名字选择,特点是选择速度快,但如果存在多个相同的标签则只返回第一个
# 1、用法   通过 .  遍历
# a=soup.html.body.a
# a=soup.a
# print(a)

# 2、获取标签的名称
# soup.a  对象
# a=soup.a.name
# print(a)

# 3、获取标签的属性
# a=soup.a.attrs
# a=soup.a.attrs.get('id')
# print(a)
# a=soup.a.attrs.get('href')
# print(a)


# 4、获取标签的内容---文本内容
# p=soup.p.text   # text 会把当前标签的子子孙孙的文本内容都拿出来,拼到一起
# print(p)
# s1=soup.p.string  #   当前标签有且只有自己(没有子标签),把文本内容拿出来
# print(s1)
# s1=list(soup.p.strings)  #generator 把子子孙孙的文本内容放到生成器中
# print(s1)

# 5、嵌套选择  . 完后可以继续再 .
# print(soup.head.title.text)



# # 6、子节点、子孙节点
# print(soup.p.contents)  # p下所有直接子节点
# print(list(soup.p.children))  # 得到一个迭代器,包含p下所有直接子节点
# print(list(soup.p.descendants))  #

# 7、父节点、祖先节点
# print(soup.a.parent) #获取a标签的父节点
# print(list(soup.a.parents)) #找到a标签所有的祖先节点,父亲的父亲,父亲的父亲的父亲...


# 8、兄弟节点
# print(soup.a.next_sibling) #下一个兄弟
# print(soup.a.previous_sibling) #上一个兄弟

# print(list(soup.a.next_siblings)) #下面的兄弟们=>生成器对象
# print(list(soup.a.previous_siblings)) #上面的兄弟们=>生成器对象

css选择器

from bs4 import BeautifulSoup

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b class ='baby'>The Dormouse's story<span>lqz</span></b><b>adfasdf<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" xx="xx">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" name="zzz">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

soup = BeautifulSoup(html_doc, 'lxml')
# css选择器

'''
div
.类名
#id号
div a  # div下的子子孙孙中得a
div>a  #div直接子节点
'''

# res=soup.select('.sister')
# res=soup.select('#link1')
# res=soup.p.find(name='b').select('span')
# print(res)




# 以后,基本所有的解析器都会支持两种解析:css,xpath,都可以去页面中复制

import requests

res=requests.get('http://it028.com/css-selectors.html')
# res.encoding=res.apparent_encoding
res.encoding='utf-8'
# print(res.text)
soup=BeautifulSoup(res.text,'lxml')
res=soup.select('#content > table > tbody > tr:nth-child(14) > td:nth-child(3)')
# //*[@id="content"]/table/tbody/tr[14]/td[3]
print(res)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑猪去兜风z1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值