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

来源:互联网 发布:kmp算法的next函数程序 编辑:程序博客网 时间:2024/06/04 17:54

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

# coding:utf-8from bs4 import BeautifulSoupimport 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")
0 0
原创粉丝点击