【Python爬虫学习】二、BeautifulSoup

BeautifulSoup库(bs4)是解析、遍历、维护变签树的功能库

  •  BeautifulSoup测试运行
import requests
from bs4 import BeautifulSoup

url = "http://python123.io/ws/demo.html"
r = requests.get(url)
demo = r.text

soup = BeautifulSoup(demo,"html.parser")#为demo指定html的解析器
print(soup.prettify())
  • BeautifulSoup属性 

BeautifulSoup遍历 

  • BeautifulSoup下行遍历

contents返回列表类型

children和descendants返回迭代类型,只能用于 for循环

#标签树的下行遍历
#儿子节点
for child in soup.body.children:
    print(child)

#儿孙节点
for child in soup.body.descendants:
    print(child)
  • BeautifulSoup上行遍历 

#标签树的上行遍历
#父亲节点
soup.title.parent

#父祖节点
for parent in soup.a.parents:
    if parent is None:
        print(parent)
    else:
        print(parent.name)
  •  BeautifulSoup平行遍历 

平行遍历条件:

#标签树的平行遍历
soup.a.next_sibling

for sibling in soup.a.next_siblings:
    print(sibling)
    

基于BS4 库的HTML内容查找方法

find_all

find扩展方法

  •  提取所有链接
#提取所有链接
for link in soup.find_all('a'):
    print(link.get("href"))
  • 打印所有标签名称 
#打印所有标签名称
for tag in soup.find_all(True):
    print(tag.name)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值