python BeautifulSoup finding certain things in a table
Question: Folks, Ive managed to get beautifulsoup to scrape a page with the following

    
    
    html =  response.read()
    soup = BeautifulSoup(html)
    links = soup.findAll('a')
    

There are several occurrences of

    
    
    <A href="javascript:Set_Variables('foo1','bar1''')"onmouseover="javascript: return window.status=''">
    <A href="javascript:Set_Variables('foo2','bar2''')"onmouseover="javascript: return window.status=''">
    

How can I iterate through this and get the foo/bar values?

Thanks


Answer: You can use regular expressions to extract variables from `href` attributes:

    
    
    import re
    from bs4 import BeautifulSoup
    
    data = """
    <div>
        <table>
            <A href="javascript:Set_Variables('foo1','bar1''')" onmouseover="javascript: return window.status=''">
            <A href="javascript:Set_Variables('foo2','bar2''')" onmouseover="javascript: return window.status=''">
        </table>
    </div>
    """
    
    soup = BeautifulSoup(data)
    
    pattern = re.compile(r"javascript:Set_Variables\('(\w+)','(\w+)'")
    for a in soup('a'):
        match = pattern.search(a['href'])
        if match:
            print match.groups()
    

Prints:

    
    
    ('foo1', 'bar1')
    ('foo2', 'bar2')
    

