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. PowerDesigner里面将表中name列值拷贝到comment列

    完整版见https://jadyer.github.io/2015/06/03/powerdesigner-copy-name-to-comment/ /** * PowerDesigner里面将表中 ...

  2. LeetCode96_Unique Binary Search Trees(求1到n这些节点能够组成多少种不同的二叉查找树) Java题解

    题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...

  3. leetCode 74.Search a 2D Matrix(搜索二维矩阵) 解题思路和方法

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  4. ubuntu下安装AndroidStudio

    近期将电脑的操作系统换成了ubuntu,对于不习惯win8/win10的人来说ubuntu确实是一个不错的选择,主要的软件都ok了,至于QQ什么的,大家能够去找wine版的,或者直接下载一个叫Cros ...

  5. 网络 - 网关的作用、DNS的作用

    DNS的作用 域名系统.负责把域名翻译成ip,或者把ip翻译成域名. hosts文件用于静态的域名解析.优先级高于DNS解析. DNS服务器,负责解析域名到ip地址上. 114.114.114.114 ...

  6. H3C路由器查看序列号信息

    H3C MSR系列的路由器,查看本机的MAC地址.序列号信息和生产日期信息等可以使用dis device manuinfo 命令查看,以下是执行结果: slot 0 DEVICE_NAME       ...

  7. First Day Python介绍

    前言:刚开通的博客,谢谢博客园平台,管理辛苦! Python介绍 Python是一门高级的.面向对象的.解释性.脚本语言. 高级语言:贴近开发者,对应底层语言,底层语言贴近机器:java.C#.php ...

  8. 关于H5优化的一些问题

    required修改默认提示  : <form action="abc.php"> <input type="text" required o ...

  9. IE浏览器 ajax传参数值为中文时出现乱码的解决方案

    找了很多方法,发现就这个方法简单.直接.方便,直接推荐哦! 在汉字的位置加个保护措施:encodeURIComponent(parentid) 举个栗子>>> $.ajax({    ...

  10. angular实现动态的留言板案例

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...