擅长:python、mysql、java
<p><code>text</code>参数在这种特殊情况下不起作用。这与如何计算元素的<a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#string" rel="nofollow">^{<cd2>} property</a>有关。相反,我将使用<a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#a-function" rel="nofollow">search function</a>,在这里您可以实际调用<code>get_text()</code>并检查包含子节点的元素的完整“文本”:</p>
<pre><code>label = thesoup.find(lambda tag: tag and tag.name == "th" and \
"Residential" in tag.get_text())
comres = label.find_next("td").get_text()
print(str(comres))
</code></pre>
<p>打印<code>Commercial</code>。在</p>
<p>我们可以更进一步,创建一个<em>可重用函数</em>以通过标签获得值:</p>
^{pr2}$
<p>印刷品:</p>
<pre><code>Commercial
NYC
</code></pre>