美文网首页爬虫
python爬取性感美女图片

python爬取性感美女图片

作者: 撸大师 | 来源:发表于2016-08-06 09:51 被阅读2494次

    需求:最近对python爬虫感兴趣,于是也依葫芦画瓢试着用爬虫爬取之前喜欢的网站上的美女图片,网站:<code>http://www.mm131.com/xinggan</code>,其中每一套图都是一张一个页面,存一套图如果是手动得点翻几十个页面,但现在用爬虫的话,就很方便了,只需输入套图的id,轻轻松松就可以把美女存到硬盘了。

    大神说:talk is cheap show me the code!

    接下来说下一般网页爬虫的的过程

    1.查看目标网站页面的源代码,找到需要爬取的内容
    2.用正则或其他如xpath/bs4的工具获取爬取内容
    3.写出完整的python代码,实现爬取过程

    1.目标网址

    url:http://www.mm131.com/xinggan/2373.html

    美女图片
    漂亮吧!!

    2.分析源代码

    F12可以找到如下2行内容

    src="http://img1.mm131.com/pic/2373/1.jpg"
    span class="page-ch">共56页
    

    我们得到如下信息

    我们点击第二页和第三页继续看源码

    3.爬取图片

    我们试着爬取第一个页面的图,直接上代码:

    import requests
    import re
    url = 'http://www.mm131.com/xinggan/2373.html'
    html = requests.get(url).text           #读取整个页面为文本
    a = re.search(r'img alt=.* src="(.*?)" /',html,re.S)  #匹配图片url
    print(a.group(1))</code>
    得到:
    http://img1.mm131.com/pic/2373/1.jpg
    

    接下来我们需要把图片保存在本地:

    pic= requests.get(a, timeout=2)  #time设置超时,防止程序苦等
    fp = open(pic,'wb')    #以二进制写入模式新建一个文件
    fp.write(pic.content)  #把图片写入文件
    fp.close()
    

    这样,你的本地就会有第一张美女图了,

    第一张既然已经保存了,那剩下的也都不要放过,继续放代码:

    4.继续把代码补全

    载入所需模块,并设置图片存放目录

    #coding:utf-8
    import requests
    import re
    import os
    from bs4 import BeautifulSoup
    pic_id = raw_input('Input pic id: ')
    os.chdir("G:\pic")
    homedir = os.getcwd()
    print("当前目录 %s" % homedir )
    fulldir = unicode(os.path.join(homedir,pic_id),encoding='utf-8')  #图片保存在指定目录,并根据套图id设置目录
    if not os.path.isdir(fulldir):
        os.makedirs(fulldir)
    

    因为需要不停翻页才能获取图片,所以我们先获取总页数

    url='http://www.mm131.com/xinggan/%s.html' % pic_id
    html = requests.get(url).text
    #soup = BeautifulSoup(html)
    soup = BeautifulSoup(html, 'html.parser')  #使用soup取关键字,上一行会报错UserWarning: No parser was explicitly specified
    ye = soup.span.string
    ye_count = re.search('\d+',ye)
    print('pages:共%d页' % int(ye_count.group()))
    

    主函数

    def downpic(pic_id):
        n = 1
        url='http://www.mm131.com/xinggan/%s.html' % pic_id
        while n <= int(ye_count.group()):  #翻完停止
            #下载图片
            try:
                if not n == 1:
                    url='http://www.mm131.com/xinggan/%s_%s.html' % (pic_id,n) #url随着n的值变化的
                html = requests.get(url).text
                pic_url = re.search(r'img alt=.* src="(.*?)" /',html,re.S)   #使用正则去关键字
                pic_s = pic_url.group(1)
                print(pic_s)
                pic= requests.get(pic_s, timeout=2)
                pic_cun = fulldir + '\\' + str(n) + '.jpg'
                fp = open(pic_cun,'wb')
                fp.write(pic.content)
                fp.close()
                n += 1
            except requests.exceptions.ConnectionError:
                print("【错误】当前图片无法下载")
                continue
    if __name__ == '__main__':
        downpic(pic_id)
    
    • 程序跑起来
    python python

    5.好了,收工

    看着硬盘里的图片是不是爽歪歪,当然爬虫能干的 不光只是下图片,它还可以做其他一些事,比如爬取12306火车信息,或求职网的职位信息,或者其他,总之赶紧把此技能get起来,丰富起来吧!

    参考http://www.jianshu.com/p/19c846daccb3

    相关文章

      网友评论

      • 憧憬Licoy:现在貌似有防盗链设置,无法抓取成功
      • 02befbf2a1e5:请教一下pic_id = raw_input('Input pic id: ') / os.chdir("G:\pic")
        1. 应该用input代替raw_input吧?看代码应该是使用的python3
        2. 如上代码跑起来应该手动输入网址吗
        3.os.chdir提示找不到目标文件夹,本地手动建立也不行,谢谢
        撸大师:@RainMo python2.7兼容3.x,所以可以用raw_input;不是手动输入网址,是你运行后让你输入一个套图的id,文中有说明和示例;os.chdir这个切换的目录肯定要手动建的,你也可以在脚本中创建

      本文标题:python爬取性感美女图片

      本文链接:https://www.haomeiwen.com/subject/uzsksttx.html