首页 > 其他 > 详细

(11).scrapy依赖的模块Twisted

时间:2018-07-03 18:57:09      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:acc   装饰   port   domain   erro   pad   三次   repl   得到   

#!/usr/bin/env python
# -*- coding:utf-8 -*- 
# author:love_cat
from twisted.internet import reactor  # 事件循环(终止条件,所有的socket都已经移除)
from twisted.internet import defer  # defer.Deferred 特殊的socket对象(不发请求,手动移除)
from twisted.web.client import getPage # 用于创建socket对象(下载完成,自动从事件循环中移除)


# 1. 利用getPage创建socket
# 2. 将socket添加到事件循环
# 3. 开始事件循环(内部发送请求,并接受响应;当所有的socket完成后,终止事件循环)
def response(content):
    print(content.decode(‘utf-8‘))

@defer.inlineCallbacks
def task():
    url = ‘http://www.bilibili.com‘
    d = getPage(url.encode(‘utf-8‘))    # 加上装饰器,得到特殊的socket对象。不发请求,只待在事件循环里面,用于让事件循环一直持续,直到我们手动移除
    d.addCallback(response)
    yield d

task()
reactor.run()
# 程序的输出结果如下
‘‘‘
<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="utf-8"><title>哔哩哔哩 (゜-゜)つロ 干杯~-bilibili</title><meta name="description" content="bilibili是国内知名的视频弹幕网站,这里有最及时的动漫新番,最棒的ACG氛围,最有创意的Up主。大家可以在这里找到许多欢乐。"><meta name="keywords" content="B站,弹幕,字幕,AMV,MAD,MTV,ANIME,动漫,动漫音乐,游戏,游戏解说,ACG,galgame,动画,番组,新番,初音,洛天依,vocaloid"><meta name="renderer" content="webkit"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="spm_prefix" content="333.334"><meta name="baidu-site-verification" content="gbRdPloQBZ"><meta name="360-site-verification" content="6057b3e352a63d37c14dfc82e2ccb288"><meta name="shenma-site-verification" content="a24cb5f89c4c9cb6d35c24236f4bf36d_1527561745"><meta name="google-site-verification" content="P_nC9t8BcWeyX8lXsTAUIAeHKrFuOYQ_uuCDC9mavwg"><link rel="dns-prefetch" href="//s1.hdslb.com"><link rel="dns-prefetch" href="//i0.hdslb.com"><link rel="dns-prefetch" href="//i1.hdslb.com"><link rel="dns-prefetch" href="//i2.hdslb.com"><link rel="dns-prefetch" href="//static.hdslb.com"><link rel="shortcut icon" href="//static.hdslb.com/images/favicon.ico"><link rel="search" type="application/opensearchdescription+xml" href="//static.hdslb.com/opensearch.xml" title="哔哩哔哩"><script type="text/javascript" src="//static.hdslb.com/js/jquery.min.js"></script><script type="text/javascript" src="//s1.hdslb.com/bfs/cm/st/bundle.js" crossorigin></script><script type="text/javascript">(function () {
        var ua = window.navigator.userAgent,
            agents = [‘Android‘, ‘iPhone‘, ‘SymbianOS‘, ‘Windows Phone‘, ‘iPod‘],
            isPC = true;
        for (var i = 0, len = agents.length; i < len; i++) {
          if (ua.indexOf(agents[i]) > 0) {
            isPC = false;
            break;
          }
        }
        if (!isPC) {
          var href = window.location.href.replace(‘www‘, ‘m‘);
          window.location.href = href;
        }
      })();</script><script type="text/javascript">window.spmReportData = {}
      window.reportConfig = { sample: 1, scrollTracker: true, msgObjects: ‘spmReportData‘, errorTracker: true }</script><link rel="prefetch" as="script" href="//static.hdslb.com/js/video.min.js"><link rel="stylesheet" href="//s1.hdslb.com/bfs/static/jinkela/home/css/home.1.ddbe2d8c7eac32c8eda17a6419def24ac66ca5c6.css"></head><body><div id="home-app"></div><div id="app" data-server-rendered="true"><div class="bili-header-m report-wrap-module"><div class="nav-menu"><div class="blur-bg"></div><div class="nav-mask"></div><div class="bili-wrapper clearfix"><div class="nav-con fl"><ul><li report-id="playpage_main" class="nav-item home"><a href="//www.bilibili.com" title="主站" class="t"><i class="bili-icon"></i>主站</a></li><li report-id="playpage_huahua" class="nav-item hbili"><a href="//h.bilibili.com" target="_blank" title="画友" class="t">画友</a></li><li report-id="playpage_game" class="nav-item game"><a href="//game.bilibili.com" target="_blank" title="游戏中心" class="t">游戏中心</a><!----></li><li report-id="playpage_live" class="nav-item live"><a href="//live.bilibili.com" target="_blank" title="直播" class="t">直播</a><!----></li><li report-id="playpage_buy" class="nav-item buy"><a href="//show.bilibili.com/platform/home.html?msource=pc_web" target="_blank" title="会员购" class="t">会员购</a></li><li report-id="playpage_bml" class="nav-item"><a href="//bml.bilibili.com" target="_blank" title="BML" class="t">BML</a></li><li report-id="playpage_bworld" class="nav-item"><a href="//bw.bilibili.com" target="_blank" title="BWORLD" class="t">BW</a></li><li report-id="playpage_download" class="nav-item mobile"><i class="b-icon b-icon-app"></i><a id="header-mobile-app" href="//app.bilibili.com" target="_blank" title="下载APP" class="t">下载APP</a><!----></li></ul></div><div report-id="playpage_contribution" class="up-load fr"><a href="//member.bilibili.com/v/video/submit.html" target="_blank" class="u-link">投 稿</a><!----></div><!----><!----></div></div><div id="banner_link" class="head-banner report-wrap-module report-scroll-module"><div class="head-content bili-wrapper"><p class="head-title"></p><div class="search"><form class="searchform"><input type="text" autocomplete="off" accesskey="s" x-webkit-speech x-webkit-grammar="builtin:translate" placeholder="" value="" class="search-keyword"><button type="submit" class="search-submit"></button></form><!----><!----><a href="//www.bilibili.com/ranking" target="_blank" class="link-ranking"><span>排行榜</span></a></div><a href="//www.bilibili.com" class="head-logo"></a></div><a href="" target="_blank" class="banner-link"></a></div><div class="bili-wrapper"><div id="primary_menu" class="primary-menu report-wrap-module report-scroll-module "><ul class="nav-menu"><li class="home"><a href="//www.bilibili.com"><!----><div class="nav-name">首页</div></a><ul class="sub-nav"></ul></li><li><a href="//www.bilibili.com/v/douga/"><div class="num-wrap"><span>--</span></div><div class="nav-name">动画</div></a><ul class="sub-nav"><li><a href="//www.bilibili.com/v/douga/mad/"><span>MAD·AMV</span></a></li><li><a href="//www.bilibili.com/v/douga/mmd/"><span>MMD·3D</span></a></li><li><a href="//www.bilibili.com/v/douga/voice/"><span>短片·手书·配音</span></a></li><li><a href="//www.bilibili.com/v/douga/other/"><span>综合</span></a></li></ul></li><li><a href="//www.bilibili.com/anime/"><div class="num-wrap"><span>--</span></div><div class="nav-name">番剧</div></a><ul class="sub-nav"><li><a href="//www.bilibili.com/v/anime/serial/"><span>连载动画</span></a></li><li><a href="//www.bilibili.com/v/anime/finish/"><span>完结动画</span></a></li><li><a href="//www.bilibili.com/v/anime/information/"><span>资讯</span></a></li><li><a href="//www.bilibili.com/v/anime/offical/"><span>官方延伸</span></a></li><li><a href="//www.bilibili.com/anime/timeline/"><span>新番时间表</span></a></li><li><a href="//www.bilibili.com/anime/index/"><span>番剧索引</span></a></li></ul></li><li><a href="//www.bilibili.com/guochuang/"><div class="num-wrap"><span>--</span></div><div class="nav-name">国创</div></a><ul class="sub-nav"><li><a href="//www.bilibili.com/v/guochuang/chinese/"><span>国产动画</span></a></li><li><a href="//www.bilibili.com/v/guochuang/original/"><span>国产原创相关</span></a></li><li><a href="//www.bilibili.com/v/guochuang/puppetry/"><span>布袋戏</span></a></li><li><a href="//www.bilibili.com/v/guochuang/information/"><span>资讯</span></a></li><li><a href="//www.bilibili.com/guochuang/timeline/"><span>新番时间表</span></a></li><li><a href="//www.bilibili.com/guochuang/index/"><span>国产动画索引</span></a></li></ul></li><li><a href="//www.bilibili.com/v/music/"><div class="num-wrap"><span>--</span></div><div class="nav-name">音乐</div></a><ul class="sub-nav"><li><a href="//www.bilibili.com/v/music/original/"><span>原创音乐</span></a></li><li><a href="//www.bilibili.com/v/music/cover/"><span>翻唱</span></a></li><li><a href="//www.bilibili.com/v/music/vocaloid/"><span>VOCALOID·UTAU</span></a></li><li><a href="//www.bilibili.com/v/music/perform/"><span>演奏</span></a></li><li><a href="//www.bilibili.com/v/music/coordinate/"><span>三次元音乐</span></a></li><li><a href="//www.bilibili.com/v/music/oped/"><span>OP/ED/OST</span></a></li><li><a href="//www.bilibili.com/v/music/collection/"><span>音乐选集</span></a></li></ul></li><li><a href="//www.bilibili.com/v/dance/"><div class="num-wrap"><span>--</span></div><div class="nav-name">舞蹈</div></a><ul class="sub-nav"><li><a href="//www.bilibili.com/v/dance/otaku/"><span>宅舞</span></a></li><li><a href="//www.bilibili.com/v/dance/three_d/"><span>三次元舞蹈</span></a></li><li><a href="//www.bilibili.com/v/dance/demo/"><span>舞蹈教程</span></a></li></ul></li><li><a href="//www.bilibili.com/v/game/"><div class="num-wrap"><span>--</span></div><div class="nav-name">游戏</div></a><ul class="sub-nav"><li><a href="//www.bilibili.com/v/game/stand_alone/"><span>单机游戏</span></a></li><li><a href="//www.bilibili.com/v/game/esports/"><span>电子竞技</span></a></li><li><a href="//www.bilibili.com/v/game/mobile/"><span>手机游戏</span></a></li><li><a href="//www.bilibili.com/v/game/online/"><span>网络游戏</span></a></li><li><a href="//www.bilibili.com/v/game/board/"><span>桌游棋牌</span></a></li><li><a href="//www.bilibili.com/v/game/gmv/"><span>GMV</span></a></li><li><a href="//www.bilibili.com/v/game/music/"><span>音游</span></a></li><li><a href="//www.bilibili.com/v/game/mugen/"><span>Mugen</span></a></li></ul></li><li><a href="//www.bilibili.com/v/technology/"><div class="num-wrap"><span>--</span></div><div class="nav-name">科技</div></a><ul class="sub-nav"><li><a href="//www.bilibili.com/v/technology/fun/"><span>趣味科普人文</span></a></li><li><a href="//www.bilibili.com/v/technology/wild/"><span>野生技术协会</span></a></li><li><a href="//www.bilibili.com/v/technology/speech_course/"><span>演讲·公开课</span></a></li><li><a href="//www.bilibili.com/v/technology/military/"><span>星海</span></a></li><li><a href="//www.bilibili.com/v/technology/digital/"><span>数码</span></a></li><li><a href="//www.bilibili.com/v/technology/mechanical/"><span>机械</span></a></li><li><a href="//www.bilibili.com/v/technology/automobile/"><span>汽车</span></a></li></ul></li><li><a href="//www.bilibili.com/v/life/"><div class="num-wrap"><span>--</span></div><div class="nav-name">生活</div></a><ul class="sub-nav"><li><a href="//www.bilibili.com/v/life/funny/"><span>搞笑</span></a></li><li><a href="//www.bilibili.com/v/life/daily/"><span>日常</span></a></li><li><a href="//www.bilibili.com/v/life/food/"><span>美食圈</span></a></li><li><a href="//www.bilibili.com/v/life/animal/"><span>动物圈</span></a></li><li><a href="//www.bilibili.com/v/life/handmake/"><span>手工</span></a></li><li><a href="//www.bilibili.com/v/life/painting/"><span>绘画</span></a></li><li><a href="//www.bilibili.com/v/life/asmr/"><span>ASMR</span></a></li><li><a href="//www.bilibili.com/v/life/sports/"><span>运动</span></a></li><li><a href="//www.bilibili.com/v/life/other/"><span>其他</span></a></li></ul></li><li><a href="//www.bilibili.com/v/kichiku/"><div class="num-wrap"><span>--</span></div><div class="nav-name">鬼畜</div></a><ul class="sub-nav"><li><a href="//www.bilibili.com/v/kichiku/guide/"><span>鬼畜调教</span></a></li><li><a href="//www.bilibili.com/v/kichiku/mad/"><span>音MAD</span></a></li><li><a href="//www.bilibili.com/v/kichiku/manual_vocaloid/"><span>人力VOCALOID</span></a></li><li><a href="//www.bilibili.com/v/kichiku/course/"><span>教程演示</span></a></li></ul></li><li><a href="//www.bilibili.com/v/fashion/"><div class="num-wrap"><span>--</span></div><div class="nav-name">时尚</div></a><ul class="sub-nav"><li><a href="//www.bilibili.com/v/fashion/makeup/"><span>美妆</span></a></li><li><a href="//www.bilibili.com/v/fashion/clothing/"><span>服饰</span></a></li><li><a href="//www.bilibili.com/v/fashion/aerobics/"><span>健身</span></a></li><li><a href="//www.bilibili.com/v/fashion/information/"><span>资讯</span></a></li></ul></li><li><a href="//www.bilibili.com/v/ad/ad/"><div class="num-wrap"><span>--</span></div><div class="nav-name">广告</div></a><ul class="sub-nav"><!----></ul></li><li><a href="//www.bilibili.com/v/ent/"><div class="num-wrap"><span>--</span></div><div class="nav-name">娱乐</div></a><ul class="sub-nav"><li><a href="//www.bilibili.com/v/ent/variety/"><span>综艺</span></a></li><li><a href="//www.bilibili.com/v/ent/star/"><span>明星</span></a></li><li><a href="//www.bilibili.com/v/ent/korea/"><span>Korea相关</span></a></li></ul></li><li><a href="//www.bilibili.com/v/cinephile/"><div class="num-wrap"><span>--</span></div><div class="nav-name">影视</div></a><ul class="sub-nav"><li><a href="//www.bilibili.com/v/cinephile/cinecism/"><span>影视杂谈</span></a></li><li><a href="//www.bilibili.com/v/cinephile/montage/"><span>影视剪辑</span></a></li><li><a href="//www.bilibili.com/v/cinephile/shortfilm/"><span>短片</span></a></li><li><a href="//www.bilibili.com/v/cinephile/trailer_info/"><span>预告·资讯</span></a></li><li><a href="//www.bilibili.com/v/cinephile/tokusatsu/"><span>特摄</span></a></li></ul></li><li><a href="//www.bilibili.com/cinema/"><div class="num-wrap"><span>--</span></div><div class="nav-name">放映厅</div></a><ul class="sub-nav"><li><a href="//www.bilibili.com/documentary/"><span>纪录片</span></a></li><li><a href="//www.bilibili.com/movie/"><span>电影</span></a></li><li><a href="//www.bilibili.com/tv/"><span>电视剧</span></a></li></ul></li><li class="side-nav zl"><a href="//www.bilibili.com/read/home" class="side-link"><i class="zhuanlan"></i><span>专栏</span></a></li><li class="side-nav nav-square"><a href="//www.bilibili.com/square" class="side-link"><i class="square"></i><span>广场</span></a><div class="sub-nav square-wrap"><ul><li><a href="//show.bilibili.com/platform/home.html"><i class="icon-prim icon-vip-buy"></i><span>会员购</span></a></li><li><a href="//activity.bilibili.com"><i class="icon-prim icon-activity"></i><span>活动中心</span></a></li><li><a href="//game.bilibili.com"><i class="icon-prim icon-game"></i><span>游戏中心</span></a></li><li><a href="//news.bilibili.com"><i class="icon-prim icon-news"></i><span>新闻中心</span></a></li><li><a href="http://h.bilibili.com"><i class="icon-prim icon-hy"></i><span>画友</span></a></li><li><a href="//www.bilibili.com/mango"><i class="icon-prim icon-mango"></i><span>芒果TV</span></a></li></ul><div class="square-field"></div></div></li><li class="side-nav"><a href="//live.bilibili.com" class="side-link"><i class="live"></i><span>直播</span></a><div class="sub-nav nav-live"><ul><li><a href="//live.bilibili.com/subject"><span>推荐主播</span></a></li><li><a href="//live.bilibili.com/pages/area/ent"><span>生活娱乐</span></a></li><li><a href="//live.bilibili.com/draw"><span>绘画专区</span></a></li><li><a href="//live.bilibili.com/pages/area/ent"><span>唱见舞见</span></a></li><li><a href="//live.bilibili.com/pages/area/ent"><span>御宅文化</span></a></li><li><a href="//live.bilibili.com/single"><span>单机联机</span></a></li><li><a href="//live.bilibili.com/online"><span>网络游戏</span></a></li><li><a href="//live.bilibili.com/e-sports"><span>电子竞技</span></a></li><li><a href="//live.bilibili.com/mobile-game"><span>手游直播</span></a></li></ul><div class="live-field fl"><a target="_blank" href="//h.bilibili.com" class="pic"><img src="//s1.hdslb.com/bfs/static/jinkela/home/images/wh2.png" alt="有文画"></a><a target="_blank" href="//vc.bilibili.com" class="pic"><img src="//s1.hdslb.com/bfs/static/jinkela/home/images/sp2.png" alt="小视频"></a></div></div></li><li class="side-nav"><a href="//www.bilibili.com/blackroom/" class="side-link"><i class="blackroom"></i><span>小黑屋</span></a></li></ul><div class="gif-menu nav-gif"><a href="" target="_blank" title="" class="random-p"><img src="" alt></a></div></div></div></div><div class="bili-wrapper"><div id="chief_recommend" class="chief-recommend-module report-wrap-module report-scroll-module clearfix"><div class="carousel-box"><div class="load-state"><span class="loading">正在加载...</span><!----><!----></div><div class="carousel-module"><div class="panel"><ul class="pic" style="width:0%;margin-left:--100%;height:;margin-top:;"></ul><ul class="title"></ul><!----></div></div></div><div class="recommend-module clearfix"><!----><div class="groom-module home-card"><a href="/video/av24383566/" target="_blank" title="【ACG大事件】肉番表化,热血再临,任天堂就是世界的主宰啊!"><img src="//i2.hdslb.com/bfs/archive/b43bd38af92c6ace7d8fb639250581df99d808c9.jpg@160w_100h.jpg" alt="【ACG大事件】肉番表化,热血再临,任天堂就是世界的主宰啊!" width="160" height="100" class="pic"><!----><div class="card-mark"><p class="title">【ACG大事件】肉番表化,热血再临,任天堂就是世界的主宰啊!</p><p class="author">up主:小M呜喵王</p><p class="play">播放:3.6万</p></div></a><div class="watch-later-trigger w-later"></div></div><div class="groom-module home-card"><a href="/video/av24380638/" target="_blank" title="【圈9x绝非言】生如逆旅【高考应援】【Lemon中文填翻】"><img src="//i0.hdslb.com/bfs/archive/4a1d0d3562102e7ba765dd5af0d6668c2fc85ac3.png@160w_100h.png" alt="【圈9x绝非言】生如逆旅【高考应援】【Lemon中文填翻】" width="160" height="100" class="pic"><!----><div class="card-mark"><p class="title">【圈9x绝非言】生如逆旅【高考应援】【Lemon中文填翻】</p><p class="author">up主:WineQ圈9</p><p class="play">播放:3.9万</p></div></a><div class="watch-later-trigger w-later"></div></div><div class="groom-module home-card"><a href="/video/av24318495/" target="_blank" title="【次郎】有球就给赞的清涼一夏!2018夏七月新番导视"><img src="//i2.hdslb.com/bfs/archive/4bcb73269767f7f3b573f65f977851c4e6355c44.jpg@160w_100h.jpg" alt="【次郎】有球就给赞的清涼一夏!2018夏七月新番导视" width="160" height="100" class="pic"><!----><div class="card-mark"><p class="title">【次郎】有球就给赞的清涼一夏!2018夏七月新番导视</p><p class="author">up主:次郎JIRO</p><p class="play">播放:7.6万</p></div></a><div class="watch-later-trigger w-later"></div></div><div class="groom-module home-card"><a href="/video/av24364690/" target="_blank" title="【高考大魔王】一口气全做对"><img src="//i0.hdslb.com/bfs/archive/b74884a76c6f89aae4f37aacf2541343d6890f1a.png@160w_100h.png" alt="【高考大魔王】一口气全做对" width="160" height="100" class="pic"><!----><div class="card-mark"><p class="title">【高考大魔王】一口气全做对</p><p class="author">up主:推背兔の</p><p class="play">播放:9.4万</p></div></a><div class="watch-later-trigger w-later"></div></div><div class="groom-module home-card"><a href="/video/av24320325/" target="_blank" title="【DNF】他的故事"><img src="//i2.hdslb.com/bfs/archive/0ea53531d36eaca6420a0e2ca8bffa294aa9302c.jpg@160w_100h.jpg" alt="【DNF】他的故事" width="160" height="100" class="pic"><!----><div class="card-mark"><p class="title">【DNF】他的故事</p><p class="author">up主:小巨人猪大星</p><p class="play">播放:19.0万</p></div></a><div class="watch-later-trigger w-later"></div></div><div class="groom-module home-card"><a href="/video/av24313640/" target="_blank" title="【起风了】一首为西卡和周淑怡写的情歌(个人翻唱填词)。催泪向"><img src="//i2.hdslb.com/bfs/archive/c0e3dc4307220cacec7ded0ba97f724f4c628af7.png@160w_100h.png" alt="【起风了】一首为西卡和周淑怡写的情歌(个人翻唱填词)。催泪向" width="160" height="100" class="pic"><!----><div class="card-mark"><p class="title">【起风了】一首为西卡和周淑怡写的情歌(个人翻唱填词)。催泪向</p><p class="author">up主:是乌龙院长啊</p><p class="play">播放:8.7万</p></div></a><div class="watch-later-trigger w-later"></div></div><div class="groom-module home-card"><a href="/video/av24310866/" target="_blank" title="【初音未来V4C原创】LIME LIGHT【蜂屋ななし@休闲主义】"><img src="//i1.hdslb.com/bfs/archive/e896ae1793e6c701f31ded251da1e22b56f8b4c6.jpg@160w_100h.jpg" alt="【初音未来V4C原创】LIME LIGHT【蜂屋ななし@休闲主义】" width="160" height="100" class="pic"><!----><div class="card-mark"><p class="title">【初音未来V4C原创】LIME LIGHT【蜂屋ななし@休闲主义】</p><p class="author">up主:蜂屋ななし</p><p class="play">播放:3154</p></div></a><div class="watch-later-trigger w-later"></div></div><div class="groom-module home-card"><a href="/video/av24240231/" target="_blank" title="【菌子】激昂壮志!从今以后,你就是我的人了?(鸾cos)"><img src="//i2.hdslb.com/bfs/archive/52fb8aad69581cfc61ae2d864223890ddbb86db6.jpg@160w_100h.jpg" alt="【菌子】激昂壮志!从今以后,你就是我的人了?(鸾cos)" width="160" height="100" class="pic"><!----><div class="card-mark"><p class="title">【菌子】激昂壮志!从今以后,你就是我的人了?(鸾cos)</p><p class="author">up主:浅野菌子</p><p class="play">播放:16.7万</p></div></a><div class="watch-later-trigger w-later"></div></div><span class="rec-btn prev">昨日</span><span class="rec-btn next">一周</span></div></div><div id="home_popularize" class="popularize-module report-wrap-module report-scroll-module clearfix"><div class="l-con"><div class="headline clearfix"><i class="icon icon_t icon-promote"></i><span class="name">推广</span></div><div class="storey-box clearfix"><div class="load-state"><span class="loading">正在加载...</span><!----><!----></div></div></div><div class="r-con"><div class="online"><a href="//www.bilibili.com/video/online.html" target="_blank" title="在线观看:0">在线人数:0</a><em></em><a href="//www.bilibili.com/newlist.html" target="_blank">最新投稿:0</a></div><!----></div></div><!----><div id="special_recommend" class="special-recommend-module report-wrap-module report-scroll-module clearfix"><div class="l-con"><div class="headline clearfix"><i class="icon icon_t icon-special"></i><a href="//www.bilibili.com/list/recommend/1.html" class="name">特别推荐</a></div><div class="storey-box clearfix"><div class="load-state"><span class="loading">正在加载...</span><!----><!----></div></div></div><!----></div></div><!----><!----><!----><!----><!----></div><script>window.__INITIAL_STATE__={"recommendData":[{"aid":"24383566","typename":"综合","title":"【ACG大事件】肉番表化,热血再临,任天堂就是世界的主宰啊!","subtitle":"","play":35936,"review":241,"video_review":725,"favorites":1594,"mid":346059,"author":"小M呜喵王","create":"2018-06-04 22:26","pic":"http:\u002F\u002Fi2.hdslb.com\u002Fbfs\u002Farchive\u002Fb43bd38af92c6ace7d8fb639250581df99d808c9.jpg","coins":3790,"duration":"15:31","badgepay":false},{"aid":"24380638","typename":"翻唱","title":"【圈9x绝非言】生如逆旅【高考应援】【Lemon中文填翻】","subtitle":"","play":38939,"review":1265,"video_review":1485,"favorites":3484,"mid":3797626,"author":"WineQ圈9","create":"2018-06-04 21:29","pic":"http:\u002F\u002Fi0.hdslb.com\u002Fbfs\u002Farchive\u002F4a1d0d3562102e7ba765dd5af0d6668c2fc85ac3.png","coins":6216,"duration":"6:42","badgepay":false},{"aid":"24318495","typename":"综合","title":"【次郎】有球就给赞的清涼一夏!2018夏七月新番导视","subtitle":"","play":76295,"review":399,"video_review":1770,"favorites":3507,"mid":10915924,"author":"次郎JIRO","create":"2018-06-03 13:39","pic":"http:\u002F\u002Fi2.hdslb.com\u002Fbfs\u002Farchive\u002F4bcb73269767f7f3b573f65f977851c4e6355c44.jpg","coins":7228,"duration":"15:12","badgepay":false},{"aid":"24364690","typename":"人力VOCALOID","title":"【高考大魔王】一口气全做对","subtitle":"","play":93847,"review":696,"video_review":634,"favorites":4357,"mid":96070394,"author":"推背兔の","create":"2018-06-04 17:31","pic":"http:\u002F\u002Fi0.hdslb.com\u002Fbfs\u002Farchive\u002Fb74884a76c6f89aae4f37aacf2541343d6890f1a.png","coins":6300,"duration":"2:51","badgepay":false},{"aid":"24320325","typename":"人力VOCALOID","title":"【DNF】他的故事","subtitle":"","play":190380,"review":3550,"video_review":2553,"favorites":10103,"mid":13357338,"author":"小巨人猪大星","create":"2018-06-03 14:27","pic":"http:\u002F\u002Fi2.hdslb.com\u002Fbfs\u002Farchive\u002F0ea53531d36eaca6420a0e2ca8bffa294aa9302c.jpg","coins":32611,"duration":"4:02","badgepay":false},{"aid":"24313640","typename":"翻唱","title":"【起风了】一首为西卡和周淑怡写的情歌(个人翻唱填词)。催泪向","subtitle":"","play":86961,"review":1772,"video_review":3880,"favorites":3027,"mid":1692960,"author":"是乌龙院长啊","create":"2018-06-03 11:23","pic":"http:\u002F\u002Fi2.hdslb.com\u002Fbfs\u002Farchive\u002Fc0e3dc4307220cacec7ded0ba97f724f4c628af7.png","coins":11014,"duration":"5:20","badgepay":false},{"aid":"24310866","typename":"V家","title":"【初音未来V4C原创】LIME LIGHT【蜂屋ななし@休闲主义】","subtitle":"","play":3154,"review":149,"video_review":117,"favorites":666,"mid":27444222,"author":"蜂屋ななし","create":"2018-06-03 10:20","pic":"http:\u002F\u002Fi1.hdslb.com\u002Fbfs\u002Farchive\u002Fe896ae1793e6c701f31ded251da1e22b56f8b4c6.jpg","coins":669,"duration":"3:33","badgepay":false},{"aid":"24240231","typename":"宅舞","title":"【菌子】激昂壮志!从今以后,你就是我的人了?(鸾cos)","subtitle":"","play":167367,"review":566,"video_review":337,"favorites":4288,"mid":8581342,"author":"浅野菌子","create":"2018-06-03 00:00","pic":"http:\u002F\u002Fi2.hdslb.com\u002Fbfs\u002Farchive\u002F52fb8aad69581cfc61ae2d864223890ddbb86db6.jpg","coins":2892,"duration":"4:19","badgepay":false}]};(function(){var s;(s=document.currentScript||document.scripts[document.scripts.length-1]).parentNode.removeChild(s);}());</script><script src="//s1.hdslb.com/bfs/static/jinkela/home/manifest.ddbe2d8c7eac32c8eda17a6419def24ac66ca5c6.js" crossorigin defer></script><script src="//s1.hdslb.com/bfs/static/jinkela/home/vendor.ddbe2d8c7eac32c8eda17a6419def24ac66ca5c6.js" crossorigin defer></script><script src="//s1.hdslb.com/bfs/static/jinkela/home/home.ddbe2d8c7eac32c8eda17a6419def24ac66ca5c6.js" crossorigin defer></script><div class="footer bili-footer report-wrap-module"></div><script type="text/javascript">if(!+"\v1"){
        document.body.innerHTML = "<div style=‘background: #00a1d6; text-align: center; padding: 10px 0; color: #fff;‘>为了保护你的账号安全,bilibili已不支持IE8及以下版本浏览器访问,建议你升级到IE最新版本浏览器,或使用Chrome等其他浏览器。<\/div>"
      }</script><script type="text/javascript" charset="utf-8" src="//static.hdslb.com/common/js/footer.js"></script><!-- built files will be auto injected --><script type="text/javascript">//防嵌入
      (function(){try{if(parent!=self && (parent.document.domain!=document.domain	|| (document.referrer && !/^http(s)?:\/\/[.\w-]+\.bilibili\.com\//i.test(document.referrer)))){throw new Error("can‘t be iframed");}}catch(e){window.open(location.href, "_top");}})();</script></body></html>

‘‘‘


@defer.inlineCallbacks
def task1():
    url = ‘http://www.bilibili.com‘
    d = getPage(url.encode(‘utf-8‘))
    d.addCallback(response)
    yield d

@defer.inlineCallbacks
def task2():
    url=‘http://www.baidu.com‘
    d = getPage(url.encode(‘utf-8‘))
    d.addCallback(response)

    url = ‘http://www.bilibili.com‘
    d = getPage(url.encode(‘utf-8‘))
    d.addCallback(response)
    yield d

def response(content):
    print(content.decode(‘utf-8‘)[0:1000])  # 这里只取前1000个字符

def run_until_complete(*args,**kwargs):
    reactor.stop()


t1 = task1()
t2 = task2()
all_task = defer.DeferredList([t1,t2])
all_task.addBoth(run_until_complete)
reactor.run()
# 程序的输出结果如下
‘‘‘
<!DOCTYPE html>
<!--STATUS OK-->


<html>
<head>
    
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
	<meta content="always" name="referrer">
    <meta name="theme-color" content="#2932e1">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" title="百度搜索" />
    <link rel="icon" sizes="any" mask href="//www.baidu.com/img/baidu_85beaf5496f291521eb75ba38eacbd87.svg">
	
	
	<link rel="dns-prefetch" href="//s1.bdstatic.com"/>
	<lin
<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="utf-8"><title>哔哩哔哩 (゜-゜)つロ 干杯~-bilibili</title><meta name="description" content="bilibili是国内知名的视频弹幕网站,这里有最及时的动漫新番,最棒的ACG氛围,最有创意的Up主。大家可以在这里找到许多欢乐。"><meta name="keywords" content="B站,弹幕,字幕,AMV,MAD,MTV,ANIME,动漫,动漫音乐,游戏,游戏解说,ACG,galgame,动画,番组,新番,初音,洛天依,vocaloid"><meta name="renderer" content="webkit"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="spm_prefix" content="333.334"><meta name="baidu-site-verification" content="gbRdPloQBZ"><meta name="360-site-verification" content="6057b3e352a63d37c14dfc82e2ccb288"><meta name="shenma-site-verification" content="a24cb5f89c4c9cb6d35c24236f4bf36d_1527561745"><meta name="google-site-verification" content="P_nC9t8BcWeyX8lXsTAUIAeHKrFuOYQ_uuCDC9mavwg"><link rel="dns-prefetch" href="//s1.hdslb.com"><link rel="dns-prefetch" href="//i0.hdslb.com"><link rel="dns-prefetch" href="//i1.hdslb.com"><link rel="dns-prefetch" href="//i2.hdslb.com"><link rel="dns-prefetch" href="//static.h
<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="utf-8"><title>哔哩哔哩 (゜-゜)つロ 干杯~-bilibili</title><meta name="description" content="bilibili是国内知名的视频弹幕网站,这里有最及时的动漫新番,最棒的ACG氛围,最有创意的Up主。大家可以在这里找到许多欢乐。"><meta name="keywords" content="B站,弹幕,字幕,AMV,MAD,MTV,ANIME,动漫,动漫音乐,游戏,游戏解说,ACG,galgame,动画,番组,新番,初音,洛天依,vocaloid"><meta name="renderer" content="webkit"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="spm_prefix" content="333.334"><meta name="baidu-site-verification" content="gbRdPloQBZ"><meta name="360-site-verification" content="6057b3e352a63d37c14dfc82e2ccb288"><meta name="shenma-site-verification" content="a24cb5f89c4c9cb6d35c24236f4bf36d_1527561745"><meta name="google-site-verification" content="P_nC9t8BcWeyX8lXsTAUIAeHKrFuOYQ_uuCDC9mavwg"><link rel="dns-prefetch" href="//s1.hdslb.com"><link rel="dns-prefetch" href="//i0.hdslb.com"><link rel="dns-prefetch" href="//i1.hdslb.com"><link rel="dns-prefetch" href="//i2.hdslb.com"><link rel="dns-prefetch" href="//static.h

‘‘‘

  

#!/usr/bin/env python
# -*- coding:utf-8 -*- 
# author:love_cat

from twisted.internet import reactor,task
def hello(name):
    print(‘hello‘, name)
task1 = task.LoopingCall(hello, ‘satori‘)   # 每隔十秒钟运行一次
task1.start(10)
reactor.callWhenRunning(hello, ‘mmp‘)  # 当循环开始时,运行
reactor.callLater(3, hello, ‘mmppp‘)  # 循环开始后三秒,运行
reactor.run()
‘‘‘
hello satori
hello mmp
hello mmppp
hello satori
hello satori
hello satori
hello satori
hello satori
.....
.....
.....
‘‘‘

  

(11).scrapy依赖的模块Twisted

标签:acc   装饰   port   domain   erro   pad   三次   repl   得到   

原文:https://www.cnblogs.com/traditional/p/9259948.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号