循环错误For和请求Python3解析

时间:2017-02-11 11:39:23

标签: python-3.x for-loop beautifulsoup python-requests

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, im in tut, [y % i for i in range(1,5)]:
        p = requests.get(t)
        out = open(im, "wb")
        out.write(p.content)
        out.close()

def main():
    get_photo_from_page()

if __name__ == '__main__':
    main()
  

来自cmd的t,即tut,[y%i for i in range(1,5)]的错误:   ValueError:解压缩的值太多(预期2)   的>我需要列出1到1的URL累积,并通过possylke,   并使用新名称保存所有图像,在不同的周期中,它始终   获取最后一个可用的引用并将其存储为数字   周期中显示的时间。

1 个答案:

答案 0 :(得分:0)

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://m.vk.com/uporols_you').text
    soup = BeautifulSoup(r, 'lxml')
    im = soup.find_all('img', class_="ph_img")
    try:
        for a in im:
            s = a.get('data-src_big').split('|')[0]
            tut.append(s)
        print(tut)  
    except:
        print('no have any links)')

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

def main():
    get_photo_from_page()

if __name__ == '__main__':
    main()