How get specific element from a div with same id and class in Python
<p>I want to print "United state" and California in separated lines like</p>

<pre><code>Country is : United State
State is : California
</code></pre>

<p>The problem is each list item has the same class and id so when I loop through the list items it gives United States and California together.</p>

<p>I hope you guys will understand what I'm trying to say.</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;ul class="breadcrumbs" id="BREADCRUMBS"&gt;
  &lt;li class="breadcrumb_item " itemscope="" itemtype="http://data- vocabulary.org/Breadcrumb"&gt;
    &lt;a class="breadcrumb_link" href="/Tourism-g191-United_States-Vacations.html" itemprop="url" onclick="ta.setEvtCookie('Breadcrumbs', 'click', 'Country', 1, this.href); "&gt;
      &lt;span itemprop="title"&gt;United States&lt;/span&gt;
    &lt;/a&gt;
    &lt;span class="separator"&gt;âº&lt;/span&gt;
  &lt;/li&gt;
  &lt;li class="breadcrumb_item " itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb"&gt;
    &lt;a class="breadcrumb_link" href="/Tourism-g28926-California-Vacations.html" itemprop="url" onclick="ta.setEvtCookie('Breadcrumbs', 'click', 'State', 2, this.href); "&gt;
      &lt;span itemprop="title"&gt;California (CA)&lt;/span&gt;
    &lt;/a&gt;
    &lt;span class="separator"&gt;âº&lt;/span&gt;
  &lt;/li&gt;
  &lt;li class="breadcrumb_item " itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb"&gt;&lt;a class="breadcrumb_link" href="/Tourism-g32655-Los_Angeles_California-Vacations.html" itemprop="url" onclick="ta.setEvtCookie('Breadcrumbs', 'click', 'City', 3, this.href); "&gt;&lt;span itemprop="title"&gt;Los Angeles&lt;/span&gt;&lt;/a&gt;&lt;span class="separator"&gt;âº&lt;/span&gt;
  &lt;/li&gt;
  &lt;li class="breadcrumb_item " itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb"&gt;&lt;a class="breadcrumb_link" href="/Restaurants-g32655-Los_Angeles_California.html" itemprop="url" onclick="ta.setEvtCookie('Breadcrumbs', 'click', '', 4, this.href); return setOneTimeCookie('mcreset','true');"&gt;&lt;span itemprop="title"&gt;Los Angeles Restaurants&lt;/span&gt;&lt;/a&gt;
    &lt;span class="separator"&gt;âº&lt;/span&gt;
  &lt;/li&gt;
  &lt;li class="breadcrumb_item "&gt;Providence&lt;/li&gt;
&lt;/ul&gt;</code></pre>
</div>
</div>
</p>

<pre><code>here is my scraping script with python beautifulsoup
</code></pre>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>import sys,io,csv,requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup
url = "https://www.tripadvisor.com/Restaurant_Review-g32655-d594024-Reviews-Providence-Los_Angeles_California.html"
r = requests.get(url)
r.content
soup = BeautifulSoup(r.content, "html.parser")
maindiv = soup.find_all("body", {"class": "ltr domn_en_US lang_en globalNav2011_reset hr_tabs_placement_test tabs_below_meta scroll_tabs full_width_page content_blocks css_commerce_buttons flat_buttons sitewide xo_pin_user_review_to_top track_back"})
for div in maindiv:
	divone = soup.find_all("div", {"id": "PAGE"})
	for listitem in divone:
		div12 = soup.find_all("div", {"class": "breadCrumbBackground blue bgwhite "})
		for listitem in div12:
			ulpart = soup.find_all("ul", {"class": "breadcrumbs"})
			for unorder in ulpart[0]:
				div2 = soup.find_all("li", {"class": "breadcrumb_item "})
				for listitem in div2:
					tag = soup.find_all("a", {"class": "breadcrumb_link"})
					for spandiv in tag:
						span = soup.find_all("span", {"itemprop": "title"})
						for country_name in span:
							
							print(country_name.text)
				
				</code></pre>
</div>
</div>
</p>

<p>You have the relevant parts of <code>onclick</code> attribute defining what breadcrumb is country and which is state. I would use a partial match via <code>*=</code> <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors" rel="nofollow">CSS selector</a> to implement that:</p>

<pre><code># -*- coding: utf-8 -*-
from bs4 import BeautifulSoup

data = u"""Your HTML"""

soup = BeautifulSoup(data, "html.parser")

country = soup.select_one("li.breadcrumb_item a[onclick*=Country]").get_text(strip=True)
state = soup.select_one("li.breadcrumb_item a[onclick*=State]").get_text(strip=True)

print("The country is: '%s'" % country)
print("The state is: '%s'" % state)
</code></pre>

<p>Prints:</p>

<pre><code>The country is: 'United States'
The state is: 'California (CA)'
</code></pre>

python html5 python-3.x beautifulsoup