Search code examples
pythonhtmlhref

using BeautifulSoup to find the href link


i am using BeautifulSoup to find the href by class "reply_to".

<div class="message">
   <div class="reply_to details">
        In reply to <a href="#go_to_message18" onclick="return 
        GoToMessage(18)">this message</a>
   </div>
</div>

now the code below can find the reply_to element, but then how to go down to and retrieve href from

soup = BeautifulSoup(parsed.input[0], "html.parser")
alldefaultmesssages = soup.select(".message.default")
alldefaultmesssages.select_one(".reply_to")

Solution

  • You need to get to the child anchor tag of the reply_to element:

    import requests
    from bs4 import BeautifulSoup
    
    
    html = """<div class="message">
       <div class="reply_to details">
            In reply to <a href="#go_to_message18" onclick="return
            GoToMessage(18)">this message</a>
       </div>
    </div>"""
    
    soup = BeautifulSoup(html, 'html.parser')
    print(soup.find("div", class_="reply_to").a.get('href'))
    #>>> #go_to_message18