擅长:python、mysql、java
<p>您需要使用find\u all首先查找所有<em>路径的</em>,然后提取最后一个路径:</p>
<pre><code>h = """<g class="1581 sqw_sv5" style="cursor: pointer;">
<path d="M397.696,126.554C397.696,126.554,404.57504,140.2417375,404.57504,140.2417375" stroke="#ffffff" style="stroke-width: 3.6; stroke-opacity: 0.5; stroke-linecap: round; fill-opacity: 0;">
</path>
<path d="M397.696,126.554C397.696,126.554,404.57504,140.2417375,404.57504,140.2417375" stroke="#f95a0b" style="stroke-width: 1.2; stroke-linecap: round; fill-opacity: 0;">
</path>"""
soup = BeautifulSoup(h)
shots = soup.find_all('g')
for shot in shots:
print(shot.find_all("path", stroke=True)[-1]["stroke"]
</code></pre>
<p>使用<code>shot.path['stroke']</code>等同于使用<code>shot.find("path")['stroke']</code>,它只返回第一个路径。在</p>
<p>或者使用<em>nth类型的</em>也可以工作,具体取决于html的结构:</p>
^{pr2}$