• python 爬取淘宝的模特照片


    前段时间花了一部分时间学习下正则表达式,总觉得利用正则要做点什么事情,所以想通过爬取页面的方式把一些美女的照片保存下来,其实过程很简单。

    1.首先读取页面信息;

    2.过滤出来照片的url地址;

    3.通过URL地址来保存图片;

    #-*-coding:utf-8 -*-
    '''爬取评论区的美女照片'''
    import requests
    import re
    RE_PICTURE_NAME = re.compile(r'(w)+.[A-Za-z]+$')
    RE_URL = re.compile(r'(w)+.(w)+.(w)+/(w)+/(w){2,}/(w)+.jpg')
    request_top_list = requests.get('https://mm.taobao.com/json/request_top_list.htm')
    RESPONSE = request_top_list.text #resp.text返回的是Unicode型的数据
    def Get_Reuest_Picture_List(RESPONSE):
        '''抓取页面的所有URL'''
        URLS = []
        for match in RE_URL.finditer(RESPONSE): #所有与pattern相匹配的全部字串,以迭代器的形式返回 与findall的区别,findall在字符串中找到正则表达式所匹配的所有子串,并组成一个列表返回
            URL = match.group()
            URLS.append('http://'+URL)
        return URLS
    
    def Download_Url_Picture(URL):
        '''下载图片'''
        REQUEST_CONTENT = requests.get(URL).content #resp.content返回的是bytes型也就是二进制的数据
        PICTURE_NAME = RE_PICTURE_NAME.search(URL).group(0) #根据url获取图片名字
        FILE_OBJECT = open(str(PICTURE_NAME),'wb') #以二进制的方式写文件
        FILE_OBJECT.write('picture\'+REQUEST_CONTENT)
        FILE_OBJECT.close()
    
    
    def Save_All_Picture(URLS):
        '''保存所有请求中的图片'''
        for URL in URLS:
            Download_Url_Picture(URL)
    
    if __name__ == '__main__':
        URLS = Get_Reuest_Picture_List(RESPONSE)
        Save_All_Picture(URLS)
  • 相关阅读:
    Invalid column name on sql server update after column create
    个人工资计算表
    xxxx
    excel cannot access the file there are several possible reasons
    .NET/VB.NET: solving the error “The system cannot find the file specified.” “Temp.NETFramework,Version=v4.0.AssemblyAttributes.vb”
    GIT
    时区
    create Excel file
    开发类分组
    判断是否已安装.net framework
  • 原文地址:https://www.cnblogs.com/mengyu/p/6664133.html
Copyright © 2020-2023  润新知