Python爬虫-萌妹子图片
最近发现一个可以看图的地方,一张张翻有点累,毕竟只有一只手(难道鼠标还能两只手翻?)。能不能下到电脑上看呢,毕竟不用等网速,还可以预览多张,总之很方便,想怎么就怎么,是吧?
刚好这几天在学python,刚好拿来试试手。
代码注释很详细就不做解释了。代码比较丑,有建议随便提,反正我也不改。
import requests as re
from bs4 import BeautifulSoup
import os
#import threading #定义一个爬去图片的类
class Spider:
# 传入图片地址,文件名,保存单张图片
def saveImg(self, imageURL, fileName):
u = re.get(imageURL)
data = u.content
f = open(fileName, 'wb')
f.write(data)
f.close() def saveBrief(self, content, name):
fileName = name + "/" + name + ".txt"
f = open(fileName, "w+")
f.write(content.encode('utf-8'))
#创建新目录
def mkdir(self,path):
path = path.strip()
# 判断路径是否存在
# 存在 True
# 不存在 False
isExists=os.path.exists(path)
# 判断结果
if not isExists:
# 如果不存在则创建目录
# 创建目录操作函数
os.makedirs(path)
return True
else:
# 如果目录存在则不创建,并提示目录已存在
return False
#下载每期的图片,并放入以标题为名字的目录中
def GetDeatilPage(self, url,encodeing="utf-8",folder=""):
res = re.get(url)
res.encoding=encodeing
soup = BeautifulSoup(res.text, "html.parser")
imgs = soup.select("article.article-content > p > img")#根据元素匹配所有的图片地址
if(len(imgs)>0):
#检查目录是否存在,不存在就创建
if(folder!=""):
folder=folder+"\\"
self.mkdir(folder+imgs[0].attrs.get("alt"))#创建目录
i=1
for img in imgs:
imgurl = img.attrs.get("src")#获取图片路径
self.saveImg(imgurl, img.attrs.get("alt")+"\\"+str(i)+".jpg")#保存图片
i=i+1
#获取页面上所有的期数连接
def GetAllPageUrl(self,url):
res=re.get(url)
soup=BeautifulSoup(res.text, "html.parser")
urls=soup.select("div.excerpts > article.excerpt > a.focus")
return [a.attrs.get('href') for a in urls] def loop(i):
spider=Spider()
urls=spider.GetAllPageUrl("http://www.52rkl.cn/mengmeizi/list_51_"+str(i)+".html")
for url in urls:
spider.GetDeatilPage(url) for i in range(1,25):
loop(i)
# threading.Thread(target=loop,args=(i),).start()
# threading.Thread(target=loop,args=((i)*3+1),).start()
# threading.Thread(target=loop,args=((i+1)*3+1,)).start()
# threading.Thread(target=loop,args=((i+2)*3+1),).start()
# threading.Thread(target=loop,args=((i+3)*3+1),).start()
# threading.Thread(target=loop,args=((i+4)*3+1),).start()
# threading.Thread(target=loop,args=((i+5)*3+1),).start()
# threading.Thread(target=loop,args=((i+6)*3+1),).start()
print("成功")
本来想实现多线程的,奈何按照网上说的都无效,还是单线程下载。知道怎么实现的还望不吝赐教。
git:https://github.com/dashenxian/ImageSpider
刚开始python学习。
Python爬虫-萌妹子图片的更多相关文章
- Python爬虫下载美女图片(不同网站不同方法)
声明:以下代码,Python版本3.6完美运行 一.思路介绍 不同的图片网站设有不同的反爬虫机制,根据具体网站采取对应的方法 1. 浏览器浏览分析地址变化规律 2. Python测试类获取网页内容,从 ...
- 如何用Python爬虫实现百度图片自动下载?
Github:https://github.com/nnngu/LearningNotes 制作爬虫的步骤 制作一个爬虫一般分以下几个步骤: 分析需求 分析网页源代码,配合开发者工具 编写正则表达式或 ...
- Python爬虫之网页图片抓取
一.引入 这段时间一直在学习Python的东西,以前就听说Python爬虫多厉害,正好现在学到这里,跟着小甲鱼的Python视频写了一个爬虫程序,能实现简单的网页图片下载. 二.代码 __author ...
- python爬虫获取百度图片(没有精华,只为娱乐)
python3.7,爬虫技术,获取百度图片资源,msg为查询内容,cnt为查询的页数,大家快点来爬起来.注:现在只能爬取到百度的小图片,以后有大图片的方法,我会陆续发贴. #!/usr/bin/env ...
- Python 爬虫之下载图片
from urllib import request import json #---------获取网页源代码-------------- def getHtml(url): response=re ...
- 【Python爬虫实战】 图片爬虫-淘宝图片爬虫--千图网图片爬虫
所谓图片爬虫,就是从互联网中自动把对方服务器上的图片爬下来的爬虫程序.有些图片是直接在html文件里面,有些是隐藏在JS文件中,在html文件中只需要我们分析源码就能得到如果是隐藏在JS文件中,那么就 ...
- python爬虫 分页获取图片并下载
--刚接触python2天,想高速上手,就写了个爬虫,写完之后,成就感暴增,用起来顺手多了. 1.源代码 #coding=utf-8 import urllib import re class Pag ...
- Python 爬虫学习 网页图片下载
使用正则表达式匹配 # coding:utf-8 import re import urllib def get_content(url): """ Evilxr, &q ...
- python爬虫requests 下载图片
import requests # 这是一个图片的url url = 'http://yun.itheima.com/Upload/Images/20170614/594106ee6ace5.jpg' ...
随机推荐
- optional的使用
:first-child { margin-top: 0px; } .markdown-preview:not([data-use-github-style]) h1, .markdown-previ ...
- 3D 数据
1.3D 图 首先在进行 3D Plot 时除了导入 matplotlib ,还要额外添加一个模块,即 Axes 3D 3D 坐标轴显示: import numpy as np import matp ...
- VB 性能优化点
1.将Single,Double和Currency类型的变量替换为Integer或Long类型的变量:10倍 2.避免使用变体: 慢:Dim FSO as object Set FSO = N ...
- 使用SpringBoot搭建一个简单的web工程
最近在学习SpringBoot,想写在博客园上记录一下,如有错误之处还望指出. 首先创建一个maven工程,不用勾选骨架. 在pom.xml文件中添加如下内容,使工程变成Springboot应用. & ...
- 安卓学习第一节--环境搭建及Android Studio 安装
1.安装JDK 2.安装AS 安装参考网址 https://www.cnblogs.com/xiadewang/p/7820377.html 下载网址: http://www.android-stud ...
- Vue+Webpack构建移动端京东金融(一、开发前准备)
一.开发前准备 1.node环境搭建 去node.js官网下载长期支持版本的node,采用全局安装,安装方式自行百度 网址:https://nodejs.org/zh-cn/ 安装后在cmd命令行运行 ...
- 行盒(line box)垂直方向的属性详解:从font-size、line-height到vertical-align
视觉格式化模型 在一个文档中,每个元素都被表示为0.1或多个矩形的盒子.确定这些盒子的尺寸, 属性 --- 像它的颜色,背景,边框方面 --- 和位置是渲染引擎的目标.① 在CSS中,使用标准盒模型描 ...
- 基于TeamCity的asp.net mvc/core,Vue 持续集成与自动部署
一 Web Server(Windows)端的配置 1.配置IIS,重要的是管理服务 1.1 配置FTP(前端NPM项目需要) 该步骤略,如果是在阿里云ESC上,需要开启端口21(用来FTP认证握手) ...
- mac ssh,mac xshell,xshell替代,ssh客户端,ssh工具,远程桌面加速
下载地址 Windows版下载地址:http://www.hostbuf.com/downloads/finalshell_install.exe Mac版,Linux版安装及教程:http://ww ...
- HDU-6153 A Secret 扩展KMP
题意:求一个字符串的所有后缀在母串中的出现次数*后缀的长度的总和. 题目链接:http://acm.split.hdu.edu.cn/viewcode.php?rid=22147273 思路:先预处理 ...