[英]Extracting text from span class tag with beautifulsoup
I am trying to extract some elements of text between a span class from a website. 我试图从网站的跨度类之间提取文本的某些元素。
Here is a snippet of the HTML code: 这是HTML代码的片段:
<h1>2 Some address</h1>
</div>
<div id="smi-summary-items">
<div id="smi-price-string">€230,000</div>
<span class="header_text"> Detached House</span><span class="bar"> | </span><span class="header_text">3 Beds</span><span class="bar"> | </span><span class="header_text">2 Baths</span>
<!-- Text_Link_Full_Ad_Unit -->
<div id='dfp-text_link_full_ad_unit' class='sale'>
<script type='text/javascript'>
googletag.cmd.push(function()
{
googletag.display('dfp-text_link_full_ad_unit');
}
);
</script>
</div>
I would like to extract the text of "3 Beds" and "2 Baths". 我想提取“ 3床”和“ 2浴”的文本。
I've tried a few solutions but mainly getting errors or an empty result. 我尝试了一些解决方案,但主要遇到错误或结果为空。
Can anyone suggest a solution? 谁能提出解决方案?
From what I understand, you can simply filter the desired elements by class: 据我了解,您可以按类简单过滤所需的元素:
[item.get_text() for item in soup.select("span.header_text")]
The complete working sample code: 完整的工作示例代码:
from bs4 import BeautifulSoup
data = """
<div id="smi-summary-items">
<div id="smi-price-string">€230,000</div>
<span class="header_text"> Detached House</span><span class="bar"> | </span><span class="header_text">3 Beds</span><span class="bar"> | </span><span class="header_text">2 Baths</span>
<!-- Text_Link_Full_Ad_Unit -->
<div id='dfp-text_link_full_ad_unit' class='sale'>
<script type='text/javascript'>
googletag.cmd.push(function()
{
googletag.display('dfp-text_link_full_ad_unit');
}
);
</script>
</div>"""
soup = BeautifulSoup(data, "html.parser")
print([item.get_text(strip=True) for item in soup.select("span.header_text")])
That produces: 产生:
['Detached House', '3 Beds', '2 Baths']
The following code works for extracting some elements of text between a span class from a website 以下代码用于从网站中提取跨度类之间的某些文本元素
>>> from bs4 import BeautifulSoup
>>> import re
>>> content = """<h1>2 Some address</h1>
... </div>
... <div id="smi-summary-items">
... <div id="smi-price-string">€230,000</div>
... <span class="header_text"> Detached House</span>
<span class="bar"> | </span><span class="header_text">3
Beds</span><span class="bar"> | </span><span class="header_text">2
Baths</span>
... <!-- Text_Link_Full_Ad_Unit -->
... <div id='dfp-text_link_full_ad_unit' class='sale'>
... <script type='text/javascript'>
... googletag.cmd.push(function()
... {
... googletag.display('dfp-
text_link_full_ad_unit');
... }
... );
... </script>
... </div>"""
>>> soup = BeautifulSoup(content, "html.parser")
>>> req = soup.find_all("span", {"class":"header_text"})
>>> print(req)
[<span class="header_text"> Detached House</span>, <span
class="header_text">3 Beds</span>, <span class="header_text">2 Baths</span>]
>>> x23 = []
>>> for i in req:
... x23.append(i.get_text())
...
>>> print(x23)
[' Detached House', '3 Beds', '2 Baths']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.