爬取Taylor图片

一、作业目的

作业描述

二、作业思路

观察可以发现每一页有24张图片,页面控制由page后的数字来控制,即:

main_url = 'http://weheartit.com/inspirations/taylorswift?page={}'.format(i)

然后常规方法解析这些url从里面获取图片的url,将其保存在一个列表里:

def get_pic_url():
    pic_urls = []
    for i in range(1, 11):
        main_url = 'http://weheartit.com/inspirations/taylorswift?page={}'.format(i)
        time.sleep(0.1)
        wb_data = requests.get(main_url)
        if wb_data.status_code != 200:
            continue
        soup = BeautifulSoup(wb_data.text, 'lxml')
        pic_url_raw = soup.select('img.entry-thumbnail')
        for a_url in pic_url_raw:
            pic_urls.append(a_url.get('src'))
    return pic_urls

定义一个函数下载这些链接:

def down(a_url):
    pic_res = requests.get(a_url)
    if pic_res.status_code != 200:
        return
    filename = a_url.split('/')[-2] + '.' + a_url.split('.')[-1]
    target = './Taylor/' + filename
    with open(target, 'wb') as pic_file:
        pic_file.write(pic_res.content)
    print('%s => %s' % (url, target))

这里有两个需要注意的地方:

  1. filename的获取使用了字符串的split方法
  2. target的目录需要手动创建,也可以使用函数创建,方法如下:
def mkdir(path):
    # 去除首位空格
    path = path.strip()
    # 去除尾部\\
    path = path.strip('\\')
    # 判断路径是否存在
    # 存在    True
    # 不存在  False
    is_exists = os.path.exists(path)
    if not is_exists:
        # 如果不存在则创建目录
        print(path + '创建成功')
        # 创建目录操作函数
        os.mkdir(path)
    else:
        # 如果目录存在则不创建并提示已存在
        print(path + '已存在')
        return False
# end of the function
mkpath = '.\\Taylor\\'
mkdir(mkpath)

三、完整代码

from bs4 import BeautifulSoup
import time
import requests
import os

def mkdir(path):
    # 去除首位空格
    path = path.strip()
    # 去除尾部\\
    path = path.strip('\\')
    # 判断路径是否存在
    # 存在    True
    # 不存在  False
    is_exists = os.path.exists(path)

    if not is_exists:
        # 如果不存在则创建目录
        print(path + '创建成功')
        # 创建目录操作函数
        os.mkdir(path)
    else:
        # 如果目录存在则不创建并提示已存在
        print(path + '已存在')
        return False

mkpath = '.\\Taylor\\'
mkdir(mkpath)


def get_pic_url():
    pic_urls = []
    for i in range(1, 11):
        main_url = 'http://weheartit.com/inspirations/taylorswift?page={}'.format(i)
        time.sleep(0.1)
        wb_data = requests.get(main_url)
        if wb_data.status_code != 200:
            continue
        soup = BeautifulSoup(wb_data.text, 'lxml')
        pic_url_raw = soup.select('img.entry-thumbnail')
        for a_url in pic_url_raw:
            pic_urls.append(a_url.get('src'))
    return pic_urls


def down(a_url):
    pic_res = requests.get(a_url)
    if pic_res.status_code != 200:
        return
    filename = a_url.split('/')[-2] + '.' + a_url.split('.')[-1]
    target = './Taylor/' + filename
    with open(target, 'wb') as pic_file:
        pic_file.write(pic_res.content)
    print('%s => %s' % (url, target))


for url in get_pic_url():
    down(url)
    time.sleep(0.1)

四、参考

http://www.qttc.net/201209207.html
//www.greatytc.com/p/73973880c204

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容