Spider__2__BeautifulSoup(2)__find_all

import re
# bs4  BeautifulSoup 可以解析 xml文件,jason文件,html文件
from bs4 import BeautifulSoup

#打开文件 并 放入内存
file = open("./baidu.html","rb")

html = file.read().decode("utf-8")
# 以 字节形式 打开并读取了 html 则  html的类型是 bytes 字节形式
# print("html: ",type(html))

# 在内存中 建立了 一个 树形 的 结构
bs = BeautifulSoup(html,"html.parser")


#bs.find_all : 字符串过滤 ( 会查找与字符串完全匹配的内容 )
# t_list = bs.find_all("a")
# print(t_list[0].string)
# print(t_list[0].attrs)

# 所有包含a的标签都会被选取
# t_list = bs.find_all(re.compile("a"))
# print(t_list)

# 正则表达式 搜索  使用 search() 方法 来匹配内容

# 也可以传入一个函数  根据函数的要求 来 访问
# def name_is_exists(tag):
#     return tag.has_attr("name")
#
# t_list = bs.find_all(name_is_exists)
# #
# # print(t_list)
#

# t_list = bs.find_all(id="news_banner_data")
# t_list = bs.find_all(href="http://news.baidu.com") # 有href属性 且 href 属性为 **** 的 给显示出来
# t_list = bs.find_all(class_=True)  # 有 class 属性的 给显示出来

# t_list = bs.find_all(text="贴吧")
# t_list = bs.find_all(text=["贴吧","地图"])
# t_list = bs.find_all(text=re.compile("\d"))

t_list = bs.find_all("a",limit=3) # 只要前三个 a

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值