<br> Tag parsing using python and beautifulsoup
Question: So I am trying to golf course extract data from a given website in which it
will create a CSV that contains the name and address. For the address though
the website where I am taking the data from has  
tag breaking it apart. Is it possible to parse out the two address which is
break apart by a  
into two separate columns?

SO it it looks like this on the HTML

    
    
    <div class="location">10799 E 550 S<br>Zionsville, Indiana, United States</div>
    

I want that it will be broken into

    
    
    Column1:10799 E 550 S
    Column2:Zionsville, Indiana, United States
    

Here is my code:

    
    
    import csv
    import requests
    from bs4 import BeautifulSoup
    
    courses_list = []
    
    with open('Garmin_GC.csv', 'w') as file:
        writer = csv.writer(file)
        for i in range(3):  #893
            url = "http://sites.garmin.com/clsearch/courses/search?course=&location=&country=US&state=&holes=&radius=&lang=en&search_submitted=1&per_page={}".format(
                i * 20)
            r = requests.get(url)
            soup = BeautifulSoup(r.text)
            g_data2 = soup.find_all("div", {"class": "result"})
            for item in g_data2:
                try:
                    name = item.find_all("div", {"class": "name"})[0].text
                except IndexError:
                    name = ''
                    print "No Name found!"
                try:    
                    address = item.find_all("div", {"class": "location"})[0].get_text(separator=' ')
                    print address
                except IndexError:
                    address = ''
                    print "No Address found!"
                writer.writerow([name.encode("utf-8"), address.encode("utf-8")])
    


Answer: Use the [`.stripped_strings`
generator](http://www.crummy.com/software/BeautifulSoup/bs4/doc/):

    
    
    address = list(item.find('div', class_='location').stripped_strings)
    

This'll produce a list of two strings:

    
    
    >>> from bs4 import BeautifulSoup
    >>> markup = '''<div class="location">10799 E 550 S<br>Zionsville, Indiana, United States</div>'''
    >>> soup = BeautifulSoup(markup)
    >>> list(soup.find('div', class_='location').stripped_strings)
    [u'10799 E 550 S', u'Zionsville, Indiana, United States']
    

Putting that in the context of your code:

    
    
    try:
        name = item.find('div', class_='name').text
    except AttributeError:
        name = u''
    try:
        address = list(item.find('div', class_='location').stripped_strings)
    except AttributeError:
        address = [u'', u'']
    writer.writerow([v.encode("utf-8") for v in [name] + address])
    

where the two address values are written to two separate columns.

