0
votes

besoin de trouver une valeur avec une belle soupe

Ceci fait partie du code HTML de la page suivante page suivante a >:

[<div class="sidebar-labeled-information" id="levelMission">
<span>Level:</span> <span>15</span>
</div>, <div class="sidebar-labeled-information" id="currRankText">
<span>Rank:</span>
<span>Colonel</span>
</div>, <div class="sidebar-labeled-information">
<span>Economic skill:</span>
<span>10.646</span>
</div>, <div class="sidebar-labeled-information">
<span>Strength:</span>
<span>2336</span>
</div>, <div class="sidebar-labeled-information">
<span>Location:</span>
<div>
<a href="region.html?id=454">Little Karoo<div class="xflagsSmall xflagsSmall-Argentina"></div>
</a>
</div>
</div>, <div class="sidebar-labeled-information">
<span>Citizenship:</span>
<div>
<div class="xflagsSmall xflagsSmall-Poland"></div>
<small><a href="pendingCitizenshipApplications.html">change</a>
</small>
</div>
</div>]

Je souhaite en extraire region.html? id = 454 . Je ne sais pas comment restreindre la recherche à , car il y a beaucoup de code> balises.

Voici le code python:

session=session()
r = session.get('https://orange.e-sim.org/battle.html?id=5377',headers=headers,verify=False) 
soup = BeautifulSoup(r.text, 'html.parser')
div = soup.find_all('div',attrs={'class':'sidebar-labeled-information'})

Et la sortie de ce code est:

<div>
 <div class="sidebar-labeled-information">
  <span>
   Economic skill:
  </span>
  <span>
   10.646
  </span>
 </div>
 <div class="sidebar-labeled-information">
  <span>
   Strength:
  </span>
  <span>
   2336
  </span>
 </div>
 <div class="sidebar-labeled-information">
  <span>
   Location:
  </span>
  <div>
   <a href="region.html?id=454">
    Little Karoo
    <div class="xflagsSmall xflagsSmall-Argentina">
    </div>
   </a>
  </div>
 </div>
 <div class="sidebar-labeled-information">
  <span>
   Citizenship:
  </span>
  <div>
   <div class="xflagsSmall xflagsSmall-Poland">
   </div>
   <small>
    <a href="pendingCitizenshipApplications.html">
     change
    </a>
   </small>
  </div>
 </div>
</div>


0 commentaires

3 Réponses :


0
votes
soup = BeautifulSoup(html)
links = soup.findAll('a', href=True)

for link in links:
  href = link['href']
  url = urlparse(href)
  if url.path == "region.html":
     print (url.path + "?" + url.query)
This prints region.html?id=454

0 commentaires

0
votes

vous pouvez essayer d'utiliser cette classe: xflagsSmall et trouvez le parent de cet élément

element=soup.find("div",{"class": "xflagsSmall"})
parent_element=element.find_parent()
link=parent_element.attrs["href"]```


2 commentaires

salut, xflagsSmall-Argentina n'est pas une valeur fixe, elle changera dans les nouvelles pages donc je ne peux pas l'utiliser. La solution consiste à restreindre la recherche à cette partie:

puis extrayez la valeur href .


@Abolfazl élargit la recherche pour qu'elle corresponde uniquement à xflagsSmall.



0
votes

Vous pouvez interroger sur la base de la valeur href:

element=soup.find("a",{"href": "region.html?id=454"})
element.attrs["href"]


0 commentaires