20170513 Python练习册0013爬取贴吧妹子照片

#!/usr/bin/env python
# -*-coding:utf-8-*-

# 第 0013 题: 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-)

import os
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'}
html = requests.get('http://tieba.baidu.com/p/2166231880?see_lz=1',headers=headers)
soup = BeautifulSoup(html.text,'lxml').select('div > img')
'''
soup_list = []
for href in soup:
soup_list.append(href['src'])
'''
soup_list = [href['src'] for href in soup]
#print(soup_list)
if not os.path.exists("D:/杉本有美"):#判断是否有这个文件夹
os.makedirs("D:/杉本有美")#如果没有就创建
os.chdir("D:/杉本有美")##切换该文件夹下面
for photo_href in soup_list:
name = photo_href[-9:-5]
#print(photo_href)
photo = requests.get(photo_href)
with open(name + '.jpg','ab') as f:
f.write(photo.content)
print('完成',name,'下载')
posted @ 2017-05-13 11:54  云ime  阅读(337)  评论(0编辑  收藏  举报