Home>

I want to save multiple images in a directory.
Currently, all 19 files have been created, but the same image has been saved.
I want to get all images as per the link.
By the way, in the above for statement, different image URLs are taken.

It seems that the URLs of different images are taken.
This is the content https://msp.c.yimg.jp/yjimage?q=XksQDoAXyLHxoogz00HtUlbeKQ8RwxIBXNLesE18LfanzXbaVJCSxg5lzyDW0.CfcTPc.uqyQGk_5JpJMysZ5eay8vteb8fNAB7LKz4RSMOZdfNWGqE4ZlmAF4f.Ca34AVF.uxJroqEBXJ98Nu9_&sig=13a5plidl&x=275&y=183
This is the content https://msp.c.yimg.jp/yjimage?q=vo8En0oXyLEuenVktykTK.0z7Ywhfc0mbO315.XhVx28xP8H0jsrfaSSGFHyQN7LQe_U27TWGSR5EKKUNr0DPPOxXcYJxAPPLejECys4V8_7T9_O97VSDRHya3vAZUcEMjyRGj9PNuicK6wtjkmcVBY-&sig=13cafubeq&x=300&y=168
This is the content https://msp.c.yimg.jp/yjimage?q=Dt7w3tcXyLFMe.2z3.eA75r2CI78dzV4NIDVkMEZLEaw0KJcRIDKtOqZKlcC1rxaGUx0ITv4gob3a90hxR3TAlM6kJOVZKCxhYPyAdYsqXvJKk6a1mIqBCyEDg4MDWS0eGHw1HjLegSNY.zePzjg&sig=13a1brdhv&x=259&y=194
This is the content https://msp.c.yimg.jp/yjimage?q=mfiwUWMXyLHDgRRDDIIIqsFCwPp1meul.3JUfBBWpDNMAMNKI5q16M.ZrSyi1LDcTZiStPl.Vlc2C0ByDivfT2bF6lIlMZ1BZivzo7CkfFdUcwi4i7ZcWmbzmQ1rFAhDSuBqr9GjXJPJGSZwrCDNHA0-&sig=13cptkvd5&x=275&y=183
This is the content https://msp.c.yimg.jp/yjimage?q=3ASplKUXyLGDKPY31dGjoe4UdlG63NrgWAIAxTuDUDcJQTjAnTYr35gFjpYn53YxTMEJ51DDAwFczRoIoVv7awNiCJNMQW5TXY97dSp88WM0Cwh8bR5MgtjxYRvQ5QatIHBwmQaMZJRw3WrCnOjN&sig=13afe9ris&x=259&y=194
This is the content https://msp.c.yimg.jp/yjimage?q=n5Ru7YkXyLHWtvhofYG6SUZo8u5NUjiIUImO9kbrURSYRzc9Z_hk510g662DQJ_BNZVdJqSn4Md9q55o9QL23LywGuzQ4LfHGgyVqgZaZs4I6mp9.aNo3uV2wfi.IYJsdiyUezqY8_3pbsEtD.APbdg-&sig=13c7bhfe7&x=275&y=183
This is the content https://msp.c.yimg.jp/yjimage?q=2MEmquwXyLHLsCXPB46gMqPHbsUE2bpx_2UcpIumdvV4fSHRDMZ17XHuu8p2UlFqWCaYNiGvuLnwrtQAoenNt5btapZ0Qbm4ah1ZGgofGnT2mfoapSI68M6EJ4lCSVNlcdRHW_R.MaDhtxGCl4R0MSc-&sig=13cgdihc7&x=275&y=183
This is the content https://msp.c.yimg.jp/yjimage?q=X86q.44XyLEkexPCsLFDmk_pPoq0Hx8eLg5umUJCmOdGyeiz2PD9TNuFRuHsnOJn5_kfoFDU0DJiPHJuEvOTmkNIVqkA5ewBA1Eqi1dvTo6gGT2Aycu1jbrJfXF1iG3VbaRzFU4LhGwd13PPMDWB&sig=13agffmhs&x=275&y=183
This is the content https://msp.c.yimg.jp/yjimage?q=1Ubp7zgXyLEss.FgalThe7HDPBckEv90uhHF6KpyNhx0ZUjRcGEb4Bhd0gq8DUUES.tgp3lKPyPaJlXlt252OUK8AwOvVgwqFxZDRC5c5VWRKP4N94YYJVPpoH_qYdYxMvBIqUlK3xyhnbS8fmHtv4Y-&sig=13c0p3l1p&x=259&y=194
This is the content https://msp.c.yimg.jp/yjimage?q=IJjErY8XyLGwlWEWBFpmAKKufnkaNR.LER_ly7eVSBndW4x185txb2rrKjdXtgds.OYKQRigv6HeD.i6VESuxm.uSp2uADiPOJtNEepXqglKQkF_qzW9_CQFxhiQ66oi6gVEhQJoKbxinG9DOzZw&sig=13amltn7d&x=275&y=183
This is the content https://msp.c.yimg.jp/yjimage?q=MdukpKoXyLEiD1dybJUtoi_lbr_oY7CGY1CbtWvKXmbi99udlELo4kOIZ_kaTPInKYdwaWHdwVsFxgDjot8jYV90JTuZi3r_HxCP6kNw_4T8BaQ8ujG9Yl9OCj79k6e2.FrkAyxZdgtSIFFlXEqI&sig=13aa50pi6&x=259&y=194
This is the content https://msp.c.yimg.jp/yjimage?q=UN5O1GAXyLFNN_hm0_kmLaAMkTrqQIVFq1zs2kwO.yFnbh5Hyz3eCMJT5vV2wQ.xBQ3U152Gkg.76jOzsnaaKZXdoQ4w1iqDGSz531c6DIaTk0db1lLvvPjGZOwxdbK98dOnL6Y4L1xJmyJbXn5O&sig=13a6e9jm4&x=275&y=183
This is the content https://msp.c.yimg.jp/yjimage?q=6_n18d8XyLH9rVOwic7CKtDKGCgDyKmdCjud9EaPX3tb8w451EIuwx5NR06TvdhVveve6djKIFGsAY0pOzjRrpnTAaC41oEAd9w4t.GjEURFEZm8FD0a8XfmBHB2yigp7xAvdBByrYl11UUTTfGY&sig=13agur98e&x=275&y=183
This is the content https://msp.c.yimg.jp/yjimage?q=gfM8HBEXyLHdh4tnesPZpBxt4DRsS9fXXEQAkLoxsCViODebDBV.olNMWptvbdp.9gxusI0LBW.NF155cVO9NSDDrA.UAcgolmeKnhKZq3piGNeT5Wpj5NOPs_S6O_SYl730oAZLYj_Qiecf76UYWx4-&sig=13cge4ds3&x=259&y=194This is the content https://msp.c.yimg.jp/yjimage?q=NWp3KHYXyLGeIzmW0FY3kzB7YboWWNNNBQLiQ3ewAFoGEpfy3qF4cyOTDxeIq.h8SyxfevMr9CK1kLltgO8e4O.21JN7.V0z8fIUgcO073Xz4h75RxDZgYN7c4hvP.SshVBoUSRLExN3zJKTTVauzYs-&sig=13csoj0i9&x=290&y=173
This is the content https://msp.c.yimg.jp/yjimage?q=lEaVsOkXyLGnvCk2WWFdDzT50yLEEvHksdYV.Dq1e7bXCNBcBC6s.otK3rDJAZvVrQ4mJ.PXjnXKUNmHEwfHnX5zA_5SiNccsGbmZvGRTax07Db_Ug_yWT6tVLdbwQUAsg2pgKgzIj2mgntuwcW7&sig=13aubbufu&x=259&y=194
This is the content https://msp.c.yimg.jp/yjimage?q=8SfpK7sXyLExPAGsJwmmHBvPkrDF2XWPwjsA8jnX4N1eFAOPIT7GFnaNTuB4McYBFgx0xD_HT21AqhcuxbtN1NZ48KNngC3Nk8qK3pRLvB0wUT6lNQE5XVlC15CRq63q0R7P26GNl.KjP7YIXbJd&sig=13a5ihmgc&x=275&y=183
This is the content https://msp.c.yimg.jp/yjimage?q=Hyl2IJkXyLFaMcLlGPzDppn7Q1zvcLm7FjR5pH4LYFS0zuwzEieWuHWKmreF2YhUuObfk4ygHps0R4BPIVXinT3YzMdXi9pJ4jQ04xmg8TEloaKgp7icmlMHnCf1wu1MvtWVrMQ8CEhtpzCCcAuH&sig=13a2ac30r&x=275&y=183
This is the content https://msp.c.yimg.jp/yjimage?q=JPwLRo0XyLEHFafLnPkrQIJp0GkxRfPMQrBcpv3vZh.yv4S5GFOJHf6bMfzfhowH4Qhi2F1U.d9Z_.Luu5jKwjPBv8SUK3CKx8prqS8dIc5Rek4KGvHju5nCeV3VlOpvewwy4HufJO4j105i8geA&sig=13alicaco&x=259&y=194
This is the content https://msp.c.yimg.jp/yjimage?q=w1oGxlIXyLGuh1qAvHDOp_tB1yKkdhJi_6qcuJjWY0NWsJbhlh2C9tZor2qUjT.whUpitU8QF8gbOZuSDgsZHRJmQFO34Zjq4rHXiHjHeoSZIxty20CfBGv7LESQEvFdxhLSdRxhv2QR_Wc7oJYr&sig=13av68fo1&x=277&y=182
Download completed
Applicable source code
import requests
from bs4 import BeautifulSoup
import os
url ='https://search.yahoo.co.jp/image/search?p=%E6%A1%9C&fr=top_ga1_sa&ei=UTF-8&mfb=P051'
r = requests.get(url)
soup = BeautifulSoup(r.text,'html.parser')
url_src = []
show =''
base_dir = "/Users/uematsuyuuki/Desktop/sakura_image"
def download_image(url,file_path):
    r = requests.get(url,stream=True)
    print(r)if r.status_code == 200:
        with open(file_path,"wb") as f:
            f.write(r.content)
            print('Now scraping:::{}'.format(url))
for search in soup.find_all('img',attrs={'alt':'Image search results for'Sakura''}):
    img_src = search.get('src')
    print('This is the content' + img_src)
    url_src.append(img_src)
for index,url in enumerate(url_src):
    filename = "{}.jpg".format(index)
    dire = os.path.join(base_dir,filename)
    download_image(url=img_src,file_path=dire)
    print(dire)
print("Download completed")
What I tried

In the case of img_src at the top, a different URL is output when it is output by the print function,
When the download_image column in the last for statement is output, it seems that img_src is reading only one URL, and all the images saved in the file are the same.
So I tried matching the hierarchy to img_src at the top. However, the resulting downloaded images were all the same.

  • Answer # 1

    The code is such that the writing function is executed only once at the end.

  • Answer # 2

    The img_src part in the download_image(url=img_src,file_path=dire) line was incorrect. At first I was trying to display it by looping img_src with for etc from the outside, but I could not. As a result of re-thinking, there was a serious flaw. In the for index,url in enumerate(url_src): line, I realized that I was able to retrieve the index value and the contents of the list stored in url using enumerate, and then download_image(url=img_src,file_path=dire) to url I was able to solve it by setting =urlk. Thank you for your advice.