Answer the question
In order to leave comments, you need to log in
How to parse page link and image link using BS4?
There is this code:
<div>
<div class="center mt10 mb5">{}</div>
<div class="center">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td valign="top" class="lp0">
<a class="square-btn-green" href="сслыка1"><img class="va-m" src="ссылка2.png" alt=""></a> </td>
<td valign="top" class="lp0">
<a class="square-btn-green" href="ссылка1"> <img class="va-m" src="ссылка2.png" alt=""></a> </td>
<td valign="top" class="lp0">
<a class="square-btn-green" href="сслыка1"><img class="va-m" src="сслыка2.png" alt=""></a> </td>
</tr>
</tbody></table>
<div class="gray_italic center mt5"> </div>
</div>
</div>
<div class="balka-bot"></div>
</div>
link1_1 = "http://example.com"
link1_2 = "http://example.com/1-2.png"
...
link3_1 = "http://example3.com"
link3_2 = "http://example.com/3-2.png"
Answer the question
In order to leave comments, you need to log in
from bs4 import BeautifulSoup as bs4
source = '''<div>
<div class="center mt10 mb5">{}</div>
<div class="center">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td valign="top" class="lp0">
<a class="square-btn-green"><img class="va-m" src="ссылка2.png" alt=""></a> </td>
<td valign="top" class="lp0">
<a class="square-btn-green" href="ссылка1"><img class="va-m" src="ссылка2.png" alt=""></a> </td>
<td valign="top" class="lp0">
<a class="square-btn-green" href="сслыка1"><img class="va-m" src="сслыка2.png" alt=""></a> </td>
</tr>
</tbody></table>
<div class="gray_italic center mt5"> </div>
</div>
</div>
<div class="balka-bot"></div>
</div>'''
soup = bs4(source, 'html.parser')
links = soup.find_all('a', attrs={'class': 'square-btn-green'})
for link in links:
url = link.get('href', '-')
image_url = link.find('img').get('src', '-')
print(url, image_url)
link1 link2.png
link1 link2.png
link1 link2.png
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question