做了一个爬取妹子图某张索引页面的爬虫,主要用request和正则表达式。

  感谢 崔庆才大神的 爬虫教学视频 和 gitbook:

     B站:https://www.bilibili.com/video/av18202461/index_1.html

     gitBook:https://legacy.gitbook.com/book/germey/python3webspider/details

  

  源码:

  

#! user/bin/python
# coding=utf-8

import os
import re
import requests
from requests.exceptions import RequestException
from hashlib import md5

def download_from_detail(url):
    item = get_dict(url)
    save_images(item)

def get_dict(url):
    """
    :param url:
    :return:   {"title","image_url_list"}
    """
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36"
    }
    try:
        response = requests.get(url, headers=headers)
    except RequestException:
        print("request error")
        return None
    if response.status_code == 200:
        # parse html from gb2312 to utf-8
        response.encoding = "gb2312"
        html = response.text
        title = re.search('<title>(.*?)</title>', html, re.S).group(1).split()[0]
        images_url = re.findall('<img alt=.*?src="(.*?)" /><br />', html)
        return {
            "title": title,
            "images_url": images_url
        }
    else:
        return None

def save_images(item):
    """
        save image in file which name is title
    :param item:
    :return:
    """
    if not item:
        return

    # 1 affirm if directory exists
    if not os.path.exists(item["title"]):
        os.mkdir(item["title"])
    # 2 save all the images into folder
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36"
    }
    for url in item["images_url"]:
        try:
            image_response = requests.get(url, headers=headers)
        except RequestException:
            print("request image error")
            continue
        file_name = "{0}/{1}.{2}".format(item["title"], md5(image_response.content).hexdigest(), "jpeg")
        with open(file_name, "wb") as image_file:
            image_file.write(image_response.content)
            print("{0} writing successfully".format(file_name))

def get_page_index(url):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36"
    }
    try:
        response = requests.get(url, headers=headers)
        response.encoding="gb2312"
    except RequestException:
        print("request image error")
    if response.status_code == 200:
        page_index_urls = re.findall('<a href="(.*?)".*?target=\'_blank\'>',response.text,re.S)
        for url in page_index_urls:
            download_from_detail(url)

if __name__ == "__main__":
    url = "http://www.meizitu.com/a/pure.html"
    get_page_index(url)

主要问题:

  ① gb2312 转 utf-8

    

    response.encoding="gb2312"

  

requests+正则表达式 爬取 妹子图的更多相关文章

  1. Python 爬虫入门(二)——爬取妹子图

    Python 爬虫入门 听说你写代码没动力?本文就给你动力,爬取妹子图.如果这也没动力那就没救了. GitHub 地址: https://github.com/injetlee/Python/blob ...

  2. Python 爬虫入门之爬取妹子图

    Python 爬虫入门之爬取妹子图 来源:李英杰  链接: https://segmentfault.com/a/1190000015798452 听说你写代码没动力?本文就给你动力,爬取妹子图.如果 ...

  3. requests+正则表达式爬取ip

    #requests+正则表达式爬取ip #findall方法,如果表达式中包含有子组,则会把子组单独返回出来,如果有多个子组,则会组合成元祖 import requests import re def ...

  4. PYTHON 爬虫笔记八:利用Requests+正则表达式爬取猫眼电影top100(实战项目一)

    利用Requests+正则表达式爬取猫眼电影top100 目标站点分析 流程框架 爬虫实战 使用requests库获取top100首页: import requests def get_one_pag ...

  5. 爬取妹子图(requests + BeautifulSoup)

    刚刚入门爬虫,今天先对于单个图集进行爬取,过几天再进行翻页爬取. 使用requests库和BeautifulSoup库 目标网站:妹子图 今天是对于单个图集的爬取,就选择一个进行爬取,我选择的链接为: ...

  6. Requests+正则表达式爬取猫眼电影

    目标 提取出猫眼电影TOP100的电影名称.时间.评分.图片等信息,提取站点的URL为http://maoyan.com/board/4,提取的结果以文本的形式保存下来. 准备工作 请安装好reque ...

  7. scrapy 也能爬取妹子图?

    目录 前言 Media Pipeline 启用Media Pipeline 使用 ImgPipeline 抓取妹子图 瞎比比前言 我们在抓取数据的过程中,除了要抓取文本数据之外,当然也会有抓取图片的需 ...

  8. 使用Requests+正则表达式爬取猫眼TOP100电影并保存到文件或MongoDB,并下载图片

    需要着重学习的地方:(1)爬取分页数据时,url链接的构建(2)保存json格式数据到文件,中文显示问题(3)线程池的使用(4)正则表达式的写法(5)根据图片url链接下载图片并保存(6)MongoD ...

  9. Requests+正则表达式爬取猫眼电影(TOP100榜)

    猫眼电影网址:www.maoyan.com 前言:网上一些大神已经对猫眼电影进行过爬取,所用的方法也是各有其优,最终目的是把影片排名.图片.名称.主要演员.上映时间与评分提取出来并保存到文件或者数据库 ...

随机推荐

  1. NGINX conf 配置文件中的变量大全 可用变量列表及说明

    $args #这个变量等于请求行中的参数.$content_length #请求头中的Content-length字段.$content_type #请求头中的Content-Type字段.$docu ...

  2. [转]SVN 乱码问题

    以下来自:http://godchenmeng.iteye.com/blog/797727 最近研究SVN.发现在创建补丁包的时候出现这种情况. 在文件顶部不论是什么代码都会变成乱码.在文件中如果有注 ...

  3. SQL DATE_SUB 函数用法

    SQL DATE_SUB 是一个mySql函数.不象SQL DATE_ADD 函数 增加时间值,SQL DATE_SUB 将从一个日期/时间值中减去一个时间值(时间间隔). SQL DATE_SUB ...

  4. <转>Python3.x和Python2.x的区别

    这个星期开始学习Python了,因为看的书都是基于Python2.x,而且我安装的是Python3.1,所以书上写的地方好多都不适用于Python3.1,特意在Google上search了一下3.x和 ...

  5. thymeleaf switch在表格中的使用,遇到的空行问题

    switch在表格中的使用时 如果把<td>写在<div th:switch="${data.isShow}"> 里面导致外面出现很多空的<div&g ...

  6. JSR-303 结合spring 校验

    使用注解 一.准备校验时使用的JAR validation-api-1.0.0.GA.jar:JDK的接口: hibernate-validator-4.2.0.Final.jar是对上述接口的实现: ...

  7. Cisco动态路由 OSPF协议

    OSPF描述: 组播扩展OSPF  锁定 同义词 ospf一般指组播扩展OSPF 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . OSPF(Open Shortest Path Firs ...

  8. sqlldr详解

    Oracle 的SQL*LOADER可以将外部数据加载到数据库表中.下面是SQL*LOADER的基本特点: 1)能装入不同数据类型文件及多个数据文件的数据2)可装入固定格式,自由定界以及可度长格式的数 ...

  9. Rhythmk 一步一步学 JAVA (15) mybatis 入门学习-1

    1.mybatis 通过mybatis-generator-core-1.3.2 代码生成: 工具下载地址: https://code.google.com/p/mybatis/ 解压工具包 myba ...

  10. 批处理文件中获取当前所在路径的几种方法,以及写文件到txt

    @echo off setlocal EnableDelayedExpansion echo 当前正在运行的批处理文件所在路径:!cd! pause @echo off echo 当前目录是:%cd% ...