Using BeautifulSoup with multiple tags with the same name
<p>I have the below html</p>

<pre><code>&lt;g class="1581 sqw_sv5" style="cursor: pointer;"&gt;
 &lt;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;"&gt;
 &lt;/path&gt;
 &lt;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;"&gt;
 &lt;/path&gt;
</code></pre>

<p>I need to obtain the value of the 'stroke' in the second <em>path</em>. My current code is only pulling the value from the first <em>path</em>.</p>

<p>I am currently using</p>

<pre><code>shots = soup.find_all('g')
for shot in shots:
    print(shot.path['stroke'])
</code></pre>

<p>which returns #ffffff. I need it to return #f95a0b</p>

<p>You need to use find_all to first find all the <em>path's</em> and then extract the last one:</p>

<pre><code>h = """&lt;g class="1581 sqw_sv5" style="cursor: pointer;"&gt;
 &lt;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;"&gt;
 &lt;/path&gt;
 &lt;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;"&gt;
 &lt;/path&gt;"""
soup = BeautifulSoup(h)
shots = soup.find_all('g')
for shot in shots:
    print(shot.find_all("path", stroke=True)[-1]["stroke"]
</code></pre>

<p>Using <code>shot.path['stroke']</code> is equivalent to using <code>shot.find("path")['stroke']</code> which would only return the first path.</p>

<p>Or using <em>nth-of-type</em> may also work depending on the structure of the html:</p>

<pre><code>soup = BeautifulSoup(h)
shots = soup.find_all('g')
for shot in shots:
    print(shot.select_one("path:nth-of-type(2)")["stroke"])
</code></pre>

python beautifulsoup