如何使用Beautifulsoup4在onclick上打印src

时间:2017-01-04 11:26:42

标签: python onclick beautifulsoup src

<td valign="top" style="padding-left:5px;">
		<div id="small_pic" style="overflow-x:auto; padding-bottom:2px; padding-bottom:expression(this.scrollWidth > this.offsetWidth ? 18 : 0); white-space:nowrap; width:400px;"><img id="imgsmall_3151_1427114051" alt="" onclick="showLargePic('/modustpl/pics/3151_1427114051_l.jpg' , '3151_1427114051');" class="pics" src="/modustpl/pics/3151_1427114051_s.jpg" style="display:inline; border:2px solid red;" /><img id="imgsmall_3151_1427114058" alt="" onclick="showLargePic('/modustpl/pics/3151_1427114058_l.jpg' , '3151_1427114058');" class="pics" src="/modustpl/pics/3151_1427114058_s.jpg" style="display:inline;" /><img id="imgsmall_3151_1427114066" alt="" onclick="showLargePic('/modustpl/pics/3151_1427114066_l.jpg' , '3151_1427114066');" class="pics" src="/modustpl/pics/3151_1427114066_s.jpg" style="display:inline;" /><img id="imgsmall_3151_1427114072" alt="" onclick="showLargePic('/modustpl/pics/3151_1427114072_l.jpg' , '3151_1427114072');" class="pics" src="/modustpl/pics/3151_1427114072_s.jpg" style="display:inline;" /></div>
		<div id="large_pic" style="margin-top:5px; width:400px;"><img alt="" class="pics" src="/modustpl/pics/3151_1427114051_l.jpg" /></div>
	</td>
(我正在使用beautifulsoup 4,pythion 2)

image = soup.find_all(onclick= 'showLargePic')


		print image.get('src')

(AttributeError:'ResultSet'对象没有属性'get')

1 个答案:

答案 0 :(得分:0)

import bs4

html = '''<td valign="top" style="padding-left:5px;">
        <div id="small_pic" style="overflow-x:auto; padding-bottom:2px; padding-bottom:expression(this.scrollWidth > this.offsetWidth ? 18 : 0); white-space:nowrap; width:400px;"><img id="imgsmall_3151_1427114051" alt="" onclick="showLargePic('/modustpl/pics/3151_1427114051_l.jpg' , '3151_1427114051');" class="pics" src="/modustpl/pics/3151_1427114051_s.jpg" style="display:inline; border:2px solid red;" /><img id="imgsmall_3151_1427114058" alt="" onclick="showLargePic('/modustpl/pics/3151_1427114058_l.jpg' , '3151_1427114058');" class="pics" src="/modustpl/pics/3151_1427114058_s.jpg" style="display:inline;" /><img id="imgsmall_3151_1427114066" alt="" onclick="showLargePic('/modustpl/pics/3151_1427114066_l.jpg' , '3151_1427114066');" class="pics" src="/modustpl/pics/3151_1427114066_s.jpg" style="display:inline;" /><img id="imgsmall_3151_1427114072" alt="" onclick="showLargePic('/modustpl/pics/3151_1427114072_l.jpg' , '3151_1427114072');" class="pics" src="/modustpl/pics/3151_1427114072_s.jpg" style="display:inline;" /></div>
        <div id="large_pic" style="margin-top:5px; width:400px;"><img alt="" class="pics" src="/modustpl/pics/3151_1427114051_l.jpg" /></div>
    </td>'''

soup = bs4.BeautifulSoup(html, 'lxml')
image = soup.find_all(src=True)
for i in image:
    print (i.get('src'))

出:

/modustpl/pics/3151_1427114051_s.jpg
/modustpl/pics/3151_1427114058_s.jpg
/modustpl/pics/3151_1427114066_s.jpg
/modustpl/pics/3151_1427114072_s.jpg
/modustpl/pics/3151_1427114051_l.jpg

find_all()返回标记列表,如果要访问标记,则应迭代它。