Extract string from HTML String

后端 未结 5 741
名媛妹妹
名媛妹妹 2021-01-26 04:26

i want to extract a number from a html string (i usually do not know the number).

The crucial part looks like this:



        
5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-26 05:06

    You can use string partitioning to extract a "number" string from the whole HTML string like this (assuming HTML code is in html_string variable):

    num_string=html_string.partition("TOTAL:")[2].partition("<")[0]

    there you get num_string with the number as a string, then simply convert it to an integer or whatever you want. Keep in mind that this will process the first occurence of anything that looks like "TOTAL: anything_goes_here <", so you want to make sure that this pattern is unique.

提交回复
热议问题