how to print src with onclick using Beautifulsoup4

185 views Asked by At

<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>
(I'm using beautifulsoup 4, pythion 2)

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


  print image.get('src')

(AttributeError: 'ResultSet' object has no attribute 'get')

1

There are 1 answers

2
宏杰李 On
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'))

out:

/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() return a list of tag, if you want to get access to the tag, you should iterate over it.