import urllib
from urllib import request, parse
from lxml import etree

class CarModel:

    def __init__(self, search_name='车模', search_page=50, begin_page=1):
        self.name = search_name
        self.url = 'https://tieba.baidu.com/f?'
        self.search_page = search_page
        self.begin_page = begin_page
        self.tie_ba_list = []
        self.number = 0
        self.header = {'User_agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}

    def download_img(self, link, page_num, index01, index02):
        img_ = urllib.request.Request(link)
        respos = urllib.request.urlopen(img_)
        img_data = respos.read()
        file = open('../image/{0}_{1}_{2}.jpg'.format(page_num, index01, index02), 'wb')
        file.write(img_data)
        file.close()

    def find_image(self, link, page_num, index01):
        requests = urllib.request.Request(headers=self.header, url=link)
        responses = urllib.request.urlopen(requests)
        html = responses.read()     # 获取html信息
        new_html = etree.HTML(html)     # 将html转换
        image_link = new_html.xpath('//img[@class="BDE_Image"]/@src')   # xpath进行信息抽取
        tmp_num = 0
        for i in image_link:
            tmp_num += 1    # 进行图片编号
            self.download_img(i, page_num, index01, tmp_num)

    def find_link(self, link, page_num):
        requests = urllib.request.Request(headers=self.header, url=link)
        responses = urllib.request.urlopen(requests)
        html = responses.read().decode('utf-8')
        new_html = etree.HTML(html)
        # 寻找图片超链接
        link_list = new_html.xpath('//div[@class="threadlist_lz clearfix"]/div/a/@href')
        tmp_num = 0
        for i in link_list:
            tmp_num += 1
            tmp_link = 'https://tieba.baidu.com{0}'.format(i)
            self.find_image(tmp_link, page_num, tmp_num)

    def begin(self):
        for i in range(self.begin_page, self.search_page+1):
            tmp_pn = (i-1)*50
            words_01 = {'kw': self.name}
            words_02 = {'pn': tmp_pn}
            words_01 = urllib.parse.urlencode(words_01)
            words_02 = urllib.parse.urlencode(words_02)
            tmp_url ='{0}{1}&ie=utf-8&{2}'.format(self.url, words_01, words_02)
            self.find_link(tmp_url, tmp_pn/50)

if __name__ == '__main__':
    car = CarModel()
    car.begin()

最终爬取效果

Python爬虫基础--爬取车模照片的更多相关文章

  1. Python爬虫之爬取淘女郎照片示例详解

    这篇文章主要介绍了Python爬虫之爬取淘女郎照片示例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 本篇目标 抓取淘宝MM ...

  2. python --爬虫基础 --爬取今日头条 使用 requests 库的基本操作, Ajax

    '''思路一: 由于是Ajax的网页,需要先往下划几下看看XHR的内容变化二:分析js中的代码内容三:获取一页中的内容四:获取图片五:保存在本地 使用的库1. requests 网页获取库 2.fro ...

  3. Python爬虫之爬取慕课网课程评分

    BS是什么? BeautifulSoup是一个基于标签的文本解析工具.可以根据标签提取想要的内容,很适合处理html和xml这类语言文本.如果你希望了解更多关于BS的介绍和用法,请看Beautiful ...

  4. [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)

    转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...

  5. from appium import webdriver 使用python爬虫,批量爬取抖音app视频(requests+Fiddler+appium)

    使用python爬虫,批量爬取抖音app视频(requests+Fiddler+appium) - 北平吴彦祖 - 博客园 https://www.cnblogs.com/stevenshushu/p ...

  6. Python爬虫之爬取站内所有图片

    title date tags layut Python爬虫之爬取站内所有图片 2018-10-07 Python post 目标是 http://www.5442.com/meinv/ 如需在非li ...

  7. python爬虫实战---爬取大众点评评论

    python爬虫实战—爬取大众点评评论(加密字体) 1.首先打开一个店铺找到评论 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经 ...

  8. 初次尝试python爬虫,爬取小说网站的小说。

    本次是小阿鹏,第一次通过python爬虫去爬一个小说网站的小说. 下面直接上菜. 1.首先我需要导入相应的包,这里我采用了第三方模块的架包,requests.requests是python实现的简单易 ...

  9. python 爬虫之爬取大街网(思路)

    由于需要,本人需要对大街网招聘信息进行分析,故写了个爬虫进行爬取.这里我将记录一下,本人爬取大街网的思路. 附:爬取得数据仅供自己分析所用,并未用作其它用途. 附:本篇适合有一定 爬虫基础 crawl ...

随机推荐

  1. 【cl】子查询应用场景

    有推荐人/没有推荐人的 recommender_id is null / is not null 谁是推荐人=他的id是别人的recommender_id=某个人的recommender_id是他的i ...

  2. iWatch报错: Authorization request cancled

    iWatch报错: Optional (Error Domin = com.apple.healthkit Code = 5 "Autherization request canceled& ...

  3. java中String的21种使用方法

    (构造函数必须new出来) * public String (char[] vaue)                         将一个字符数组变成字符串(构造函数) * public Stri ...

  4. Makefileeasy犯错的语法

    1.引言 近期学习android的Build系统,接触最多的自然就是Makefile语法.发现非常多easy出错的地方,不避开这些错误语法没法真正了解Makefile的内涵.以下就介绍遇到的一些让人困 ...

  5. elasticsearch indices.recovery 流程分析(索引的_open操作也会触发recovery)——主分片recovery主要是从translog里恢复之前未写完的index,副分片recovery主要是从主分片copy segment和translog来进行恢复

    摘自:https://www.easyice.cn/archives/231 elasticsearch indices.recovery 流程分析与速度优化 目录 [隐藏] 主分片恢复流程 副本分片 ...

  6. mysql 从库落后主库太多优化

    有时候为了避免master.info和中继日志崩溃,在容忍额外的fsync()带来的开销,推荐设置sync_master_info = 1sync_relay_log = 1sync_relay_lo ...

  7. ios UISegmentedControl的使用简介

    初始化UISegmentedControl NSArray *arr = [[NSArray alloc]initWithObjects:@"轻拍",@"长按" ...

  8. C#调用SMS短信接口,轻松搞定发送短信的任务。。。。

    首先我们需要去这里http://sms.webchinese.cn/申请一个账号和短信接口秘钥,在该网址下有许多语言的demo介绍,下面我主要为大家贴一个C#中的Helper类: using Syst ...

  9. [转]C#使用Window图片查看器打开图片

    //建立新的系统进程 System.Diagnostics.Process process = new System.Diagnostics.Process(); //设置文件名,此处为图片的真实路径 ...

  10. Dalvik虚拟机和JVM的对比

    Dalvik虚拟机与Java虚拟机有着很多相似的特性,都支持GC,JIT,JNI等等.其主要区别在于文件格式以及指令集不同,下面对两者的特性进行比较与讨论. Difference1:文件格式 Dalv ...