P
P
PANDEMIC2017-02-11 15:58:34
Python
PANDEMIC, 2017-02-11 15:58:34

I can’t complete the cycle, I need to sort through 1 link, then save it, a picture from the site with a new name, the cycle is different, but it works, what should I do?

there is a list of links, you need it to sort through the list by 1 link, follow it, then download the pictures each time with a new name, I tried to have several script options, who will help with the error?
1var)

import requests
from requests import Session
from bs4 import BeautifulSoup
import re
from multiprocessing.dummy import Pool as ThreadPool

#s = Session()

def get_photo_from_page():
  tut = []
  names = []
  r = requests.get('https://vk.com/uporols_you').text
  soup = BeautifulSoup(r, 'lxml')
  im = soup.find_all('img', class_="ph_img")

  for a in im:
    s = a.get('data-src_big').split('|')[0]
    tut.append(s)

  y = "img%s.jpg"
  for im in [y % i for i in range(1,5)]:
    names.append(im)

  for t, a in tut, names:
    p = requests.get(t)
    out = open(a, "wb")
    out.write(p.content)
    out.close()

def main():
  get_photo_from_page()

if __name__ == '__main__':
  main()

2var)
import requests
from requests import Session
from bs4 import BeautifulSoup
import re
from multiprocessing.dummy import Pool as ThreadPool

#s = Session()

def get_photo_from_page():
  tut = []
  r = requests.get('https://vk.com/uporols_you').text
  soup = BeautifulSoup(r, 'lxml')
  im = soup.find_all('img', class_="ph_img")

  for a in im:
    s = a.get('data-src_big').split('|')[0]
    tut.append(s)

  y = "img%s.jpg"
  for t in tut:
    p = requests.get(t)
    for im in [y % i for i in range(1,5)]:
      out = open(im, "wb")
      out.write(p.content)
      out.close()

def main():
  get_photo_from_page()

if __name__ == '__main__':
  main()

in the 2nd option, it takes all the links to the photo, and puts x in the dictionary, then it takes the last link and only the last one and saves it the number of times as specified in the loop

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
javedimka, 2017-02-11
@InvictusManeo

import requests
import bs4


tut = []
names = []

r = requests.get('https://vk.com/uporols_you').text
soup = bs4.BeautifulSoup(r, 'lxml')
im = soup.find_all('img', class_='ph_img')

for a in im:
    s = a.get('data-src_big').split('|')[0]
    tut.append(s)

for num, link in enumerate(tut):
    p = requests.get(link)
    out = open("img%s.jpg" % (num), 'wb')
    out.write(p.content)
    out.close()

All 5 funny pictures were saved in a folder with names (img0.jpg, img1.jpg, etc.)
Why didn't it work for you? Well, in the first case, because range(1,5) gives a sequence of numbers from one to 5, not including 5, i.e. 1, 2, 3, 4. It turns out you have 5 pictures, and 4 names. Then at one fine moment in your cycle: the link was assigned to the
variable t , but the name was not assigned to the variable a, so you got an error:
Hardcoding this is generally strange, you can (do not need to) use "in len(range(tut))", and then when they tell you about enumerate - useenumerate .
But the first time you had a slightly different code, with a generator right in the loop. Now it’s clear why I advised you to look into the generator?
Why didn't the second option work for you? Well, because you need to understand how nested for loops work in python . To understand, you can run this code in an interpreter or terminal
for i in ('a', 'b', 'c', 'd'):
    for x in ('1', '2', '3', '4'):
        print(i + " => " + x)

You just saved one picture 4 times at each iteration and assigned different names to it, then on the next link you did the same overwriting those 4 saved pictures, as a result, all the last 4 saved pictures are the same picture from the last link.
Good luck.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question