• python获取ajax加载的数据


    """  
    https://www.duitang.com/napi/blogv2/list/by_search/?
    
    
    堆糖页面分析:
        使用Ajax加载,aferid是控制加载的图片和页面,从零开始,会提前加载下一页的Ajax数据
        第一页的图片是after_id从0到120,会提前加载下一页的after_id:124
    """
    import time
    from urllib.parse import urlencode
    import requests
    import re
    from threading import Thread
    from queue import Queue
    import json
    import os
    
    
    class ThreadFetchUrl(Thread):
        def __init__(self, url_queue, img_data_queue, headers):
            super().__init__()
            self.url_queue = url_queue
            self.headers = headers
            self.img_data_queue = img_data_queue
    
        def run(self):
            while not self.url_queue.empty():
                url = self.url_queue.get()
                response = requests.get(url, headers=self.headers).text
                """
                '''将Ajax中的json字符串写入文本'''
                page_name = re.findall('&after_id=(.*?)&', url)[0]
                with open(self.path + page_name + '.txt', 'w', encoding='utf-8') as f:
                    f.write(response.text)
                """
                dict_resp = json.loads(response)
                list = dict_resp['data']['object_list']
                for i in list:
                    id = i['photo']['id']
                    href = i['photo']['path']
                    self.img_data_queue.put((id, href))
            else:
                print('url_queue已空,线程结束')
    
    
    class ThreadSaveImg(Thread):
        ''' 将url添加到队列中 '''
        def __init__(self, img_data_queue, path):
            super().__init__()
            self.path = path
            self.img_data_queue = img_data_queue
    
        def run(self):
            ''' 线程执行代码块 '''
            while True:
                try:
                    id, href = self.img_data_queue.get(timeout=3)
                except:
                    print('等待超时,线程停止!')
                    break
                else:
                    postfix = href.split('.')[-1]
                    img_data = requests.get(href).content
                    with open(self.path + str(id) + '.' + postfix, 'wb') as f:
                        f.write(img_data)
                        print(f'图片{id},保存成功!')
    
    
    class ImageDuitang(ThreadFetchUrl, ThreadSaveImg):
        def __init__(self):
            self.url_prefix = 'https://www.duitang.com/napi/blogv2/list/by_search/?'
            self.headers = {
                'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.69",
            }
            self.url_queue = Queue()
            self.img_data_queue = Queue()
            if not os.path.exists('./duitang1'):
                os.mkdir('./duitang1')
            self.path = './duitang1/'
    
        def urlenqueue(self, page_num, kwd):
            for i in range(0, 24 * 5 * page_num, 24):
                params = {
                    'kw': '{}'.format(kwd),
                    'after_id': f'{i}',
                    'type': 'feed',
                    'include_fields': 'top_comments,is_root, source_link, item, buyable, root_id, status, like_count, like_id, sender, album, reply_count, favorite_blog_id',
                    '_type': '',
                }
                url = self.url_prefix + urlencode(params)
                self.url_queue.put(url)
    
    
        def main(self):
            kwd = input('请输入数据关键字:')
            page_num = int(input('请输入要抓取前几页:'))
            self.urlenqueue(page_num, kwd)
            for i in range(10):
                t1 = ThreadFetchUrl(self.url_queue, self.img_data_queue, self.headers)
                t1.start()
            for i in range(30):
                t2 = ThreadSaveImg(self.img_data_queue, self.path)
                t2.start()
    
    
    
    if __name__ == '__main__':
        DT = ImageDuitang()
        DT.main()
        print('\n&&&&&&&&&&主线程已结束&&&&&&&&&&\n')
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
  • 相关阅读:
    python脚本自动下载图片
    从工厂打螺丝到月薪9.5k测试工程师,我该满足吗?
    初识AOS --------AOS学习笔记系列
    A_03.Aosp11源码开发环境搭建
    OWASP top10 的介绍
    centOs 6.10 编译 qt 5.15.11
    web全栈框架next.js 使用api功能发送文件及多级路由 & useSwr
    Nexus存储库管理器搭建-Maven私服
    DeepStream--测试TrafficCamNet检测模型
    LeetCode 146. LRU 缓存
  • 原文地址:https://blog.csdn.net/qq248606117/article/details/132766428