Splitting HTML text by <br> while using beautifulsoup
Question: HTML code:

    
    
    <td> <label class="identifier">Speed (avg./max):</label> </td> <td class="value"> <span class="block">4.5 kn<br>7.1 kn</span> </td>
    

I need to get values 4.5 kn and 7.1 as separate list items so I could append
them separately. I do not want to split it I wanted to split the text string
using re.sub, but it does not work. I tried too use replace to replace br, but
it did not work. Can anybody provide any insight?

Python code:

    
    
      def NameSearch(shipLink, mmsi, shipName):
        from bs4 import BeautifulSoup
        import urllib2
        import csv
        import re
    
        values = []
        values.append(mmsi)
        values.append(shipName)
        regex = re.compile(r'[\n\r\t]')
        i = 0
        with open('Ship_indexname.csv', 'wb')as f:
            writer = csv.writer(f)
            while True:
                try:
                    shipPage = urllib2.urlopen(shipLink, timeout=5)
                except urllib2.URLError:
                    continue
                except:
                    continue
                break
            soup = BeautifulSoup(shipPage, "html.parser")  # Read the web page HTML
            #soup.find('br').replaceWith(' ')
            #for br in soup('br'):
                #br.extract()
            table = soup.find_all("table", {"id": "vessel-related"})  # Finds table with class table1
            for mytable in table:                                   #Loops tables with class table1
                table_body = mytable.find_all('tbody')                  #Finds tbody section in table
                for body in table_body:
                    rows = body.find_all('tr')                #Finds all rows
                    for tr in rows:                                 #Loops rows
                        cols = tr.find_all('td')                    #Finds the columns
                        for td in cols:                             #Loops the columns
                            checker = td.text.encode('ascii', 'ignore')
                            check = regex.sub('', checker)
                            if check == ' Speed (avg./max): ':
                                i = 1
                            elif i == 1:
                                print td.text
                                pat=re.compile('<br\s*/>')
                                print pat.sub(" ",td.text)
                                values.append(td.text.strip("\n").encode('utf-8'))  #Takes the second columns value and assigns it to a list called Values
                                i = 0
        #print values
        return values
    
    
    NameSearch('https://www.fleetmon.com/vessels/kind-of-magic_0_3478642/','230034570','KIND OF MAGIC')
    


Answer: Locate the "Speed (avg./max)" label first and then go to the value via
[`.find_next()`](http://www.crummy.com/software/BeautifulSoup/bs4/doc/#find-
all-next-and-find-next):

    
    
    from bs4 import BeautifulSoup   
    
    data = '<td> <label class="identifier">Speed (avg./max):</label> </td> <td class="value"> <span class="block">4.5 kn<br>7.1 kn</span> </td>'
    soup = BeautifulSoup(data, "html.parser")
    
    label = soup.find("label", class_="identifier", text="Speed (avg./max):")
    value = label.find_next("td", class_="value").get_text(strip=True)
    print(value)  # prints 4.5 kn7.1 kn
    

Now, you can extract the actual numbers from the string:

    
    
    import re
    
    speed_values = re.findall(r"([0-9.]+) kn", value)
    print(speed_values)
    

Prints `['4.5', '7.1']`.

You can then further convert the values to floats and unpack into separate
variables:

    
    
    avg_speed, max_speed = map(float, speed_values)
    

