爬取图片

pycharm可以用ctrl+alt+l对代码进行自动格式规范化。

#!author zhang
import requests #引入requests包#爬取网站图片
import os
from bs4 import BeautifulSoup
r=requests.get("http://699pic.com/sousuo-218808-13-1.html") #请求网站
fengjing=r.content # 以html的格式返回
#print(s)
soup=BeautifulSoup(fengjing,"html.parser")
imags=soup.find_all(class_="lazy") #使用firbug查看属性
lujing="C:\\Users\\Administrator\\Desktop\\python3\\test_blog\\test_case\\twodir\\threedir\\jpg" #定义获取图片存储的路径
print(imags) #返回list对象
for i in imags:
title = i["title"]
jpg_url=i["data-original"]
print(title)
print(jpg_url)
print(" ")
with open(lujing+title+'.jpg',"wb") as f: #equests里get打开图片的url地址,content方法返回的是二进制流文件,可以直接写到本地
f.write(requests.get(jpg_url).content)

posted @ 2017-08-29 15:15  binghex  阅读(175)  评论(0编辑  收藏  举报