抓取妹子图

import requests

from bs4 import BeautifulSoup

import os

class mzitu():

def requst(self,url):#定义获取网页的函数

headers={‘user-agent‘:"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"}#模拟浏览器,以防反爬虫

content=requests.get(url,headers=headers)#获取网页内容

return content

def all_url(self,url):#定义获取主网页的函数

html=self.requst(url)#获取主网页的全部内容

all_a=BeautifulSoup(html.text,‘lxml‘).find(‘div‘,class_=‘all‘).find_all(‘a‘)#通过beautifulsoup来解析网页。找到主页面所有图片的内容

print(len(all_a))#打印一共有多少个图片

for a in all_a:#遍历所有的图片链接

title=a.get_text()#将图片属性a里的文字内容赋值给title

print(u‘开始保存:‘,title)

path=str(title).replace("?",‘_‘)#将title里的?替换为空

self.mkdir(path)按路径将片存储

href=a[‘href‘]#得到每个图片的内容链接

self.html(href)

def html(self,href):#定义获得每套图片每页的链接的函数

html=self.requst(href)#获取图片的网页内容

#因为一套图片拥有多页图片,每页都会有一个图片。所以要分析每套图片的每页图片链接

max_span=BeautifulSoup(html.text,‘lxml‘).find(‘div‘,class_=‘pagenavi‘).find_all(‘span‘)[-2].get_text()#解析到获取每套图片所有的页数

for page in range(1,int(max_span)+1):#遍历页数

page_url=href+‘/‘+str(page)#得到每页图片的链接,就是每套图片的链接加上歌页数

self.img(page_url)

def img(self,page_url):#得到每页图片的详细信息

img_html=self.requst(page_url)#获取每个页面的所有信息

img_url=BeautifulSoup(img_html.text,‘lxml‘).find(‘div‘,class_=‘main-image‘).find(‘img‘)[‘src‘]#解析页面的的内容

self.save(img_url)#保存图片

def save(self,img_url):#定义保存图片的函数

name=img_url[-9:-4]#图片的名字

img=self.requst(img_url)#获取图片的内容

f=open("E:\\Python\\Pythonprogram\\妹子图\\"+name+‘.jpg‘,‘ab‘)#打开保存路径

f.write(img.content)保存图片的内容

f.close()关闭

def mkdir(self,path):#保存路径的文件

path=path.strip()

isExists=os.path.join("E:\Python\Pythonprogram\妹子图%s"%path)是否存在这个路径

if not isExists:

print(u‘建一个名字叫做:‘,path,u‘的文件夹‘)

os.makedirs(os.path.join("E:\Python\Pythonprogram\妹子图%s"%path))#如果不存在创建一个去路径

os.chdir(os.path.join("E:\Python\Pythonprogram\妹子图%s"%path))#保存图片到路径

return True

else:

print(u‘名字叫做‘,path,u‘的文件夹已存在‘)

return False

Mzitu=mzitu()将妹子图类实例化

Mzitu.all_url(‘http://www.mzitu.com/all‘)#传入参数网址

时间: 2024-08-24 06:08:59

抓取妹子图的相关文章

Python 爬虫入门(二)——爬取妹子图

Python 爬虫入门 听说你写代码没动力?本文就给你动力,爬取妹子图.如果这也没动力那就没救了. GitHub 地址: https://github.com/injetlee/Python/blob/master/%E7%88%AC%E8%99%AB%E9%9B%86%E5%90%88/meizitu.py 公众号:[智能制造专栏].欢迎关注,分享智能制造与编程那些事. 爬虫成果 当你运行代码后,文件夹就会越来越多,如果爬完的话会有2000多个文件夹,20000多张图片.不过会很耗时间,可以在

Python 爬虫入门之爬取妹子图

Python 爬虫入门之爬取妹子图 来源:李英杰  链接: https://segmentfault.com/a/1190000015798452 听说你写代码没动力?本文就给你动力,爬取妹子图.如果这也没动力那就没救了. GitHub 地址: https://github.com/injetlee/Python/blob/master/%E7%88%AC%E8%99%AB%E9%9B%86%E5%90%88/meizitu.py 爬虫成果 当你运行代码后,文件夹就会越来越多,如果爬完的话会有2

python抓取妹纸图

import urllib.request import re import os re_img = re.compile(r'<p><img src="(.+?)" ></p>') # 预编译正则, 提高代码效率 re_url = re.compile(r'http://aimm\.92game\.net/xinggan/(\d+)\.html') f2 = open("456.txt") headers = { 'user-a

爬取妹子图

这是之前写的一个简单爬取妹纸图的爬虫,下面是源代码: # -*- coding: utf-8 -*- import requests,time,urllib.request,os from multiprocessing import Processfrom lxml import etree #os.chdir("meizhu")切换工作目录print (os.getcwd())#查看当前工作目录 headers = {"User-Agent" : "M

Python又来爬取妹子图啦,一个T的硬盘都不够用

淘女郎爬虫,可动态抓取淘女郎的信息和照片. 需要额外安装的第三方库 requests pip install requests pymongo pip install pymongo 学习Python过程中会遇到很多问题,你可以到我们的 python学习交流群[七 三 五,九 三 四,八 四 一],基础,进阶.从企业招聘人才需求 到怎么学习python,和学习什么内容都有免费系统分享,让你无论是自学还是找相应的培训都能让你少走弯路.希望可以帮助你快速了解Python,学习python 模块功能

python 爬取妹子图

作为一个python还没入门的小白,搞懂这段代码实在是很不容易,还要去学html的知识(#黑脸) 因此我加上了注释,比较好读懂点 #coding=utf-8 import time import requests from bs4 import BeautifulSoup import os import sys if(os.name == 'nt'): print(u'你正在使用win平台') else: print(u'你正在使用linux平台') header = {'User-Agent

使用request+Beautiful爬取妹子图

一.request安装 pip install requests request使用示例 import requests response = requests.get('https://www.mzitu.com/') # get()方法发送请求,获取HTML网页 response.status_code # 返回状态码 response.text #以文本格式返回网页内容 response.content # 以二进制形式返回 二.BeautifulSoup库 BeautifulSoup是P

爬取妹子图(requests + BeautifulSoup)

---恢复内容开始--- 刚刚入门爬虫,今天先对于单个图集进行爬取,过几天再进行翻页爬取. 使用requests库和BeautifulSoup库 目标网站:妹子图 今天是对于单个图集的爬取,就选择一个进行爬取,我选择的链接为:http://www.mzitu.com/123114 首先网站的分析,该网站有一定的反爬虫策略,所以应对就是加入headers(目前是小白,目前不知道具体为毛这样做) Hostreferer = { 'User-Agent':'Mozilla/4.0 (compatibl

aiohttp aiofiles asyncio异步爬取妹子图

import aiohttp import asyncio import aiofiles import async_timeout from bs4 import BeautifulSoup import time import os async def aitaotu(sem, urllist):     headers = {         'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/web