python - 如何提高爬虫爬取的速度?
天蓬老师
天蓬老师 2017-04-18 09:25:20
[Python讨论组]

写了个简单的协程爬虫爬取B站用户信息,代码如下:

import requests
import re
import json 
import datetime
import asyncio

def get_info(uid):
    url_info = "http://space.bilibili.com/ajax/member/GetInfo?mid=" #基本信息
    uid = str(uid)
    return loop.run_in_executor(None, requests.get, url_info+uid)

    
async def user_info(num):
    """
    uid = mid
    
    info
    'birthday': 生日
    'regtime': 注册时间
    'attentions': 关注
    'spacesta': ? 
    'attention': 关注人数 
    'toutu': ?
    'sex': 性别
    'fans': 粉丝数
    'friend': 关注?
    'level_info': 等级
    'place': 地理位置
    'face': 头像
    'name': 昵称
    'sign': 简介
    """
    for uid in range(num, num+10):
        info = await get_info(uid)
        info = json.loads(info.text)["data"]
        try:
        # print(datetime.datetime.fromtimestamp(info['regtime']))
            print("ok", uid)
            print(info)
        except UnicodeEncodeError as e:
            print("UnicodeEncodeError:", e)
        except TypeError:
            print(info)
            
loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(asyncio.wait([user_info(x) for x in range(1, 1000, 10)]))
except Exception as e:
    print("Error:", e)

爬取1000条需要45秒左右,这样需要爬20天才能爬完。有没有什么办法提高爬取的速度?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(2)
阿神

用scrapy

巴扎黑

分布式爬虫或者开多个进程

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号