#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
from lxml import etree
import os
# 1. 获取网页,通过requests工具包实现(集成环境自带)
# 如果没有就手动安装 pip install requests
url = "http://www.ivsky.com/tupian/ziranfengguang/"
response = requests.get(url)
# 响应码/状态码
# 200 表示成功/ok
# 404 资源不存在
# 500 服务器错误
print(response.content) # bytes类型
print(response.text) # 字符串类型
# r=read
# w=write
# a=append
# wb=WriteBytes
f = open("1.html", "wb")
f.write(response.content)
f.close()
# 2. 解析网页,引入lxml工具包
root = etree.HTML(response.content)
a_list = root.xpath("//ul[@class='tpmenu']/li/a")
# 缺点:会破坏列表中的数据
# a_list.pop(0)
# a_list.remove("所有分类")
for a in a_list[1:]:
title = a.xpath("text()")[0]
url1 = "http://www.ivsky.com" + a.xpath("@href")[0]
print(url1)
# 3. 文件夹操作,导入os模块,OperationSystem(操作系统)
# os.makedirs("文件夹路径")
# 绝对路径 c:/desktop/phone
# 相对路径 相对于当前文件所在路径
res = requests.get(url1)
root1 = etree.HTML(res.content)
a_list1 = root1.xpath("//div[@class='sline']/div/a")
for a1 in a_list1:
title1 = a1.xpath("text()")[0]
if not os.path.exists("imgs/"+title+"/"+title1):
os.makedirs("imgs/"+title+"/"+title1)
# -*- coding:utf-8 -*-
import requests
from lxml import etree
import os
# 1. 获取网页,通过requests工具包实现(集成环境自带)
# 如果没有就手动安装 pip install requests
url = "http://www.ivsky.com/tupian/ziranfengguang/"
response = requests.get(url)
# 响应码/状态码
# 200 表示成功/ok
# 404 资源不存在
# 500 服务器错误
print(response.content) # bytes类型
print(response.text) # 字符串类型
# r=read
# w=write
# a=append
# wb=WriteBytes
f = open("1.html", "wb")
f.write(response.content)
f.close()
# 2. 解析网页,引入lxml工具包
root = etree.HTML(response.content)
a_list = root.xpath("//ul[@class='tpmenu']/li/a")
# 缺点:会破坏列表中的数据
# a_list.pop(0)
# a_list.remove("所有分类")
for a in a_list[1:]:
title = a.xpath("text()")[0]
url1 = "http://www.ivsky.com" + a.xpath("@href")[0]
print(url1)
# 3. 文件夹操作,导入os模块,OperationSystem(操作系统)
# os.makedirs("文件夹路径")
# 绝对路径 c:/desktop/phone
# 相对路径 相对于当前文件所在路径
res = requests.get(url1)
root1 = etree.HTML(res.content)
a_list1 = root1.xpath("//div[@class='sline']/div/a")
for a1 in a_list1:
title1 = a1.xpath("text()")[0]
if not os.path.exists("imgs/"+title+"/"+title1):
os.makedirs("imgs/"+title+"/"+title1)
=========================2.下载图片到本地===============
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 24 17:13:25 2018
@author: mac
"""
import requests
url = "http://img.ivsky.com/img/tupian/t/201712/09/maotouying.jpg"
res1 = requests.get(url)
f = open("2.jpg","wb")
f.write(res1.content)
f.close()