zoukankan      html  css  js  c++  java
  • python 并发爬虫的快感

    import time
    from tomorrow import threads
    from requests_html import HTMLSession
    session=HTMLSession()
    @threads(50)  # 使用装饰器,这个函数异步执行
    def download(url):
        return session.get(url)
    
    
    def main():
        start = time.time()
        urls = [
            'https://pypi.org/project/tomorrow/0.2.0/',
            'https://www.cnblogs.com/pyld/p/4716744.html',
            'http://www.xicidaili.com/nn/10',
            'http://baidu.com',
            'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879','https://pypi.org/project/tomorrow/0.2.0/',
            'https://www.cnblogs.com/pyld/p/4716744.html',
            'http://www.xicidaili.com/nn/10',
            'http://baidu.com',
            'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879','https://pypi.org/project/tomorrow/0.2.0/',
            'https://www.cnblogs.com/pyld/p/4716744.html',
            'http://www.xicidaili.com/nn/10',
            'http://baidu.com',
            'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879','https://pypi.org/project/tomorrow/0.2.0/',
            'https://www.cnblogs.com/pyld/p/4716744.html',
            'http://www.xicidaili.com/nn/10',
            'http://baidu.com',
            'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879','https://pypi.org/project/tomorrow/0.2.0/',
            'https://www.cnblogs.com/pyld/p/4716744.html',
            'http://www.xicidaili.com/nn/10',
            'http://baidu.com',
            'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879','https://pypi.org/project/tomorrow/0.2.0/',
            'https://www.cnblogs.com/pyld/p/4716744.html',
            'http://www.xicidaili.com/nn/10',
            'http://baidu.com',
            'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879','https://pypi.org/project/tomorrow/0.2.0/',
            'https://www.cnblogs.com/pyld/p/4716744.html',
            'http://www.xicidaili.com/nn/10',
            'http://baidu.com',
            'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879','https://pypi.org/project/tomorrow/0.2.0/',
            'https://www.cnblogs.com/pyld/p/4716744.html',
            'http://www.xicidaili.com/nn/10',
            'http://baidu.com',
            'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879','https://pypi.org/project/tomorrow/0.2.0/',
            'https://www.cnblogs.com/pyld/p/4716744.html',
            'http://www.xicidaili.com/nn/10',
            'http://baidu.com',
            'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879','https://pypi.org/project/tomorrow/0.2.0/',
            'https://www.cnblogs.com/pyld/p/4716744.html',
            'http://www.xicidaili.com/nn/10',
            'http://baidu.com',
            'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879','https://pypi.org/project/tomorrow/0.2.0/',
            'https://www.cnblogs.com/pyld/p/4716744.html',
            'http://www.xicidaili.com/nn/10',
            'http://baidu.com',
            'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879','https://pypi.org/project/tomorrow/0.2.0/',
            'https://www.cnblogs.com/pyld/p/4716744.html',
            'http://www.xicidaili.com/nn/10',
            'http://baidu.com',
            'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879','https://pypi.org/project/tomorrow/0.2.0/',
            'https://www.cnblogs.com/pyld/p/4716744.html',
            'http://www.xicidaili.com/nn/10',
            'http://baidu.com',
            'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879','https://pypi.org/project/tomorrow/0.2.0/',
            'https://www.cnblogs.com/pyld/p/4716744.html',
            'http://www.xicidaili.com/nn/10',
            'http://baidu.com',
            'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879','https://pypi.org/project/tomorrow/0.2.0/',
            'https://www.cnblogs.com/pyld/p/4716744.html',
            'http://www.xicidaili.com/nn/10',
            'http://baidu.com',
            'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879','https://pypi.org/project/tomorrow/0.2.0/',
            'https://www.cnblogs.com/pyld/p/4716744.html',
            'http://www.xicidaili.com/nn/10',
            'http://baidu.com',
            'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879','https://pypi.org/project/tomorrow/0.2.0/',
            'https://www.cnblogs.com/pyld/p/4716744.html',
            'http://www.xicidaili.com/nn/10',
            'http://baidu.com',
            'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879'
        ]
        req_list=[]
        for i in urls:
            req_list.append(download(i))
        print(req_list)
        responses = [i.html.xpath("//title/text()") for i in req_list]
        print(responses)
        end = time.time()
        print("Time: %f seconds" % (end - start))
    
    
    if __name__ == "__main__":
        main()
    
    
  • 相关阅读:
    NVelocity语法
    Linux C socket 编程之UDP
    MFC之Radio Button按钮学习 转载
    动态加载DLL
    JS中 判断一个整数是偶数还是奇数,并输出判断结果。
    JS中判断一个整数,属于哪个范围:大于0;小于0;等于0
    JS输入分数,判定成绩等级(if和switch两种写法)
    JS中根据输入的三个数字,判断是这一年的第几天。
    JS中开发一款软件,根据公式(身高108)*2=体重,可以有10斤左右的浮动。来观察测试者体重是否合适。
    php 多语言(UTF8编码)导出Excel、CSV乱码解决办法之导出UTF8编码的Excel、CSV
  • 原文地址:https://www.cnblogs.com/c-x-a/p/9485734.html
Copyright © 2011-2022 走看看