H
H
HEnl2020-06-10 18:32:19
Python
HEnl, 2020-06-10 18:32:19

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>


How to parse all links to both pages and images and write them into separate variables? no matter how I tried, empty elements are obtained. You need to copy the link in full
For example:
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

1 answer(s)
S
soremix, 2020-06-10
@markbrutx

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)

Conclusion
link1 link2.png
link1 link2.png
link1 link2.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question