【代码】第6章 Ajax数据爬取

本文介绍了一种使用Python爬取微博个人主页和今日头条图片的方法,包括Ajax请求解析、数据提取及存储到MongoDB的过程。

6.3 Ajax结果提取


# 微博个人主页爬取
from urllib.parse import urlencode
from pyquery import PyQuery as pq
from pymongo import  MongoClient

# 需要先开启MongoDB服务
client = MongoClient()
db = client['weibo']
collection = db['weibo']

import requests
base_url = 'https://m.weibo.cn/api/container/getIndex?'
headers = {
    'Host': 'm.weibo.cn',
    'Referer': 'https://m.weibo.cn/u/2830678474',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                  'AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/69.0.3497.100 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest',
}


def get_page(page):
    params = {
        'type': 'uid',
        'value': '2830678474',
        'containerid': '1076032830678474',
        'page': page
    }
    url = base_url + urlencode(params)
    try:
        resp = requests.get(url=url, headers=headers)
        if resp.status_code == 200:
            return resp.json()
    except requests.ConnectionError as e:
        print('Error:', e.args)


def parse_page(json):
    if json:
        items = json.get('data').get('cards')
        for item in items:
            item = item.get('mblog')
            # 这里,某个getIndex?可能返回不了含有mblog键的数据,返回None
            if item == None:
                # print(item, end='\n===================\n')
                continue
            weibo = {}
            weibo['id'] = item.get('id')
            weibo['text'] = pq(item.get('text')).text()
            weibo['attitudes'] = item.get('attitudes_count')
            weibo['comments'] = item.get('comments_count')
            weibo['reposts'] = item.get('reposts_count')
            yield weibo


def save_2_mongo(result):
    if collection.insert(result):   # 插入字典
        print('save to mongo')


if __name__ == '__main__':
    for page in range(1, 11):
        json = get_page(page)
        results = parse_page(json)
        for result in results:
            save_2_mongo(result)

6.4 分析ajax,爬取今日头条斋藤飞鸟美图

import requests
from urllib.parse import urlencode
import os
from hashlib import md5
from multiprocessing.pool import Pool

GROUP_START = 1
GROUP_END = 20


def get_page(offset):
    para = {
        'offset': offset,
        'format': 'json',
        'keyword': '斋藤飞鸟',
        'autoload': 'true',
        'count': 20,
        'cur_tab': 3,   # 3,而不是1,对应:“综合 视频 图集 用户” 中的图集
        'from': 'gallery'   # gallery,而不是search_tab
    }
    headers = {
        'Host': 'www.toutiao.com',
        'Referer': 'https://www.toutiao.com/search/?keyword=%E6%96%8B%E8%97%A4%E9%A3%9E%E9%B8%9F',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/69.0.3497.100 Safari/537.36',
        'x-requested-with': 'XMLHttpRequest',
    }
    url = 'https://www.toutiao.com/search_content/?' + urlencode(para)
    try:
        resp = requests.get(url=url)    # 不加headers=headers,似乎也能爬取
        if resp.status_code == 200:
            return resp.json()     # ajax请求,似乎返回json格式数据
    except requests.ConnectionError:
        return None


def get_image(json):
    # print(json)
    if json.get('data'):    # 如果data项不为空
        for item in json.get('data'):
            title = item.get('title')
            image = item.get('large_image_url')
            # 课本原文爬取参数项已失效,看来需要拿到article_url,在新的文章页面中进行爬取,此处只爬取大封面图作为示范
            yield {
                'image': image,
                'title': title
            }


def save_image(item):
    file_name = item.get('title')
    if not os.path.exists(file_name):
        os.makedirs(file_name)
    try:
        resp = requests.get(item.get('image'))
        if resp.status_code == 200:
            file_path = '{0}/{1}.{2}'.format(file_name, md5(resp.content).hexdigest(), 'jpg')
            if not os.path.exists(file_path):
                with open(file_path, 'wb') as f:
                    f.write(resp.content)
            else:
                print('Already Download ', file_path)
    except requests.ConnectionError:
        print('Failed to Save Image.')


def main(offset):
   json = get_page(offset)
   for item in get_image(json):
       print(item)
       save_image(item)


if __name__ == '__main__':
    pool = Pool()
    groups = ([x*20 for x in range(GROUP_START, GROUP_END+1)])
    print(groups)
    pool.map(main, groups)
    pool.close()
    pool.join() # 等待所有子线程结束(即关闭后)
本项目采用C++编程语言结合ROS框架构建了完整的双机械臂控制系统,实现了Gazebo仿真环境下的协同运动模拟,并完成了两台实体UR10工业机器人的联动控制。该毕业设计在答辩环节获得98分的优异成绩,所有程序代码均通过系统性调试验证,保证可直接部署运行。 系统架构包含三个核心模块:基于ROS通信架构的双臂协调控制器、Gazebo物理引擎下的动力学仿真环境、以及真实UR10机器人的硬件接口层。在仿真验证阶段,开发了双臂碰撞检测算法和轨迹规划模块,通过ROS控制包实现了末端执行器的同步轨迹跟踪。硬件集成方面,建立了基于TCP/IP协议的实时通信链路,解决了双机数据同步和运动指令分发等关键技术问题。 本资源适用于自动化、机械电子、人工智能等专业方向的课程实践,可作为高年级课程设计、毕业课题的重要参考案例。系统采用模块化设计理念,控制核心与硬件接口分离架构便于功能扩展,具备工程实践能力的学习者可在现有框架基础上进行二次开发,例如集成视觉感知模块或优化运动规划算法。 项目文档详细记录了环境配置流程、参数调试方法和实验验证数据,特别说明了双机协同作业时的时序同步解决方案。所有功能模块均提供完整的API接口说明,便于使用者快速理解系统架构并进行定制化修改。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值