美文网首页
第一周第四课时

第一周第四课时

作者: 采矿 | 来源:发表于2016-05-21 17:52 被阅读3次
    运行结果 运行图片
    from bs4 import BeautifulSoup
    import requests
    import urllib.request
    data1 = []
    data2 = []
    path = 'C:/Users/zhangyang/Desktop/swift/'
    
    
    def get_url(url):
        web_data = requests.get(url)
        soup = BeautifulSoup(web_data.text, 'lxml')
        headerimgs = soup.select('#main-container > div > div.header > div.header-cover')
        contentimgs = soup.select('img')
        # for headerimg in headerimgs:
        #     #data1.append(headerimg.get('style'))
        #     data1 =data1[0][23:-2]
        #     print(data1)
        for contentimg in contentimgs:
            data2.append(contentimg.get('src'))
            print(data2)
    
    
    def get_pic(start, end):
        for i in range(start, end):
            get_url('http://weheartit.com/inspirations/taylorswift?page='+str(i))
        # for url in data1:
        #     urllib.request.urlretrieve(url, path + url.split('/')[-2] + url.split('/')[-1])
        #     print('done')
        for url in data2:
            urllib.request.urlretrieve(url, path + url[-10:])
            print('done')
    get_pic(0, 1)
    

    相关文章

      网友评论

          本文标题:第一周第四课时

          本文链接:https://www.haomeiwen.com/subject/nahorttx.html