3

I try to scrape out an ImageId inside a button tag, want to have the result:

"25511e1fd64e99acd991a22d6c2d6b6c".

When I try:

drawing_url = drawing_url.find_all('button', class_='inspectBut')['onclick'] 

it doesn't work. Giving an error-

TypeError: list indices must be integers or slices, not str

Input =

for article in soup.find_all('div', class_='dojoxGridRow'):
drawing_url = article.find('td', class_='dojoxGridCell', idx='3')
drawing_url = drawing_url.find_all('button', class_='inspectBut')
if drawing_url:
    for e in drawing_url:
        print(e)

Output =

    <button class="inspectBut" href="#" 
        onclick="window.open('getImg?imageId=25511e1fd64e99acd991a22d6c2d6b6c&amp;
                 timestamp=1552011572288','_blank', 'toolbar=0, 
                 menubar=0, modal=yes, scrollbars=1, resizable=1, 
                 height='+$(window).height()+', width='+$(window).width())" 
         title="Open Image" type="button">
    </button>
... 
...
4
  • I am not an expert but shouldnt [onclick] be ['onclick']
    – Matt
    Mar 8, 2019 at 2:49
  • sorry typo i actually typed ['onclick'], it gives error : drawing_url = drawing_url.find_all('button', class_='inspectBut')['onclick'] TypeError: list indices must be integers or slices, not str Mar 8, 2019 at 2:51
  • providing an url/sufficient source html helps elicit better quality answers and likely faster resolution.
    – QHarr
    Mar 8, 2019 at 5:04

3 Answers 3

2

Try this one.

import re

#for all the buttons
btn_onlclick_list = [a.get('onclick') for a in soup.find_all('button')]
for click in btn_onlclick_list:
     a = re.findall("imageId=(\w+)", click)[0]
     print(a)
5
  • beautiful! it works! Thank you so much!! how do i loop through all then? Mar 8, 2019 at 4:28
  • dont know how to get all buttons, print(click) only show the first button. and import array to re is not allowed. do you have any idea? Mar 8, 2019 at 5:29
  • for multiple button: [btn.get("onclick") for btn in soup.find_all('button')]
    – Pankaj
    Mar 8, 2019 at 5:50
  • can you edit your code to complete your answer please, many thanks!! Mar 8, 2019 at 6:10
  • @LucasK.C.L Updated.
    – Pankaj
    Mar 8, 2019 at 6:30
1

You first need to check whether the attribute is present or not. tag.attrs returns a list of attributes present in the current tag

Consider the following Code.

Code:

from bs4 import BeautifulSoup
a="""
<td>
<button class='hi' onclick="This Data">
<button class='hi' onclick="This Second">
</td>"""
soup = BeautifulSoup(a,'lxml')
print([btn['onclick'] for btn in soup.find_all('button',class_='hi') if 'onclick' in btn.attrs])

Output:

['This Data','This Second']

or you can simply do this

[btn['onclick'] for btn in soup.find_all('button', attrs={'class' : 'hi', 'onclick' : True})]
2
  • getting the same error, TypeError: list indices must be integers or slices, not str Mar 8, 2019 at 3:29
  • Yes. Using print([btn['onclick'] for btn in soup.find_all('button',class_='inspectBut') if 'onclick' in btn.attrs]) Mar 8, 2019 at 3:37
1

You should be searching for

button_list = soup.find_all('button', {'class': 'inspectBut'})

That will give you the button array and you can later get url field by

 [button['getimg?imageid'] for button in button_list]

You will still need to do some parsing, but I hope this can get you on the right track.

Your mistake here was that you need to search correct property class and look for correct html tag, which is, ironically, getimg?imageid.

1
  • Your answer works best , i tested it out, thanks a lot. should be the best answer. Mar 8, 2019 at 5:53

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.