[英]how do I split parsed html output using python?
I have this html 我有这个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 我想分别获得“ 0070648261”和“ 3”,如ID = 0070648261和数量=3。我能够使用下面的代码
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 ? ID = 0070648261数量= 3?
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'
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]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.