Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

215
Vistas
How to access 'parent' from BeautifulSoup using select or find?
import requests
from bs4 import BeautifulSoup


# raw = requests.get("https://www.daum.net")
# raw = requests.get("http://127.0.0.1:5000/emp")

response = requests.get("https://vip.mk.co.kr/newSt/rate/item_all.php?koskok=KOSPI&orderBy=upjong")
response.raise_for_status()
response.encoding= 'EUC-KR'
html = response.text
bs = BeautifulSoup(html, 'html.parser')

result = bs.select("tr .st2")

    <tr>
      <td width='92' class='st2'><a href="javascript:goPrice('000020&MSid=&msPortfolioID=')" title='000020'>somethinbg</a></td>
      <td width='60' align='right'>15,100</td>
      <td width='40' align='right'><span class='t_12_blue'>▼300</span></td>
    </tr>

I want to get datas from the someweher by using BeautifulSoup. But I should access parent Node where has . However, it's really hard to do it.

This is the code:

Then, how can I get datas from the parent which has the '<tr class ='st2>''

here is the example

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can access the parent element in BeautifulSoup via the element's parent attribute. Since you asked for a list of elements, this has to be done in an iteration.
I'm assuming here that you want to extract each row in the table.

import requests
from bs4 import BeautifulSoup

response = requests.get("https://vip.mk.co.kr/newSt/rate/item_all.php?koskok=KOSPI&orderBy=upjong")
response.raise_for_status()
response.encoding= 'EUC-KR'
html = response.text
bs = BeautifulSoup(html, 'html.parser')

result = [[child.text for child in elem.parent.findChildren('td', recursive=False)] \
    for elem in bs.select('tr .st2')] 

The result is:

[
   ['동화약품', '15,100', '▼300'], 
   ['유한양행', '61,100', '▼400'], 
   ['유한양행우', '58,900', '▲300'],
   ...
]
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda