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. POJ 3905

    加深了对有向边意义的理解了.2-SAT #include <iostream> #include <cstdio> #include <cstring> #incl ...

  2. C++ exit 与 return 浅析

    [摘要] 本文从代码形式.经常使用方式,相关概念,调用关系和比較分析,这5个维度浅析 exit 与 return 在C++的同样点与差别. [常见形式] exit(0):   正常执行程序并退出程序. ...

  3. 开源工作流BPM软件JFlow安装配置视频教程

    上周上传了一次,被抽了.刚開始不知道CSDN没有视频许可.造成一些爱好者无法下载,对此感到羞愧. 在下载后,依照文档内的连接,直接取出来就能够了,包括文档说明.视频教程两部分. http://down ...

  4. 出错Can't convert 'WebElement' object to str implicitly

  5. 【cl】多表查询(内、外连接)

    交叉连接(cross join):该连接产生的结果集笛卡尔积 a有7行,b有8行    a的第一行与b的每一行进行连接,就有8条a得第一行 7*8=56条 select a.real_name,s.u ...

  6. Codeforces Beta Round #95 (Div. 2) D. Subway 边双联通+spfa

    D. Subway   A subway scheme, classic for all Berland cities is represented by a set of n stations co ...

  7. win7 32位支持多大内存|win7 32位旗舰版最多能识别多少内存

    win7 32位支持多大内存|win7 32位旗舰版最多能识别多少内存 内存的大小决定系统运行速度,所以不少人认为只要内存加大就行了,其实这是不对的,因为win7 32位能支持的内存大小是有限制的,并 ...

  8. 【转】寻找最好的笔记软件:海选篇 (v1.0)

    原文网址:http://blog.sina.com.cn/s/blog_46dac66f01000b55.html   序言: 我见过的多数软件爱好者,无论是资深用户,还是初级用户,都有一个梦想:找到 ...

  9. js设计模式-组合模式

    组合模式是一种专为创建web上的动态用户界面而量身定制的模式.使用这种模式,可以用一条命令在多个对象上激发复杂的或递归的行为.这可以简化粘合性代码,使其更容易维护,而那些复杂行为则被委托给各个对象. ...

  10. BZOJ 2002 LCT板子题

    思路: LCT啊... (分块也行) 不过YOUSIKI出了一道“弹飞大爷” 就不能用分块水过去了 //By SiriusRen #include <cstdio> #include &l ...