Python实战计划学习作业1-4
来源:互联网 发布:面朝大海春暖花开 知乎 编辑:程序博客网 时间:2024/05/17 17:16
代码如下
from bs4 import BeautifulSoupimport requestsimport timeimport osimport urllibimport randomsingle_url = "http://weheartit.com/inspirations/taylorswift?scrolling=true&page="target_dir = "/Users/reed/Desktop/images"def make_file_name(path): if not os.path.isdir(target_dir): os.mkdir(target_dir) dot_ps = path.rindex('.') image_path = os.path.join(target_dir, str(random.random())[-9:] + path[dot_ps:]) print(image_path) return image_pathdef get_page(url, data=None): print(url) wb_data = requests.get(url) soup = BeautifulSoup(wb_data.text, 'lxml') image_divs = soup.find_all("div", class_="entry-preview ") if data is None: print('len of images: ' + str(len(image_divs))) for image_div in image_divs: images = image_div.find_all("img", class_="entry-thumbnail") image_url = images[0].get('src') print(image_url) urllib.request.urlretrieve(image_url, make_file_name(image_url)) print('.')def walk_pages(start, end): for num in range(start, end): print('..') get_page(single_url + str(num)) time.sleep(2)walk_pages(3, 4)
log如下
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/reed/PycharmProjects/web01/web_parse4.py..http://weheartit.com/inspirations/taylorswift?scrolling=true&page=3len of images: 24http://data.whicdn.com/images/197230154/superthumb.jpg/Users/reed/Desktop/images/643362636.jpg.http://data.whicdn.com/images/149640530/superthumb.jpg/Users/reed/Desktop/images/690903127.jpg.http://data.whicdn.com/images/158339547/superthumb.jpg/Users/reed/Desktop/images/279342765.jpg.http://data.whicdn.com/images/165672936/superthumb.png/Users/reed/Desktop/images/178600404.png.http://data.whicdn.com/images/197581707/superthumb.jpg/Users/reed/Desktop/images/001178043.jpg.http://data.whicdn.com/images/248682240/superthumb.png/Users/reed/Desktop/images/950703002.png.http://data.whicdn.com/images/198418513/superthumb.jpg/Users/reed/Desktop/images/644595508.jpg.http://data.whicdn.com/images/179303382/superthumb.jpg/Users/reed/Desktop/images/248896182.jpg.http://data.whicdn.com/images/179303481/superthumb.jpg/Users/reed/Desktop/images/693730015.jpg.http://data.whicdn.com/images/179311943/superthumb.jpg/Users/reed/Desktop/images/269555826.jpg.http://data.whicdn.com/images/266178739/superthumb.jpg/Users/reed/Desktop/images/847567642.jpg.http://data.whicdn.com/images/266238176/superthumb.jpg/Users/reed/Desktop/images/763393865.jpg.http://data.whicdn.com/images/266254626/superthumb.jpg/Users/reed/Desktop/images/915840388.jpg.http://data.whicdn.com/images/266315562/superthumb.jpg/Users/reed/Desktop/images/188748947.jpg.http://data.whicdn.com/images/266317310/superthumb.jpg/Users/reed/Desktop/images/442216453.jpg.http://data.whicdn.com/images/266317447/superthumb.jpg/Users/reed/Desktop/images/520423894.jpg.http://data.whicdn.com/images/266317495/superthumb.jpg/Users/reed/Desktop/images/290174412.jpg.http://data.whicdn.com/images/266317536/superthumb.jpg/Users/reed/Desktop/images/162962825.jpg.http://data.whicdn.com/images/266317654/superthumb.jpg/Users/reed/Desktop/images/647146572.jpg.http://data.whicdn.com/images/266318571/superthumb.jpg/Users/reed/Desktop/images/829340404.jpg.http://data.whicdn.com/images/266318590/superthumb.jpg/Users/reed/Desktop/images/077170361.jpg.http://data.whicdn.com/images/266318702/superthumb.jpg/Users/reed/Desktop/images/761035396.jpg.http://data.whicdn.com/images/266318671/superthumb.jpg/Users/reed/Desktop/images/996100169.jpg.http://data.whicdn.com/images/266374822/superthumb.jpg/Users/reed/Desktop/images/786120363.jpg.Process finished with exit code 0
1 0
- Python实战计划学习作业1-4
- Python实战计划学习作业1-1
- Python实战计划学习作业1-2
- Python实战计划学习作业1-3
- Python实战计划学习作业1-5
- Python实战计划学习作业2-1
- Python实战计划学习作业2-2
- Python学习-课后作业1
- PYTHON实战开发作业 -WEEK1
- Python学习day3作业
- Python学习day4作业
- Python学习day5作业
- python学习作业D1
- Python学习-PY4E作业
- 学习实战java虚拟机的计划图
- python机器学习实战4:Logistic回归
- 从零开始机器学习实战1---python安装
- python学习与实战
- 如何避免野指针?
- c#使用access的那些坑
- 51nod1743 JZOJ4899【NOIP2016提高A组集训第17场11.16】雪之国度
- Codeforces Round #379 (Div. 2)题解
- 使用Octave音频处理(二):基本数学信号处理
- Python实战计划学习作业1-4
- redis如何分配哈希槽
- 5个Linux 服务器发行版你值得拥有
- 数组相关
- 正则表达式-子表达式(四)
- 安卓Andriod使用入门(十四)【滑动布局】
- Activity的生命周期之onPause、onStop
- scrapy 初体验
- Template 模块模式