Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda