Extracting href ID from block of code using Python
Question: From the following code, I want to target the href and collect the ID number
at the end of the link, setting that number as a variable named "VARIABLE" (in
this block of code, the number would be "2019786"). I've used requests to
collect the page in my program and I also have imported BeautifulSoup4. Also,
this block of code could change from page to page, thus changing the href ID
number, so I was thinking when collecting this number, use something like:

/d2l/lp/ouHome/home.d2l?ou=%

    
    
    <a class="d2l-link d2l-outline d2l-left" href="/d2l/lp/ouHome/home.d2l?ou=2019786" title="Enter CHEM-1315-018 - Lab-CHEM 1315-001">CHEM-1315-018 - Lab-CHEM 1315-001</a>
    

Thanks in advance, Jackson


Answer: You can use BeautifulSoup and a simple regex to get the id if the pattern of
the URLs is going to be the same.

The code would look something like this:

    
    
    from bs4 import BeautifulSoup
    import re
    
    text = """<a class="d2l-link d2l-outline d2l-left" href="/d2l/lp/ouHome/home.d2l?ou=2019786" title="Enter CHEM-1315-018 - Lab-CHEM 1315-001">CHEM-1315-018 - Lab-CHEM 1315-001</a>"""
    soup = BeautifulSoup(text)
    href_link = soup.a['href']
    match = re.search(r'ou=(\d*)', href_link)
    if match:
        print int(match.group(1)) # do whatever you want with it here
    else:
        print "no id found"
    

Note that you don't need to specify the whole URL to get the id, as the id
would be always sent as a parameter after "ou=".

