2

I have this html

<tr class="BgWhite"> 
 <td headers="th6" valign="top">
    0070648261<br/>QTY: 3
 </td>
</tr>

I want to obtain "0070648261" and "3" separately as in ID = 0070648261 and quantity = 3. I was able to use the code below

container1.find("td", {"headers": "th6"}).text.strip() 

to produce this output

   0070648261<br/>QTY: 3

but how do I split and the output to get

ID = 0070648261 quantity = 3 ?

2
  • Just split the string... Using split function Commented Sep 7, 2017 at 23:33
  • What HTML parsing library are you using? Commented Sep 7, 2017 at 23:34

2 Answers 2

1

Try this.

a="0070648261<br/>QTY: 3"
a=a.split("<br/>")
a="ID = "+a[0]+" quantity ="+a[1].split(':')[1]

Output:

'ID = 0070648261 quantity = 3'
Sign up to request clarification or add additional context in comments.

Comments

1

Why not to do that with regex?

import re
s = '<tr class="BgWhite"> <td headers="th6" valign="top">0070648261<br/>QTY: 3</td></tr>'

res = re.findall(r'(\d+)<br/>QTY: (\d+)', s)[0]
print('ID = {} quantity = {}'.format(res[0], res[1]))

2 Comments

it's sometimes appropriate to parse a limited, known set of HTML. - from the post you sent above
Right, but the question already parsed down to the inner tag, so while this solution works, you could also just use the string that was output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.