Python瀑布流爬虫-爬取360美女图片+爬取百度美女图片(嘿嘿~)

今天编写脚本的目的是什么?看美女,看美女,看美女(重要的事情说三遍),不过看美女可以缓解疲劳,在这个浮躁的社会里我希望沉淀下自己的灵魂。嘿嘿~                                                                          

                                               

 

Python瀑布流爬虫

1、爬虫认识

2、Python与爬虫

3、关于爬取图片的设想

4、瀑布流爬虫的分析

实战:快速爬取360网站图片

实战:快速爬取百度图片瀑布流爬虫实现,批量下载图片!

1、爬虫认识

爬虫(spider:网络蜘蛛):是一个用脚本代替浏览器请求服务器获取服务器资源的程序。

扫描二维码关注公众号,回复: 11160637 查看本文章

我们知道什么是爬虫了,无非就是模拟浏览器去请求服务器,那什么是瀑布流爬虫呢?为啥这个名字这么吊?它又有什么前尘往事呢,让我们走进科学~不~走进它深入探究下……

                                                                                                                                                       -----Smile Mr

什么是瀑布流?

我们数据比较多的时候,为了更好用户体验节省服务器资源,我们进行渐进式的加载。

顾名思义,瀑布流肯定数据量很大,然后进行用户体验非常好的渐进式加载,让我们一直下拉就会有不同的数据展示,来来来,让我们通过图来走两步~

                         

没错,同志们,就是这味道,一下拉就会有神奇的事情发生,实在是太棒了~

2、Python与爬虫

上面的种种举例大家都可能在脑海中有一定的印象了,什么是瀑布流,不过,关键的时候还需要实操下,对不,想要看瀑布流网页效果的,请单击:  自行体验下,36D的非一般的感觉。

下面的例子按照:https://image.baidu.com/这个例子来做的,其实效果都是一样滴,不过百度的美女实在太暴力了,鄙人受不鸟,所以用唯美替代了,毕竟生活中我也是一个小清新呢~

                                                                                 

    结构分析

            

抓包分析

             

              

访问抓包数据

            

经过上面的紧张略微带刺激的分析,嘿嘿(流鼻血~),我发现,下面几个规律:

1.  图片来源js的渲染

2.  使用的ajax技术

3.  返回json数据

4.  每次下拉都会请求一个url地址

5.  每次请求的url地址具体的参数都有一定的规律

讲到这里老铁们,都差不多了解了,我们只需要拿到具体的 json数据(响应体),然后进行转换提取就欧了~真是so easy~

废话不多说,老规矩,直接上代码, 我们开始尝试编写代码,先介绍下我自己的环境啊哈~

Pycharm 编译器 2018.2.3版本

Python3.7.2 

requests(这是网络请求包,需要下载  :pip install requests)

实战:爬取360图片,将图片保存在本地

import  requests,time
from urllib import request
url = 'http://image.so.com/zj?ch=beauty&sn=30&listtype=new&temp=1'
headers= {
    'Referer': 'http://image.so.com/z?ch=beauty',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36'
}
str_data = '''ch: beauty
sn: 120
listtype: new
temp: 1
'''
send_data = {}
for data in str_data.splitlines():

line_data = data.split(': ')

if len(line_data) == 2:
            key,value = line_data
            if key and value:
                 send_data[key] = value
response = requests.get(url,headers=headers,params=send_data)

json_data = response.json()['list']

for index,src in enumerate(json_data):
    image_url = src['qhimg_url']
    try:
        image_name = './image/'+image_url[-8:]
        request.urlretrieve(url=image_url,filename=image_name)

    except Exception as e:
        print(e)
    else:
        print('{} is download'.format(image_name))

运行效果如下:

  

实战:百度图片下载

import time
import requests
from urllib import request
url = 'http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%94%AF%E7%BE%8E&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=&z=&ic=&hd=&latest=&copyright=&word=%E5%94%AF%E7%BE%8E&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=1&fr=&expermode=&selected_tags=&pn=30&rn=30&gsm=1e&1545019467831='
headers = {
'Referer': 'http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=%E5%94%AF%E7%BE%8E',
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
}

data_str = '''
tn: resultjson_com
ipn: rj
ct: 201326592
is:
fp: result
queryWord: 唯美
cl: 2
lm: -1
ie: utf-8
oe: utf-8
adpicid:
st:
z:
ic:
hd:
latest:
copyright:
word: 唯美
s:
se:
tab:
width:
height:
face:
istype:
qc:
nc: 1
fr:
expermode:
selected_tags:
pn: 30
rn: 60
gsm: 1e
1545019467831:
'''
send_data = {}
for line in data_str.splitlines():
    line_data = line.split(': ')
    if len(line_data) == 2:
        key,value = line_data
        if key and value:
            send_data[key] = value
response = requests.get(url= url,headers = headers,params=send_data)
content = response.json()['data']
print(content)
num = 1

for index,src in enumerate(content):
    img_url = src.get('middleURL')
    if img_url:
        name = './image/iamge_%s_%s.png'%('唯美',index)
        try:
            request.urlretrieve(url = img_url,filename=name)

        except Exception as e:
            print(e)
        else:
            print('%s is down'%name)
        time.sleep(2)

 接下来我们进行简单的封装:

from urllib import request
import requests,os,time
def get_image(keywords,num):
    url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E9%AC%BC%E5%88%80&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=&hd=&latest=&copyright=&word=%E9%AC%BC%E5%88%80&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&pn=150&rn=30&gsm=96&1545877953682='

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
        'Host': 'image.baidu.com',
        'Referer': 'https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1545877913556_R&pv=&ic=&nc=1&z=&hd=&latest=&copyright=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&hs=2&word=%E9%AC%BC%E5%88%80',
    }
    data = '''
    tn: resultjson_com
    ipn: rj
    ct: 201326592
    is:
    fp: result
    queryWord: 鬼刀
    cl: 2
    lm: -1
    ie: utf-8
    oe: utf-8
    adpicid:
    st: -1
    z:
    ic:
    hd:
    latest:
    copyright:
    word: 鬼刀
    s:
    se:
    tab:
    width:
    height:
    face: 0
    istype: 2
    qc:
    nc: 1
    fr:
    expermode:
    force:
    pn: 30
    rn: 30
    gsm: 96
    1545877953682:
    '''
    sendData = {}
    send_data = data.splitlines()
    try:
        for i in send_data:
            data_list = i.split(': ')
            if len(data_list) == 2:
                key,value = data_list
                if key and value:
                    sendData[key] = value
    except Exception as e:
        print(e)
    sendData['word'] = sendData['queryWord'] = keywords
    sendData['rn'] = str(30*num)
    response = requests.get(url=url,headers=headers,params=sendData)
    content = response.json()['data']
    for index,src in enumerate(content):
        # index =str(index).zfill(2)
        image_url = src.get('middleURL')
        if os.path.exists('image')
            pass
        else:
            os.mkdir('image')
        if image_url and os.path.exists('image'):
            name = './image/image_%s_%s.png'%(index,keywords)
            try:
                request.urlretrieve(url=image_url,filename=name)
            except Exception as e:
                print(e)
            else:
                print('%s is download'%name)
                time.sleep(1)
# print(content)
if __name__ == '__main__':
    keywords = input('请输入你要的内容:')
    num = int(input('请输入你想要的数量:'))
    get_image(keywords,num)

运行结果如下:

时间有限,该下班了,我就写到这里,封装的并不是很完美,但是我需要下班,放飞下自我喽~

生命诚可贵,爱情价更高,若为自由故,两个皆可抛

                                                                                                                                           -----Smile Mr

 

 

 

原创文章 49 获赞 23 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Smile_Mr/article/details/85773640