Answer a question

I've been trying to get beautiful soup to extract the image files (pokemon card images) from this page: https://www.pokellector.com/sets/EVO-Evolutions

The code below only gives some src's of buttons but I can't manage to extract all the images sources.

for a in soupimages.find_all('a'):
    if a.img:
        if a.img.has_attr('src'):
            print(a.img['src'])

Answers

Looks like all the card image thumbnails are formatted like this:

<img class="card lazyload" data-src="https://.../Caterpie.EVO.5.thumb.png">

Find the <img> elements with class='card' and you should get the card image URLs.

from bs4 import BeautifulSoup
import requests

url = "https://www.pokellector.com/sets/EVO-Evolutions"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
for img in soup.find_all('img', class_="card"):
    print(img.get('data-src'))

Output:

https://den-cards.pokellector.com/197/Venusaur-EX.EVO.1.thumb.png
https://den-cards.pokellector.com/197/M-Venusaur-EX.EVO.2.thumb.png
...
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐