Python实战_3_第一周_第四节课程:爬取霉霉图片

我在we heart it 上爬去了冰与火之歌的图片。以下是我的代码

# coding:utf-8

from bs4 import BeautifulSoup
import requests


'''
这个脚本可以下载weheartit网站下冰与火之歌主题的图片
url:http://weheartit.com/vvitaa_way/collections/103074737-a-song-of-ice-and-fire
'''


# 因为weheartit采用的是动态加载,所以第一页和后面几页的图片获取方法有些区别
first_url = "http://weheartit.com/vvitaa_way/collections/103074737-a-song-of-ice-and-fire"
first_selector = "#content > div > div > div > div > a"

# 在after_url的后面加上页码
after_url = "http://weheartit.com/vvitaa_way/collections/103074737-a-song-of-ice-and-fire?scrolling=true&page="
after_selector = "body > div > div > div > a"

def get_img (url, selector, file_path=r"F:\111"):
    # 取得一个页码中子页面
    sub_url_list = []
    html = requests.get(url).text
    soup = BeautifulSoup(html, 'lxml')
    sub_url = soup.select(selector)
    for i in sub_url:
        sub_url_list.append(i.get("href"))

    # 取得子页面中图片的地址和名字
    for i in sub_url_list:
        sub_html = requests.get("http://weheartit.com"+i).text
        soup = BeautifulSoup(sub_html, 'lxml')
        img_tag = soup.select("#content > div > div > div > div > div > div > div > div > a > img")
        img_url = img_tag[0].get('src')
        img_name = img_tag[0].get('alt')
    # 下载图片
        with open(file_path + '\\' + img_name + '.jpg', 'wb') as f:
            img_byte = requests.get(img_url, stream = True)
            for chunk in img_byte:
                f.write(chunk)
        print(img_name + " save finish!")


get_img(first_url, first_selector, file_path = r"F:\a song of ice and fire")

for i in range(2,10):
    get_img(after_url + str(i), after_selector, file_path = r"F:\a song of ice and fire")

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值