BeautifulSopu

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

from bs4 import BeautifulSopu
soup = BeautifulSoup(html_doc, 'html.parser')

print(soup.prettify())



<br> The Dormouse's story<br>




The Dormouse's story



Once upon a time there were three little sisters; and their names were

Elsie

,

Lacie

and

Tillie

;
and they lived at the bottom of a well.



...



soup.a

Elsie

soup.find_all("a")

[Elsie,
Lacie,
Tillie]

soup.findAll("a")

[Elsie,
Lacie,
Tillie]

#在文档中找到所有<a>标签的链接
for link in soup.find_all('a'):
    print(link.get('href'))
    

http://example.com/elsie
http://example.com/lacie
http://example.com/tillie

#从文档中获取所有文字内容:
print(soup.get_text())

The Dormouse's story

The Dormouse's story
Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.
...

#通过正则表达式的search()来匹配内容
import re
for tag in soup.find_all(re.compile("^b")):
    print(tag.name)

body
b

#如果传入列表参数,BS会将列表中任一元素匹配的内容返回
soup.find_all(["a",'b'])

[The Dormouse's story,
Elsie,
Lacie,
Tillie]

def has_class_but_no_id(tag):
    return tag.has_attr('class') and not tag.has_attr('id')
#将这个方法作为参数传入find_all()方法,将得到所有<p>标签
soup.find_all(has_class_but_no_id)

[

The Dormouse's story

,

Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.

,

...

]

soup.find_all("p")

[

The Dormouse's story

,

Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.

,

...

]

soup.find_all("p","title")

#find_all(name,attrs,recursive,string,**kwargs)

[

The Dormouse's story

]

按CSS搜索

soup.find_all("a",class_="sister")

[Elsie,
Lacie,
Tillie]

CSS选择器

soup.select("title")

[The Dormouse's story]

soup.select("body a")

[Elsie,
Lacie,
Tillie]

soup.select("head>title")

[The Dormouse's story]

soup.select("p>a")

[Elsie,
Lacie,
Tillie]

soup.select("p>a:nth-of-type(2)")

[Lacie]

soup.select("p> #link1")

[Elsie]

#通过css的类名查找
soup.select(".sister")

[Elsie,
Lacie,
Tillie]

#通过tag的id查找
soup.select("a#link2")

[Lacie]

#get_text()
markup = '<a href="http://example.com/">\nI linked to <i>example.com</i>\n</a>'
soup = BeautifulSoup(markup)

print(soup.get_text("|",strip=True))
print(soup.i.get_text())

#或使用.stripped_strings生成器,并获得文本列表后手动处理列表:
[text for text in soup.stripped_strings]

I linked to|example.com
example.com

['I linked to', 'example.com']

解析器之间的区别

from lxml import etree

root = etree.Element("root")
print(root.tag)

root

root.append(etree.Element("child1"))
child2=etree.SubElement(root,"child2")
child3=etree.SubElement(root,"child3")

print(etree.tostring(root, pretty_print=True))

b'\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n'

#Elements are lists
child = root[0]
print(child.tag)
print(len(root))

child1
14

for child in root:
    print(child.tag)

child1
child1
child1
child2
child3
child1
child2
child3
child1
child2
child3
child1
child2
child3

posted @ 2022-06-01 17:51  渐远的围城  阅读(46)  评论(0)    收藏  举报