H
H
hey_umbrella2021-11-26 22:59:44
Python
hey_umbrella, 2021-11-26 22:59:44

How to take text from only one tag in bs4?

<math-input id="m7275895MI1" keyboard-skin="junior" keyboard-type="base" strict-check="false">
  <math-input-answer>2</math-input-answer>
  <math-input-answer>-2</math-input-answer>
</math-input> и 
<math-input id="m7275895MI2" keyboard-skin="junior" keyboard-type="base" strict-check="false">
  <math-input-answer>2</math-input-answer>
  <math-input-answer>-2</math-input-answer>
</math-input>.</p>


How can I take only one math-input-answer from math-input? Now my script outputs 2, -2 2, -2, and I need 2, -2 there is no difference from the first or second. I tried to change it to just find, but then it takes only the very first and skips the second math-input

for i in soup.find_all('math-input-answer'):
     results.append(i.text)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hey_umbrella, 2021-11-26
@hey_umbrella

I solved the problem like this, maybe someone needs it

for i in soup.find_all('math-input'):
            j = i.find('math-input-answer')
            results.append(j.text)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question