import requests
import os
from lxml import etree
class spider(object):
def __init__(self):
self.headers = {
"user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
"referer": "https://www.mzitu.com/"
}
# 1.取得网站数据
def requsts_dada(self):
response = requests.get("https://www.mzitu.com/",headers = self.headers)
html = etree.HTML(response.text)
# 2.获取大链接地址和分类标题
class_tit = html.xpath('//ul[@id="pins"]/li/span/a/text()')
class_href = html.xpath('//ul[@id="pins"]/li/span/a/@href')
# print(class_href)
# 建立文件夹
for tit,src in zip(class_tit,class_href):
if os.path.exists(tit) == False:
os.mkdir(tit)
self.download_img_data(src,tit)
def download_img_data(self,src,tit):
# 3.取得分类页面数据
response = requests.get(src,headers = self.headers)
html = etree.HTML(response.text)
img_num = html.xpath('//div[@class="pagenavi"]/a[5]/span/text()')
for i in range(1,int(img_num[0])+1):
# 4.获取分类页面大图标题及大图链接
img_tit = html.xpath('//h2/text()')
img_data = requests.get(src + "/" + str(i),headers = self.headers)
html = etree.HTML(img_data.text)
img_href = html.xpath('//div[@class ="main-image"]/p/a/img/@src')
for imgtit,imgsrc in zip(img_tit,img_href):
jpg_name = tit + "\\" + tit + str(i) + ".jpg"
response = requests.get(imgsrc,headers = self.headers).content
print("正在下载图片……")
# 5.保存图片
with open(jpg_name,"wb") as f:
f.write(response)
spider = spider()
spider.requsts_dada()
2019-01-14 图片爬取
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 参考资料:利用google图片搜索收集图片数据集 由于需要的数据集是用于特定的场景,明星脸需要自己搜集。学习用Py...
- 先上图 结果 1.导入requests 2.导入beautifulsoup 3.用requests获取网站get方...
- 本人长期出售超大量微博数据、旅游网站评论数据,并提供各种指定数据爬取服务,Message to YuboonaZh...
- 还是先放图(网页) 可以看出新闻在div为id=news-item 然后在H2标题的a标签中 示例代码 可以看到直...