Home>

I'm new to Python.
I'm making a program that acquires images from a net shop site and saves them locally in Python. The following problems have occurred. It would be helpful if you could lend me your help.

Error message

Although I use find_all (), I can get only the first image that meets the condition.
(In the middle of the source code, the code that prints once when the image is acquired is written. At that stage, only one image can be acquired.)

Note-In the following source code, the folder name described after "with open" is left blank.

Applicable source code

Python

# coding: utf-8
import requests
from bs4 import BeautifulSoup
url = 'https://store.nike.com/us/en_us/product/epic-react-flyknit-id/?piid=44794&pbid=1032652773'
headers = {'User-Agent': 'Mozilla/5.0'}
soup = BeautifulSoup (requests.get (url, headers = headers) .content, 'html.parser')
images = [] # Empty array of image list
for img in soup.find_all ('img', class _ = "exp-id-imagegrid__image", limit = 10):
    print (img.get ("src"))
    images.append (img.get ("src"))
for target in images:
    re = requests.get (target)
    with open (r '', 'wb') as f: # store in img folder
      f.write (re.content)
print ("Image saving is complete")
Tried

-There were several image class names that I wanted to acquire, so I tried them all.
-I tried two parsers, "html.parser" and "lxml".

Supplemental information
  • Answer # 1

      

    Although I use find_all (), I can get only the first image that meets the condition.

    print (requests.get (url, headers = headers) .content)

    Add the above line and print the HTML content
    Searching withsrc ="https: //render.nikeid.comis easy to understand
    classattribute isJavaScriptinstead ofexp-id-imagegrid__imageexcept for one image.
    * When you view the target page with wappalyzer, it is SPA (single-page application) ofReact&Handlebars.
    Scraping withSelenium&ChromeDriverand analyzingdriver.page_sourcewithBeautifulSoupIt may not be.