BeautifulSoup的详细用法

# -*- coding:utf-8 -*-
from bs4 import  BeautifulSoup
'''
soup=BeautifulSoup(html,'lxml')   #创建一个对象
soup.title                       #打印标签中的所有内容
soup.title.text                  #打印标签中的文本内容   ==soup.title.string
soup.p.attrs                     #以字典的形式将标签中的属性输出
soup.p.attrs['href']              #获得标签中的属性值   ==soup.p.attrs.get('href')  ==soup.p.get('href')==soup.p['href']
soup.find_all('a')                 #查找所有的a标签并且将所有的a标签存储在列表中,可以遍历列表输出
soup.find('a')                     #只查找第一个a标签,只是一个
soup.find_all(['a','b'])            #查找所有的a和b标签,并且存储在列表中

soup.find_all('p',class_='title')     #查找属性是title的p    ==all_p=soup.find_all('p',{'class':'title'})

soup.find_all('p',class_=['title','main'])      #查找同时具备两个属性的p

get_text()                                     #获取文本内容


soup.find_all('div').find_all('a')                #在所有的div中查找a标签
等同于   all_div=soup.find_all('div')
         all_a=all_div.find_all('a')


soup.find_all('a',limit=2)                        #limit=2是限制的作用,用来限制返回的a标签最多两个
soup.find_all(text=".....")                         #查找所有文本内容是。。。。的标签


#在css中进行查找的方法
soup.select('p')                                  #查找所有的p标签   ==soup.find_all('p')

soup.select(".title")                            #通过类名进行查找

soup.select('#link1')                             #通过id名进行查找

all_p=soup.select('p[class="title"]')                    #属性进行查找

all_p[0].get_text()                              #获取文本内容









'''

html="""
<html><head><title>The Dormouse's story</title></head>
<body>
<div>
<p class="title" name="dromouse"><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>
</div>
"""


soup=BeautifulSoup(html,'lxml')   #创建一个对象
'''
print soup.prettify()   #格式化的输出
print soup.title  #输出标签中的全部内容
print soup.p
print soup.body

print soup.title.text
print soup.p.attrs
print soup.p.get('text')
print soup.p.attrs.get('name')
print soup.title.string


all_a=soup.find_all('a')
for i in all_a:
    print i.get("href")


a=soup.find('a')
print a.get('href')


all_a_p=soup.find_all(['a','p'])
for i in all_a_p:
    print i
all_p=soup.find_all('p',class_=['title','story'])
print all_p


all_p=soup.find_all('p',{'class':'title'})

all_div=soup.find('div')
print all_div
all_a=all_div.find_all('a')
for i in all_a:


all_a=soup.find_all('a',limit=4)
for i in all_a:



all_p=soup.select('p')
for i in all_p:
    print i

all=soup.select(".title")
print al


all_p=soup.select('p[class="title"]')
print all_p
print all_p[0].get_text()
'''

all_p=soup.find('p')
print all_p.get_text()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不才陈某

欢迎关注公众号【码猿技术专栏】

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

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

打赏作者

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

抵扣说明:

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

余额充值