how to get href link from onclick function in python

2024/10/12 22:33:29

I want to get href link of website form onclick function Here is html code in which onclick function call a website

<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>

Normaly i use this code to get href link from any span or element

geturl = soup.findsoup("span", {"class": "taLnk"})
for link in geturl:
hreflink = link.get("href")
print(hreflink)

But in this case there is no way to directly call href because href exist in onclick function

Please help me what to do now

Answer

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'
https://en.xdnf.cn/q/118152.html

Related Q&A

Python tkinters entry.get() does not work, how can I fix it? [duplicate]

This question already has answers here:Why is Tkinter Entrys get function returning nothing?(6 answers)Closed 7 years ago.I am building a simple program for university. We have to convert our code to …

Pandas secondary y axis for boxplots

Id like to use a secondary y-axis for some boxplots in pandas, but it doesnt seem available. import numpy as np import pandas as pddata = np.random.random((10, 5)) data[:,-1] += 10 # offset one column…

Fixing Negative Assertion for end of string

I am trying to accept a capture group only if the pattern matches and there is not a specific word before the end of the group. Ive tried a # of approaches and none seem to work, clearly Im not getting…

Two Sorted Arrays, sum of 2 elements equal a certain number

I was wondering if I could get some help. I want to find an algorithm that is THETA(n) or linear time for determining whether 2 numbers in a 2 sorted arrays add up to a certain number.For instance, let…

I cant seem to install numpy

I tried to install numpy, but whenever I start my program, I get these messages.Error importing numpy: you should not try to import numpy fromits source directory; please exit the numpy source tree, an…

Using slices in Python

I use the dataset from UCI repo: http://archive.ics.uci.edu/ml/datasets/Energy+efficiency Then doing next:from pandas import * from sklearn.neighbors import KNeighborsRegressor from sklearn.linear_mode…

Elasticsearch delete_by_query wrong usage

I am using 2 similar ES methods to load and delete documents:result = es.search(index=users_favourite_documents,doc_type=favourite_document,body={"query": {"match": {user: user}}})A…

SQLAlchemy: Lost connection to MySQL server during query

There are a couple of related questions regarding this, but in my case, all those solutions is not working out. Thats why I thought of asking again. I am getting this error while I am firing below quer…

row to columns while keeping part of dataframe, display on same row

I am trying to move some of my rows and make the them columns, but keep a large portion of the dataframe the same.Resulting Dataframe:ID Thing Level1 Level2 Time OAttribute IsTrue Score Value 1 …

SQLAlchemy InvalidRequestError when using composite foreign keys

My table relationships in SQLAlchemy have gotten quite complex, and now Im stuck at this error no matter how I configure my relationship.Im a bit new to SQLAlchemy so Im not sure what Im doing wrong, b…