Python 爬取斗图啦图片

斗图啦
requests
BeautifulSoup4
代码
# -*- coding:utf-8 -*-
# pip install requests 框架
import requests
# pip install beautifulsoup4 框架
# pip install lxml 解析器
from bs4 import BeautifulSoup
import os

class doutuSpider(object):
    headers = {
        "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
    }

    def get_url(self, url):
        data = requests.get(url, headers=self.headers)
        soup = BeautifulSoup(data.content, 'lxml')
        totals = soup.findAll("a", {"class": "list-group-item"})
        for one in totals:
            sub_url = one.get('href')
            global path
            path = 'E:\\img' + '\\' + sub_url.split('/')[-1]
            os.mkdir(path)
            try:
                self.get_img_url(sub_url)
            except:
                pass
            pass
        pass

    def get_img_url(self, url):
        data = requests.get(url, headers = self.headers)
        soup = BeautifulSoup(data.content, 'lxml')
        totals = soup.findAll('div', {'class': 'artile_des'})
        for one in totals:
            img = one.find('img')
            try:
                sub_url = img.get('src')
            except Exception as e:
                raise e
            finally:
                urls = sub_url
            try:
                self.get_img(urls)
            except:
                print urls
                pass
            pass
        pass

    def get_img(self, url):
        filename = url.split('/')[-1]
        global path
        img_path = path + '\\' + filename
        img = requests.get(url, headers = self.headers)
        try:
            with open(img_path, 'wb') as f:
                f.write(img.content)
        except:
            pass
        pass

    def create(self):
        for count in range(1,10):
            url = 'https://www.doutula.com/article/list/?page={}'.format(count)
            print 'download {} page'.format(count)
            self.get_url(url)
            pass
        pass

if __name__ == '__main__':
    doutu = doutuSpider()
    doutu.create()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值