Week1_Practice4

GetImage.py

import urllib.request
import time
path='Image/'
def get_image(url):

    urllib.request.urlretrieve(url,path+url[-5:])
    print("Done one")
    # time.sleep(1)


get_image('https://cbu01.alicdn.com/img/ibank/2013/513/346/908643315_1282632295.220x220.jpg')

GetImagePage.py

import requests
from bs4 import BeautifulSoup
import time

def get_image_href(url):
    html=requests.get(url)
    bshtml=BeautifulSoup(html.text,'lxml')

    # 获取照片链接

    image_hrefs=bshtml.select('div.grid-thumb.grid-responsive > div > div > div > a > img')
    hrefs=[]
    for href in image_hrefs:
        hrefs.append(href.get('src'))
        time.sleep(2)
        print("Get One")
    return hrefs

get_image_href('http://weheartit.com/inspirations/taylorswift?page=4')

week1_4.py

from GetImagePage import get_image_href
from GetImage import get_image

def main():
    urls=["http://weheartit.com/inspirations/taylorswift?page={}".format(str(i)) for i in range(1,21)]

    for url in urls:
        part_url=get_image_href(url)
        for imageUrl in part_url:
            get_image(imageUrl)

main()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容