您的位置:首页 > 编程语言 > Python开发

原创|如何使用Python爬虫优雅的批量下载妹子图?|Python爬妹子

2018-02-09 20:00 821 查看
原创文章,转载请注明出处和作者,谢谢。
0X001
如题,本篇文章来讨论一下如何优雅的爬妹子图
实际上,我的代码可以还爬下这个站所有的“帅哥”“卡通”“唯美图片”“风景”“壁纸”“搞笑”栏目下图片。上图





本文爬的是 美女栏目
来看看我们的目标网站:http://www.mmonly.cc

0X002
嗯,不错。我说的是域名....{:1_119:}
好,我们来分析一下妹子图所在的地址是 http://www.mmonly.cc/mmtp/  妹子图 http://www.mmonly.cc/mmtp/list_9_773.html 注意上面链接的红色部分,773表示的是最后一页,每页是3*8 =24个图集,或者说是主题。
第一页也就是 http://www.mmonly.cc/mmtp/list_9_1.html
每个图集里面又有很多妹子图

思路 我想的是:
1.获得所有主题页面:用range()函数和format()函数批量生成 773个链接
2.从这773个链接中解析出图集的名字和第一张图片链接并用sqlite3保存起来(mmonly.db)
3.生成图集的所有图片页面链接 例如 
主题:超性感美女潘安安半球浴室写真  对应链接 http://www.mmonly.cc/mmtp/xgmn/5819.html   第一张图 http://www.mmonly.cc/mmtp/xgmn/5819_2.html 第二张图 http://www.mmonly.cc/mmtp/xgmn/5819_3.html 第三张图
一个主题图集有多少张图,就有多少个页面。
4.将所有图片页面链接解析出图片地址和对应的图片标题
5.将所有的图片地址和标题保存在sqlite3中
6.从数据库中获得地址和标题 进行下载

0X004
 代码如下(实现思路 1,2):import requests
import sqlite3
import os
from bs4 import BeautifulSoup

firs = 'http://www.mmonly.cc/mmtp/list_9_{}.html'

def found_db():
con = sqlite3.connect('mmonly.db')
c = con.cursor()
sql = '''CREATE TABLE mmonly
(title TEXT NOT NULL,
url TEXT NOT NULL);'''
try:
c.execute(sql)

except:
print("Failed")
else:
print("OK")

con.commit()
con.close()

def insert(a,b):
con = sqlite3.connect('mmonly.db')
c = con.cursor()
sql = '''INSERT INTO mmonly (title,url) VALUES ('%s','%s');''' % (a, b)
try:
c.execute(sql)
except:
print("Failed")
else:
print("***"+ a + "***"+"插入数据库成功")

con.commit()
con.close()

def check(fir):
r = requests.get(fir)
r.encoding = 'gbk'
soup = BeautifulSoup(r.text, "html.parser")
a_soup = soup.select(".title span a")
for b_soup in a_soup:
insert(b_soup.text,b_soup.get("href"))
return 0

def mainfun():
for i in range(1, 773):
newurl = firs.format(i)
check(newurl)
print("***************************************")
print("程序启动成功,程序仅用于学习交流,是否同意?")
print("######################################")
print("请输入yes或no:")
s = input()
if s == 'yes':
found_db()
mainfun()
else:
os._exit(0)
0X005 (实现思路3,4,5)
import requests
import sqlite3
import os
from bs4 import BeautifulSoup

headers={ 'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'}

def found_db():
con = sqlite3.connect('mmonly_img_url.db')
c = con.cursor()
sql = '''CREATE TABLE mmonly
(title TEXT    NOT NULL,
url TEXT    NOT NULL);'''
try:
c.execute(sql)
except:
print("Database creation failure!")
else:
print("Database creation success!")

con.commit()
con.close()

#查询已经入库的 名字和图集链接
def select():
con = sqlite3.connect('mmonly.db')
c = con.cursor()
sql = "select url from mmonly"
c.execute(sql)
res = c.fetchall()
return res

def fir(sec):
sec = sec
r = requests.get(sec,headers = headers)
r.encoding = "gbk"
soup = BeautifulSoup(r.text, "html.parser")

sec_url = []
c_soup = soup.select(".totalpage")[0].text
t_text = sec.rstrip(".html") + "_" + "{}" + ".html"
for a_sec in range(2,int(c_soup) + 1):
sec_url.append(t_text.format(a_sec))
sec_url.append(sec)
return sec_url

def tu(sec):
r = requests.get(sec, headers = headers)
r.encoding = "gbk"
soup = BeautifulSoup(r.text, "html.parser")
a_soup = soup.select(".big-pic p a img")
con = sqlite3.connect('mmonly_img_url.db')
c = con.cursor()
for b_soup in a_soup:
a = b_soup.get("src")
b = b_soup.get("alt")
sql = '''INSERT INTO mmonly (title,url) VALUES ('%s','%s');''' % (b, a)
try:
c.execute(sql)
except:
print("***" + b + "***" + "插入失败")
else:
print("***" + b + "***" + "插入数据库成功")
con.commit()
con.close()
return "OK"
def mainfun():
for ress in select():
for firs in fir(ress[0]):
tu(firs)

print("***************************************")
print("程序启动成功,程序仅用于学习交流,是否同意?")
print("######################################")
print("请输入yes或no:")
s = input()
if s == 'yes':
found_db()
mainfun()
else:
os._exit(0)


0X006测试环境:
Windows 7 x64 旗舰版
IED :Pycharm 2017 64位
Python3
使用的库 requests,BeautifulSoup4,sqlite3,os(可选)
windows环境 在安装目录\Scripts  下执行如下命令即可安装

pip install requests
pip install BeautifulSoup4

os,sqlite3 python3已经集成

0X007
至于思路六你们自己实现吧!下载并不难...

Ps:写的爬虫不多,也没有多完善,不足之处比较多,甚喷!

我们的华夏官方群号码 588142303
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: