|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- >>> html_doc = """<html><head><title>睡鼠的故事</title></head>
- <body>
- <p class="title"><b>睡鼠的故事</b></p>
- <p class="story">从前有三位小姐姐,她们的名字是:
- <a href="http://example.com/elsie" class="sister" id="link1">埃尔西</a>,
- <a href="http://example.com/lacie" class="sister" id="link2">莱斯</a>和
- <a href="http://example.com/tillie" class="sister" id="link3">蒂尔莉</a>;
- 她们住在一个井底下面。</p>
- <p class="story">...</p>
- """
- >>> from bs4 import BeautifulSoup
- >>> soup = BeautifulSoup(html_doc, 'html.parser')
复制代码
看了小甲鱼老师的帮助文档,知道使用
- >>> soup.html.body.p
- <p class="title"><b>睡鼠的故事</b></p>
复制代码
得到body标签下第一个p标签的内容
请问有没有办法使用类似这样的代码
soup.html.body.p(class='story')
直接得到body标签下另一个p标签的内容?即返回
- <p class="story">从前有三位小姐姐,她们的名字是:
- <a href="http://example.com/elsie" class="sister" id="link1">埃尔西</a>,
- <a href="http://example.com/lacie" class="sister" id="link2">莱斯</a>和
- <a href="http://example.com/tillie" class="sister" id="link3">蒂尔莉</a>;
- 她们住在一个井底下面。</p>
复制代码
- soup = BeautifulSoup(html_doc, "lxml")
- all_link = soup.find_all("p", attrs={"class": "story"})
复制代码
|
|