Issue
HTML contains strings in divs:
- 'div class="slide"' 'img src="xttps://site.com/files/r_1000,kljg894/43k5j/35h43jkl.jpg' '/div'
- 'div class="slide"' 'img src="xttps://site.com/files/r_2000,kljg894/43k5j/35h43jkl.jpg' '/div'
- 'div class="slide"' 'img src="xttps://site.com/files/r_3000,kljg894/43k5j/35h43jkl.jpg' '/div'
Need to get a link containing only "r_3000"
This code gets all the links:
imglink = soup.find_all('img')
print(imglink)
I looked through and tried many tips. For example:
('img', string="*r_3000*")
, ('img', string=re.compile('r_3000')
and ('img', string=lambda s: 'r_3000' in s)
and many others, but all this does not work :(
Help me please.
Solution
Many ways you can do that.Try css selector.
from bs4 import BeautifulSoup
html='''<div class="slide"><img src="xttps://site.com/files/r_1000,kljg894/43k5j/35h43jkl.jpg"></div>
<div class="slide"> <img src="xttps://site.com/files/r_2000,kljg894/43k5j/35h43jkl.jpg"></div>
<div class="slide"><img src="xttps://site.com/files/r_3000,kljg894/43k5j/35h43jkl.jpg"></div>'''
soup=BeautifulSoup(html,"html.parser")
for item in soup.select("img[src*='r_3000']"):
print(item['src'])
Answered By - KunduK
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.