改进版爬虫 爬取漂亮妹纸图1

看到网上“小甲鱼”“小姐姐”写的“轮一只爬虫的自我修养4:OOXX",搜查下,爬的网站早已修复了此bug,突然想,能不能修改下再爬一次,(*^__^*) 嘻嘻!经过观察,发现一些规律,写了如下程序,试了一下,妹子图片又是一大堆,嘿嘿!需要的拿去不谢!不对的地方烦请帮忙指正,不多说,贴代码:

import urllib.request
import os

pagedict = { }
def url_open(url):
    req = urllib.request.Request(url)
    req.add_header('Usr-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36')
    response = urllib.request.urlopen(url)
    html = response.read()
    return html

def get_page(url,pages=2):
    
    html = url_open(url).decode('utf-8')
    a = html.find('//jandan.net/ooxx/',0)
    b = html.find('">', a)   # 从a开始,找到第一个">, 返回索引    
    c = html.find('</a>', b)   # 从b开始,找到第一个 ], 返回索引
    page = 'http:'+ html[a:b]    
    pagenum = html[b+2:c].strip()
    pagedict.update({pagenum:page})
    print(pagedict)    
    for i in range(pages): 
        a = html.find('//jandan.net/ooxx/',c)
        b = html.find('">', a)   # 从a开始,找到第一个">, 返回索引    
        c = html.find('</a>', b)   # 从b开始,找到第一个 ], 返回索引
        page = 'http:'+ html[a:b]    
        pagenum = html[b+2:c].strip()
        pagedict.update({pagenum:page})
#        print(pagedict)
    
def save_imgs(folder, img_addrs):
    for each in img_addrs:
        filename = each.split('/')[-1]
        print('http:' + each)
        with open(filename, 'wb') as f:
            img = url_open('http:' + each)
            f.write(img)
        
    

def find_imgs(url):
    html = url_open(url).decode('utf-8')
    img_addrs = []
    a = html.find('img src=')
    while a != -1:
        b = html.find('.jpg', a, a + 255)
        if b != -1:   # 找到一个 jpg
#            print('%s'%html[a+9 : b+4])
            img_addrs.append(html[a+9 : b+4])  # 加入列表
        else:         # 到不到, 移动b的位置
            b = a + 9
        a = html.find('img src=', b)  # 在b之后开始,再找img src
#    for each in img_addrs:
#         print(each)
    return img_addrs
    
    


def download_mm(folder='OOXX',pages=10):
    os.mkdir(folder)
    os.chdir(folder)
    
    url = "http://jandan.net/ooxx"
    page_num = (get_page(url))
    print(pagedict)
    for key in pagedict:
        page_url = pagedict[key]
        print(page_url)
        img_addrs = find_imgs(page_url)
        save_imgs(folder,img_addrs)
if __name__ == '__main__':
    download_mm()

运行:

>>> 
================= RESTART: C:\Users\softRoom\Desktop\ooxx.py =================
{'170': 'http://jandan.net/ooxx/MjAyMDAxMzEtMTcw#comments'}
{'170': 'http://jandan.net/ooxx/MjAyMDAxMzEtMTcw#comments', '169': 'http://jandan.net/ooxx/MjAyMDAxMzEtMTY5#comments', '下一页': 'http://jandan.net/ooxx/MjAyMDAxMzEtMTcw#comments" class="previous-comment-page'}
http://jandan.net/ooxx/MjAyMDAxMzEtMTcw#comments
http://ww1.sinaimg.cn/mw600/00745YaMgy1gbfug5554fj30ly0t9dj3.jpg
http://ww1.sinaimg.cn/mw600/00745YaMgy1gbfug4an1qj30ly0ccmyu.jpg
http://ww1.sinaimg.cn/mw600/00745YaMgy1gbfug6oki4j30ly0lwjsl.jpg
http://ww1.sinaimg.cn/mw600/00745YaMgy1gbfug6dnt0j30ly0lfq56.jpg
http://ww1.sinaimg.cn/mw600/00745YaMgy1gbfug64iplj30ly0iu75i.jpg
http://ww1.sinaimg.cn/mw600/00745YaMgy1gbfug60ib8j30ly0t90w9.jpg
http://ww1.sinaimg.cn/mw600/00745YaMgy1gbfug6qhrqj30ly0qvmy0.jpg
http://ww1.sinaimg.cn/mw600/00745YaMgy1gbfug5e2qnj30ly0gfmyg.jpg
http://ww1.sinaimg.cn/mw600/00745YaMgy1gbfug5msekj30ly0t9jtp.jpg
http://ww1.sinaimg.cn/mw600/00745YaMgy1gbfug6hk4xj30ly0ljgqy.jpg
http://ww1.sinaimg.cn/mw600/00745YaMgy1gbfug5hma8j30ly0ucgo0.jpg
http://ww1.sinaimg.cn/mw600/00745YaMgy1gbfug5p9y1j30ly0t9gnf.jpg
http://wx2.sinaimg.cn/mw600/0076BSS5ly1gbfudnp9q7j30u011idj8.jpg
http://wx4.sinaimg.cn/mw600/0076BSS5ly1gbfu9x6syaj30bv0hst9e.jpg
http://wx2.sinaimg.cn/mw600/0076BSS5ly1gbfu5rjy5aj30af0dw40u.jpg
http://wx4.sinaimg.cn/mw600/0076BSS5ly1gbfu1at54ij30m80xcn5q.jpg
http://wx4.sinaimg.cn/mw600/0076BSS5ly1gbftwphm5fj30i20r3ta6.jpg
http://wx4.sinaimg.cn/mw600/0076BSS5ly1gbftiuwi6bj317k0t0wjn.jpg
http://wx1.sinaimg.cn/mw600/0076BSS5ly1gbfterhmrbj30m80x3ju6.jpg
http://wx3.sinaimg.cn/mw600/0076BSS5ly1gbftardh65j30u018zn4t.jpg
http://wx4.sinaimg.cn/mw600/0076BSS5ly1gbfsy7944xj31e00s5kjl.jpg
http://wx1.sinaimg.cn/mw600/0076BSS5ly1gbfstvohbej30im0sg78l.jpg
http://wx4.sinaimg.cn/mw600/0076BSS5ly1gbfspdsk02j30jg0jg0up.jpg
http://wx1.sinaimg.cn/mw600/0076BSS5ly1gbfslm4fsvj31920u0gru.jpg
http://wx3.sinaimg.cn/mw600/0076BSS5ly1gbfshc7w9zj31900u0tm9.jpg
http://jandan.net/ooxx/MjAyMDAxMzEtMTY5#comments
http://wx3.sinaimg.cn/mw600/0076BSS5ly1gbfsd0qrdaj30u0190dqo.jpg
http://wx2.sinaimg.cn/mw600/0076BSS5ly1gbfs91srn2j316y0u0450.jpg
http://wx2.sinaimg.cn/mw600/0076BSS5ly1gbfs529cclj30ia0s5go5.jpg
http://wx4.sinaimg.cn/mw600/0076BSS5ly1gbfs0yuvxvj30m80m80w1.jpg
http://wx2.sinaimg.cn/mw600/0076BSS5ly1gbfrws27djj31920u0agt.jpg

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值