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
- Python实战_3_第一周_第四节课程:爬取霉霉图片
- Python实战_2_第一周_第三节课程:真实世界中的网页解析
- Python实战_1_第一周_第二节练习项目:爬取商品信息
- Python实战_0_第一周_第一节练习项目:动手做自己的网页
- 第一周课程
- C++_系列自学课程_第_10_课_表达式_《C++ Primer 第四版》
- 中国大学MOOC课程《Python语言程序设计》课后练习第一周
- Andrew Ng deeplearning.ai专项课程第四课Convolutional Neural Networks第一周笔记
- Rcurl--炼数成金课程第一周
- 【GeekBand】第一周课程笔记
- 学习python第一周
- leetcode-python 第一周
- python作业第一周
- python学习第一周
- Fireworks焦祺_第一周
- 我的第一周cpp课程
- Linux内核分析第一周课程小结
- 博览网C++课程 第一周
- navicat for mysql 中sql脚本乱码问题
- 网站开发从陌生到了解
- poj 1611 The Suspects 并查集
- cocos2dx 概要
- C++ 面向对象(二)—— 操作符重载
- Python实战_3_第一周_第四节课程:爬取霉霉图片
- Android:Intent传递SparseIntArray
- LeetCode 97. Interleaving String(字符串交织)
- nyoj711最舒适的路线 (枚举 + 并查集 )
- Tomcat Server.xml详解
- 百度之星2016初赛
- C++ 面向对象(三)—— 类之间的关系
- 31 Spark资源调度
- iOS开发(OC)——AFNetworking之文件上传