Skip to content Skip to sidebar Skip to footer

How To Get Href Link From Onclick Function In Python

I want to get href link of website form onclick function Here is html code in which onclick function call a website Normaly i use this code to get href link from any span or ele

Solution 1:

You cannot directly parse aHref attribute, you need to extract onclick first.

>>>import re>>>data = soup.select('.taLnk')[0].get('onclick')>>>href = re.search(r"(?is)'aHref':'(.*?)'",str(data)).group(1)
'LqMWJQzZYUWJQpEcYGII26XombQQoqnQQQQoqnqgoqnQQQQoqnQQQQoqnQQQQoqnqgoqnQQQQoqnQQuuuQQoqnQQQQoqnxioqnQQQQoqnQQ2EisSMVCnVcJQQoqnQQQQoqnxioqnQQQQoqnQQniaQQoqnQQQQoqnqgoqnQQQQoqnQQWJQzhYMJkH3KHVAdJJH3VVdB'

Solution 2:

You can use a regex with bs4, selecting the span with the class taLnk and the onclick attribute starting with ta.trackEventOnPage:

h = """<div class="fl">
  <span class="taLnk" onclick="ta.trackEventOnPage('Eatery_Listing', 'Website', 594024, 1); ta.util.cookie.setPIDCookie(15190); ta.call('ta.util.link.targetBlank', event, this, {'aHref':'LqMWJQzZYUWJQpEcYGII26XombQQoqnQQQQoqnqgoqnQQQQoqnQQQQoqnQQQQoqnqgoqnQQQQoqnQQuuuQQoqnQQQQoqnxioqnQQQQoqnQQ2EisSMVCnVcJQQoqnQQQQoqnxioqnQQQQoqnQQniaQQoqnQQQQoqnqgoqnQQQQoqnQQWJQzhYMJkH3KHVAdJJH3VVdB', 'isAsdf':true})">Website</span>
</div>"""from bs4 import BeautifulSoup
import re
soup = BeautifulSoup(h)

data = soup.select_one("span.taLnk[onclick^=ta.trackEventOnPage]")["onclick"]
print(re.search("'aHref':'(.*?)'", data).group(1))

Post a Comment for "How To Get Href Link From Onclick Function In Python"