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>

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:
   # Get attribute value
   value = element['onclick']
   # Disregard wrong elements
   if not value.startswith('analysis('):
   # Extract position of opening bracket
   position = value.index('(') + 1
   # Slice string so only content inside bracket is left
   value = value[position:-1]
   # Print result

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:


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


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


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 onClick={sayHello}>
  Say Hello

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
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) => {

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

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