置顶 老师参与

Requests库的爬取性能分析

嵩天 发表于2020年02月02日
<p>尽管Requests库功能很友好、开发简单(其实除了import外只需一行主要代码),但其性能与专业爬虫相比还是有一定差距的。请编写一个小程序,“任意”找个url,测试一下成功爬取100次网页的时间。(某些网站对于连续爬取页面将采取屏蔽IP的策略,所以,要避开这类网站。)</p><p>请回复代码,并给出url及在自己机器上的运行时间。</p><p><br/></p><p><br/></p><p><br/></p>
459 回复

    1楼

  • 龍mooc205 发表于2020年03月17日
    0 | 0 | 举报
    <p>import&nbsp;requests<br >import&nbsp;time<br >def&nbsp;getHEMLText(url):<br >&nbsp;&nbsp;&nbsp;try:<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;requests.get(url,&nbsp;timeout&nbsp;=&nbsp;30)<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.raise_for_status()&nbsp;#如果状态不是200,引发HTTPError异常<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.encoding&nbsp;=&nbsp;r.apparent_encoding<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;r.text<br >&nbsp;&nbsp;&nbsp;except:<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;&quot;产生异常&quot;<br >if&nbsp;__name__&nbsp;==&nbsp;&quot;__main__&quot;:<br >&nbsp;&nbsp;&nbsp;url&nbsp;=&nbsp;&quot;https://www.baidu.com&quot;<br >&nbsp;&nbsp;&nbsp;begin&nbsp;=&nbsp;time.time()<br >&nbsp;&nbsp;&nbsp;for&nbsp;i&nbsp;in&nbsp;range(0,&nbsp;100):<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getHEMLText(url)<br >&nbsp;&nbsp;&nbsp;print(&quot;{:.2f}&quot;.format(time.time()-begin))<br ><br ><br >D:\Python题库\Scripts\python.exe&nbsp;D:/Python题库/网络爬取通用代码框架.py<br >9.68<br ><br >Process&nbsp;finished&nbsp;with&nbsp;exit&nbsp;code&nbsp;0<br ></p>
    龍mooc205 发表于2020年03月17日
    添加评论
  • 2楼

  • mooc1528519975754 发表于2020年03月17日
    1 | 1 | 举报
    <code class="brush:python;toolbar:false" >import&nbsp;requests import&nbsp;time t1&nbsp;=&nbsp;time.time() for&nbsp;i&nbsp;in&nbsp;range(100): &nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;requests.get(&quot;https://www.baidu.com&quot;) t2&nbsp;=&nbsp;time.time() t&nbsp;=&nbsp;t2&nbsp;-&nbsp;t1 print(t)</code><p><img src="https://nos.netease.com/edu-image/0d658e51e5674e81956b4cc7a6d268e3.png" /></p><br />
    mooc1528519975754 发表于2020年03月17日
    • mooc1528519975754 2020年03月17日
      1 | 举报
      <p><code class="brush:python;toolbar:false" >import&nbsp;requests import&nbsp;time t1&nbsp;=&nbsp;time.time() for&nbsp;i&nbsp;in&nbsp;range(5): &nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;requests.get(&quot;https://lq.fyxfw.gov.cn/display.php?id=35139&quot;)#爬取某可计数网页 &nbsp;&nbsp;&nbsp;&nbsp;t2&nbsp;=&nbsp;time.time() &nbsp;&nbsp;&nbsp;&nbsp;t&nbsp;=&nbsp;t2&nbsp;-&nbsp;t1 &nbsp;&nbsp;&nbsp;&nbsp;print(t) &nbsp;&nbsp;&nbsp;&nbsp;#可以通过爬取可计数网页来验证是否真正的爬取成功,我的例子是“临泉县先锋网”的,每次爬取耗时3.4秒。</code></p>
      mooc1528519975754 发表于2020年03月17日
      1 | 举报
    添加评论
  • 3楼

  • nono_226 发表于2020年03月17日
    1 | 0 | 举报
    <p>import requests<br >import time<br ><br ># 京东商品页<br >prefix_url = &quot;https://item.jd.com/{}.html&quot;<br >urls = []<br >num = 100000768781</p><p># 随机的100个任意页面<br >for i in range(100):<br > &nbsp; &nbsp;urls.append(prefix_url.format(num))<br > &nbsp; &nbsp;num +=1</p><p><br ></p><p># 启动性能计时<br >start_time = time.time()<br >for url in urls:<br > &nbsp; &nbsp;r = requests.get(url)<br >end_time = time.time()<br >delta_time = end_time - start_time</p><p># 打印耗时<br >print(delta_time)</p><p><br ></p><p>D:\Python\python.exe E:/Code/python/web_crawler/test.py</p><p>36.75410223007202</p><p><br ></p><p>Process finished with exit code 0</p>
    nono_226 发表于2020年03月17日
    添加评论
  • 4楼

  • FishXxxxx 发表于2020年03月17日
    1 | 0 | 举报
    <p><img src="https://nos.netease.com/edu-image/34a07db1d4444c378c217e6c36380410.png" /></p><p>爬取的是中国大学MOOC(慕课)国家精品页面,用时17.84秒。</p>
    FishXxxxx 发表于2020年03月17日
    添加评论
  • 5楼

  • CharltonZL 发表于2020年03月17日
    0 | 0 | 举报
    <p>import requests</p><p>import time</p><p>t1 = time.time()</p><p>for i in range(100):</p><p> &nbsp;&nbsp;&nbsp;&nbsp;r = request.get(&quot;https://www.baidu.com&quot;)</p><p>t2 = time.time()</p><p>t = t2 - t1</p><p>print(t)</p><p><br ></p>
    CharltonZL 发表于2020年03月17日
    添加评论
  • 6楼

  • 小洪didi 发表于2020年03月17日
    0 | 0 | 举报
    <p>import&nbsp;requests</p><p>import&nbsp;time</p><p><br ></p><p>def&nbsp;getHtmlText(url):</p><p>&nbsp;&nbsp;&nbsp;&nbsp;try:</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;requests.get(url,&nbsp;timeout&nbsp;=&nbsp;30)</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.raise_for_status</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.encoding&nbsp;=&nbsp;r.apparent_encoding</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;r.text</p><p>&nbsp;&nbsp;&nbsp;&nbsp;except:</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;''</p><p>if&nbsp;__name__&nbsp;==&nbsp;&quot;__main__&quot;:</p><p>&nbsp;&nbsp;&nbsp;&nbsp;url&nbsp;=&nbsp;'https://baidu.com'</p><p>&nbsp;&nbsp;&nbsp;&nbsp;start&nbsp;=&nbsp;time.perf_counter()</p><p>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i&nbsp;in&nbsp;range(100):</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getHtmlText(url)</p><p>&nbsp;&nbsp;&nbsp;&nbsp;end&nbsp;=&nbsp;time.perf_counter()</p><p>&nbsp;&nbsp;&nbsp;&nbsp;dur&nbsp;=&nbsp;end&nbsp;-&nbsp;start</p><p>&nbsp;&nbsp;&nbsp;&nbsp;print(f'{dur&nbsp;=&nbsp;:.2f}')</p><p>url = '<a href="https://baidu.com&#39;" >https://baidu.com'</a> </p><p>运行时间dur = 9.78</p>
    小洪didi 发表于2020年03月17日
    添加评论
  • 7楼

  • 201曹杨 发表于2020年03月17日
    0 | 0 | 举报
    <p><img src="https://nos.netease.com/edu-image/9789858e594c43298a1dc0d2860773e8.png" /></p>
    201曹杨 发表于2020年03月17日
    添加评论
  • 8楼

  • ryfecho163com 发表于2020年03月17日
    0 | 0 | 举报
    <p>import requests</p><p>import time</p><p><br ></p><p>def getHTMLText(url):</p><p>&nbsp; &nbsp; try:</p><p>&nbsp; &nbsp; &nbsp; &nbsp; r=requests.get(url,timeout=30)</p><p>&nbsp; &nbsp; &nbsp; &nbsp; r.raise_for_status()</p><p>&nbsp; &nbsp; &nbsp; &nbsp; r.encoding=r.apparent_ecoding</p><p>&nbsp; &nbsp; &nbsp; &nbsp; return r.text</p><p>&nbsp; &nbsp; except:</p><p>&nbsp; &nbsp; &nbsp; &nbsp; return &quot;产生异常&quot;</p><p><br ></p><p>if __name__==&quot;__main__&quot;:</p><p>&nbsp; &nbsp; start=time.perf_counter()</p><p>&nbsp; &nbsp; url=&quot;https://www.qq.com&quot;</p><p>&nbsp; &nbsp; for i in range(100):</p><p>&nbsp; &nbsp; &nbsp; &nbsp; r=requests.get(url)</p><p>&nbsp; &nbsp; end=time.perf_counter()</p><p>&nbsp; &nbsp; print(&quot;{:.2f}&quot;.format(end-start))</p><p><br ></p><p>39.11</p>
    ryfecho163com 发表于2020年03月17日
    添加评论
  • 9楼

  • 媛媛ykt54340324227634056 发表于2020年03月17日
    0 | 0 | 举报
    <img src='https://edu-image.nosdn.127.net/638935717B899E0BCEB346699819B229.jpg' />
    媛媛ykt54340324227634056 发表于2020年03月17日
    添加评论
  • 10楼

  • 黎瑞Clare 发表于2020年03月17日
    0 | 0 | 举报
    <p>import requests<br >import time<br ><br ><br >def getHtmlText(url):<br > &nbsp; &nbsp;try:<br > &nbsp; &nbsp; &nbsp; &nbsp;r = requests.get(url, timeout=30)<br > &nbsp; &nbsp; &nbsp; &nbsp;r.raise_for_status<br > &nbsp; &nbsp; &nbsp; &nbsp;r.encoding = r.apparent_encoding<br > &nbsp; &nbsp; &nbsp; &nbsp;return r.text<br > &nbsp; &nbsp;except:<br > &nbsp; &nbsp; &nbsp; &nbsp;return '爬取失败!'<br ><br ><br >if __name__ == &quot;__main__&quot;:<br > &nbsp; &nbsp;url = 'https://m.bilibili.com/'<br > &nbsp; &nbsp;start = time.perf_counter()<br ><br > &nbsp; &nbsp;for i in range(100):<br > &nbsp; &nbsp; &nbsp; &nbsp;getHtmlText(url)<br ><br > &nbsp; &nbsp;end = time.perf_counter()<br > &nbsp; &nbsp;dur = end - start<br > &nbsp; &nbsp;print(f'{dur = :.2f}')</p><p><br ></p><p>url = '<a href="https://m.bilibili.com/&#39;" >https://m.bilibili.com/'</a> </p><p><br ></p><p>运行时间:199.30</p>
    黎瑞Clare 发表于2020年03月17日
    添加评论
  • 11楼

  • 物流151-崔莹04 发表于2020年03月17日
    1 | 0 | 举报
    <p><img src="https://nos.netease.com/edu-image/98d924c035ad4f1ea0e012416f8bad56.png" /><img src="https://nos.netease.com/edu-image/58e0d02dbd5b44cebfc41f681c34edcd.jpg" /></p>
    物流151-崔莹04 发表于2020年03月17日
    添加评论
  • 12楼

  • 细雨青铜 发表于2020年03月17日
    0 | 0 | 举报
    <p>#coding=gbk<br >from time import perf_counter<br >import requests<br >def getHTMLText(url):<br >&nbsp;&nbsp;&nbsp; try:<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r=requests.get(url,timeout=30)<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.raise_for_status() #如果状态不是200,引发HTTPError异常<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.encoding=r.apparent_encoding<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return r.text<br >&nbsp;&nbsp;&nbsp; except:<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return &quot;产生异常&quot;<br >start=perf_counter()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br >for i in range(100):<br >&nbsp;&nbsp;&nbsp; if __name__==&quot;__main__&quot;:<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url=&quot;https://www.danda.com.cn/index/index/page_title/cate_id/3/sub_style/0.html?device=pc&amp;renqun_youhua=165944&quot;<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(getHTMLText(url))<br >&nbsp;&nbsp;&nbsp; <br >print(&quot;爬取网页100次的时间为{}s&quot;.format(perf_counter()-start))</p>
    细雨青铜 发表于2020年03月17日
    添加评论
  • 13楼

  • 大方的头 发表于2020年03月17日
    0 | 0 | 举报
    <p><code class="brush:python;toolbar:false" >import&nbsp;requests import&nbsp;time t&nbsp;=&nbsp;time.perf_counter() for&nbsp;i&nbsp;in&nbsp;range(100): &nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;requests.get(&quot;https://www.baidu.com&quot;,&nbsp;timeout=10) duri&nbsp;=&nbsp;time.perf_counter()-t print(duri)</code>结果:16.93397787</p>
    大方的头 发表于2020年03月17日
    添加评论
  • 14楼

  • 太原理工大学软件1825班姬靖宇 发表于2020年03月17日
    0 | 0 | 举报
    import requests<br >import time<br >def getHTMLText(url):<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r=requests.get(url,timeout=30)<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.raise_for_status()<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.encode=r.apparent_encode<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return r.text<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except:<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return(&quot;返回错误&quot;)<br >if __name__==&quot;__main__&quot;:<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; start=time.perf_counter()<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url=&quot;https://www.baidu.com&quot;<br ><br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for i in range(100):<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getHTMLText(url)<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dur=time.perf_counter()-start<br >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(&quot;{:.2f}&quot;.format(dur))<br ><br >4.57
    添加评论
  • 15楼

  • Yuzx 发表于2020年03月17日
    4 | 4 | 举报
    <p><code class="brush:python;toolbar:false" >import&nbsp;requests&nbsp;as&nbsp;rq import&nbsp;time&nbsp;as&nbsp;t def&nbsp;gethtml(url): &nbsp;&nbsp;&nbsp;&nbsp;try: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;rq.get(url) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.raise_for_status() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.encoding&nbsp;=&nbsp;r.apparent_encoding &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;except: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;'爬取失败' if&nbsp;__name__&nbsp;==&nbsp;'__main__': &nbsp;&nbsp;&nbsp;&nbsp;start&nbsp;=&nbsp;t.perf_counter() &nbsp;&nbsp;&nbsp;&nbsp;url&nbsp;=&nbsp;'https://baidu.com' &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i&nbsp;in&nbsp;range(100): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gethtml(url) &nbsp;&nbsp;&nbsp;&nbsp;end&nbsp;=&nbsp;t.perf_counter() &nbsp;&nbsp;&nbsp;&nbsp;print('一百次爬取时间为{:.2f}秒'.format(end-start))</code>一百次爬取时间为10.38秒</p>
    Yuzx 发表于2020年03月17日
    添加评论
  • 16楼

  • 星河百穿 发表于2020年03月17日
    0 | 0 | 举报
    <p><code class="brush:python;toolbar:false" >import&nbsp;requests&nbsp;as&nbsp;rq import&nbsp;time&nbsp;as&nbsp;t def&nbsp;gethtml(url): &nbsp;&nbsp;&nbsp;&nbsp;try: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;rq.get(url) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.raise_for_status() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.encoding&nbsp;=&nbsp;r.apparent_encoding &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;except: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;'爬取失败' if&nbsp;__name__&nbsp;==&nbsp;'__main__': &nbsp;&nbsp;&nbsp;&nbsp;start&nbsp;=&nbsp;t.perf_counter() &nbsp;&nbsp;&nbsp;&nbsp;url&nbsp;=&nbsp;'https://baidu.com' &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i&nbsp;in&nbsp;range(100): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gethtml(url) &nbsp;&nbsp;&nbsp;&nbsp;end&nbsp;=&nbsp;t.perf_counter() &nbsp;&nbsp;&nbsp;&nbsp;print('一百次爬取时间为{:.2f}秒'.format(end-start))</code>一百次爬取时间为103.37秒</p>
    星河百穿 发表于2020年03月17日
    添加评论
  • 17楼

  • 程澜午 发表于2020年03月17日
    1 | 0 | 举报
    <p><code class="brush:python;toolbar:false" >import&nbsp;requests&nbsp;as&nbsp;rq import&nbsp;time&nbsp;as&nbsp;t def&nbsp;gethtml(url): &nbsp;&nbsp;&nbsp;&nbsp;try: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;rq.get(url) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.raise_for_status() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.encoding&nbsp;=&nbsp;r.apparent_encoding &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;except: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;'爬取失败' if&nbsp;__name__&nbsp;==&nbsp;'__main__': &nbsp;&nbsp;&nbsp;&nbsp;start&nbsp;=&nbsp;t.perf_counter() &nbsp;&nbsp;&nbsp;&nbsp;url&nbsp;=&nbsp;'https://baidu.com' &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i&nbsp;in&nbsp;range(100): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gethtml(url) &nbsp;&nbsp;&nbsp;&nbsp;end&nbsp;=&nbsp;t.perf_counter() &nbsp;&nbsp;&nbsp;&nbsp;print('一百次爬取时间为{:.2f}秒'.format(end-start))</code>一百次爬取时间为27.85秒</p>
    程澜午 发表于2020年03月17日
    添加评论
  • 18楼

  • 星河百穿 发表于2020年03月17日
    0 | 0 | 举报
    <p><code class="brush:python;toolbar:false" >import&nbsp;requests&nbsp;as&nbsp;rq import&nbsp;time&nbsp;as&nbsp;t def&nbsp;gethtml(url): &nbsp;&nbsp;&nbsp;&nbsp;try: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;rq.get(url) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.raise_for_status() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.encoding&nbsp;=&nbsp;r.apparent_encoding &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;r &nbsp;&nbsp;&nbsp;&nbsp;except: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;'爬取失败' if&nbsp;__name__&nbsp;==&nbsp;'__main__': &nbsp;&nbsp;&nbsp;&nbsp;start&nbsp;=&nbsp;t.perf_counter() &nbsp;&nbsp;&nbsp;&nbsp;url&nbsp;=&nbsp;'https://www.icourse163.org' &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i&nbsp;in&nbsp;range(100): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gethtml(url) &nbsp;&nbsp;&nbsp;&nbsp;end&nbsp;=&nbsp;t.perf_counter() &nbsp;&nbsp;&nbsp;&nbsp;print('一百次爬取时间为{:.2f}秒'.format(end-start))</code>一百次爬取时间为94.74秒</p>
    星河百穿 发表于2020年03月17日
    添加评论
  • 19楼

  • MF1915043徐慈寒 发表于2020年03月17日
    0 | 0 | 举报
    <p><img src="https://nos.netease.com/edu-image/50b9bb8fedcc452189132bf41d619013.png" /></p><p>85.53秒</p>
    MF1915043徐慈寒 发表于2020年03月17日
    添加评论
  • 20楼

  • 这个昵称太抢手了_换一个 发表于2020年03月17日
    0 | 0 | 举报
    <p>import requests<br >import time as t<br >def getHTMLText(url):<br > &nbsp; &nbsp;try :<br > &nbsp; &nbsp; &nbsp; &nbsp;r = requests.get(url, timeout = 30)<br > &nbsp; &nbsp; &nbsp; &nbsp;r.raise_for_status()#如果状态不是200,引发HTTPError异常<br > &nbsp; &nbsp; &nbsp; &nbsp;r.encoding = r.apparent_encoding &nbsp;#按照内容猜测的编码格式赋值给解析内容用的编码格式<br > &nbsp; &nbsp; &nbsp; &nbsp;return r.text<br > &nbsp; &nbsp;except:<br > &nbsp; &nbsp; &nbsp; &nbsp;return &quot;产生异常&quot;<br >if __name__ == &quot;__main__&quot;:<br > &nbsp; &nbsp;url = &quot;https://www.duba.com&quot;<br > &nbsp; &nbsp;start = t.perf_counter()<br > &nbsp; &nbsp;for i in range(100):<br > &nbsp; &nbsp; &nbsp; &nbsp;getHTMLText(url)<br > &nbsp; &nbsp;end = t.perf_counter()<br > &nbsp; &nbsp;print(f&quot;爬取该网页一百次所用时间: {end-start}&quot;)</p><p></p><p><br ></p><p>12.387908399999999</p>
    这个昵称太抢手了_换一个 发表于2020年03月17日
    添加评论
点击加载更多