python - Extracting a specific list item using Beautiful Soup -


i working on web scraper project using python , beautiful soup. please have @ html code section having trouble with. since <li> items in <ul> have same class names <div> , <span>, how can extract phone number i.e value of second <span> in third <li> item?

i can extract <ul> tag , contents using ad_soup.find("ul",{"class":"menu"}), not know how proceed after that. highly appreciated.

<ul class="menu">   <li>     <div class="item">       <span class="name">name:</span>       <span class="value">....</span>     </div>   </li>   <li>     <div class="item">       <span class="name">location:</span>       <span class="value">....</span>     </div>   </li>   <li>     <div class="item">       <span class="name">phone:</span>       <span class="value">....</span>     </div>   </li> </ul> 

if know phone third element, following should help:

(ad_soup.find("ul",{"class":"menu"}).          find_all("li")[2].find("span", {"class": "value"})) 

if don't know phone third element, can loop on li , select needed one:

[li.find("span", {"class": "value"})   li in ad_soup.find("ul",{"class": "menu"}).find_all("li")  if li.find("span", {"class": "name"}).string == "phone:"] 

Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -