Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!