从 BeautifulSoup 获取 class 数据
Getting class data from BeautifulSoup
我正在尝试使用 BeautifulSoup 从 HTML 页面获取 class 数据。
数据如下所示:
<div class="quoteText">
“I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best.”
<br> ―
<span class="authorOrTitle">
Marilyn Monroe
</span>
</div>
我只想要class"quoteText"下面的数据,不需要class"authorOrTitle"
里面的数据
下面的脚本returns作者的名字也是。
for div in soup.find('div', {'class': 'quoteText'}):
print(div)
如何在没有 "authorOrTitle" class 数据的情况下获取 "quoteText" class 数据?
谢谢!
试试这个,
from bs4 import BeautifulSoup
sample = """<div class="quoteText">
“I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best.”
<br> ―
<span class="authorOrTitle">
Marilyn Monroe
</span>
</div>
"""
soup = BeautifulSoup(sample, "html.parser")
print(soup.find('div', {'class': 'quoteText'}).contents[0].strip())
我正在尝试使用 BeautifulSoup 从 HTML 页面获取 class 数据。 数据如下所示:
<div class="quoteText">
“I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best.”
<br> ―
<span class="authorOrTitle">
Marilyn Monroe
</span>
</div>
我只想要class"quoteText"下面的数据,不需要class"authorOrTitle"
里面的数据下面的脚本returns作者的名字也是。
for div in soup.find('div', {'class': 'quoteText'}):
print(div)
如何在没有 "authorOrTitle" class 数据的情况下获取 "quoteText" class 数据?
谢谢!
试试这个,
from bs4 import BeautifulSoup
sample = """<div class="quoteText">
“I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best.”
<br> ―
<span class="authorOrTitle">
Marilyn Monroe
</span>
</div>
"""
soup = BeautifulSoup(sample, "html.parser")
print(soup.find('div', {'class': 'quoteText'}).contents[0].strip())