本文介绍了(美丽汤)在按钮标签内获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在按钮标签内抓取一个ImageId,希望得到结果:

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

"25511e1fd64e99acd991a22d6c2d6b6c".

当我尝试时:

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

输入=

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)

输出=

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

推荐答案

尝试一下.

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)

这篇关于(美丽汤)在按钮标签内获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 07:44