how can i extract data from within the brackets of `onclick` attribute values?

  • Last Update :
  • Techknowledgy :

I tried to explain everything in the comments:

from bs4 import BeautifulSoup

html = '''<td style="word-spacing:-3px" align="left">
   <a href="javascript:" onclick="analysis(1644983)"></a>
   <a href="javascript:" onclick="AsianOdds(1644983)" style="margin-left:3px;"></a>
   <a href="javascript:" onclick="EuropeOdds(1644983)" style="margin-left:3px;"></a>
</td>'''

soup = BeautifulSoup(html, 'html.parser')

# Find all <a> elements
   elements = soup.find_all('a')

   # Loop over all found elements
   for element in elements:
   # Disregard element if it doesn't contain onclick attribute
   if 'onclick' not in element.attrs:
   continue
   # Get attribute value
   value = element['onclick']
   # Disregard wrong elements
   if not value.startswith('analysis('):
   continue
   # Extract position of opening bracket
   position = value.index('(') + 1
   # Slice string so only content inside bracket is left
   value = value[position:-1]
   # Print result
   print(value)

Suggestion : 2

When using function expression, the same process applies – set the handler value to the variable name without the trailing parentheses, as seen below:,The onclick event occurs when a user clicks on an element with an assigned onclick event .,To invoke the handler, we use the name of the variable – and not that of the function – when defining the onclick event handler. The function could also have been anonymous when defined.,While the order in which the event handlers are defined on the button is arbitrary, the event handlers will always be called in the same sequence. The output produced by clicking the button above is as follows:

JavaScript

function greet() {
   console.log('Hey there clicker!');
}

HTML

<button onclick="greet()">Click me</button>

JavaScript

myButton.onclick = greeting

Suggestion : 3

July 8, 2022 5 min read 1622

Take the following simple example written in HTML:

<button onclick="sayHello()">
   Say Hello
   <button>
2._
<button onClick={sayHello}>
  Say Hello
<button>

The following example shows how to prevent a link from opening a new page by default:

<a href="#" onclick="console.log('The link was clicked.'); return false">
   Click me
</a>
5._
import React from "react";

const ShowAlertComponent = () => {
  const showAlert = () => {
    alert("I'm an alert");
  }

  return <button onClick={showAlert}>Show alert</button>;
}
export default ShowAlertComponent;

Inline functions allow you to write code for event handling directly in JSX. See the example below:

import React from "react";

const App = () => {
  return (
    <button onClick={() => alert("Hello!")}>Say Hello</button>
  );
};

export default App;

Suggestion : 4

Last updated: May 2, 2022

Copied!const App = () => {
  const handleClick = (event, param) => {
    console.log(event);
    console.log(param);
  };

  return (
    <div>
      <button onClick={event => handleClick(event, 'hello world')}>
        Click
      </button>
    </div>
  );
};

export default App;
Copied!<button onClick={event => handleClick(event, 'hello world')}>
  Click
</button>