置顶 老师参与

Requests库的爬取性能分析

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

    1楼

  • 王皓阳041319250 发表于2023年03月14日
    0 | 0 | 举报
    <p><code class="brush:python;toolbar:false" >import&nbsp;requests import&nbsp;time start&nbsp;=&nbsp;time.perf_counter() for&nbsp;i&nbsp;in&nbsp;range(100): &nbsp;&nbsp;&nbsp;&nbsp;try: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;requests.get(&quot;https://python123.io/&quot;,&nbsp;timeout=30) &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;except: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;产生异常&quot;) end&nbsp;=&nbsp;time.perf_counter() print(end&nbsp;-&nbsp;start)</code>爬取https://python123.io100次耗时63.7722963s。</p>
    王皓阳041319250 发表于2023年03月14日
    添加评论
  • 2楼

  • Zouyea 发表于2023年03月19日
    0 | 0 | 举报
    <p>import time<br >import requests<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 >start = time.perf_counter()<br >if __name__ == '__main__':<br > &nbsp; &nbsp;for i in range(100):<br > &nbsp; &nbsp; &nbsp; &nbsp;url = 'https://www.fishc.com.cn'<br > &nbsp; &nbsp; &nbsp; &nbsp;getHTMLtext(url)<br > &nbsp; &nbsp;dur = time.perf_counter() - start<br > &nbsp; &nbsp;print(f'执行程序一共耗费了{dur}s')</p><p><br ></p><p>爬取鱼C工作室(https://www.fishc.com.cn)100次耗时45.1654737s</p>
    Zouyea 发表于2023年03月19日
    添加评论
  • 3楼

  • mooc109659764727099005 发表于2023年03月19日
    0 | 0 | 举报
    <p>import time<br >import requests<br >def getHTML(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 >start = time.time()<br >for i in range(100):<br > &nbsp; &nbsp;url = f'https://movie.douban.com/'<br > &nbsp; &nbsp;getHTML(url)<br >end = start = time.time()<br >print(f'time:{i}s'.format(i=end-start))</p><p><br ></p><p>运行结果 time:99s</p>
    mooc109659764727099005 发表于2023年03月19日
    添加评论
  • 4楼

  • φk1260072289826808925 发表于2023年03月20日
    1 | 0 | 举报
    <p><code class="brush:python;toolbar:false" >import&nbsp;requests import&nbsp;time def&nbsp;getHTML(Url): &nbsp;&nbsp;&nbsp;&nbsp;try: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;requests.get(Url,&nbsp;timeout=30) &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;except: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;产生异常&quot;) start&nbsp;=&nbsp;time.perf_counter() if&nbsp;__name__&nbsp;==&nbsp;&quot;__main__&quot;: &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i&nbsp;in&nbsp;range(100): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url&nbsp;=&nbsp;&quot;https://www.icourse163.org&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getHTML(url) &nbsp;&nbsp;&nbsp;&nbsp;end&nbsp;=&nbsp;time.perf_counter() &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;爬虫100次mooc官网用时为'{}'s&quot;.format(end&nbsp;-&nbsp;start))</code></p>
    φk1260072289826808925 发表于2023年03月20日
    添加评论
  • 5楼

  • 小政今天也很困 发表于2023年03月20日
    0 | 0 | 举报
    <p>import requests</p><p>import time</p><p><br ></p><p><br ></p><p>def getHTML(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_encoding</p><p>&nbsp; &nbsp; except:</p><p>&nbsp; &nbsp; &nbsp; &nbsp; print(&quot;产生异常&quot;)</p><p><br ></p><p><br ></p><p>start = time.perf_counter()</p><p>if __name__ == &quot;__main__&quot;:</p><p>&nbsp; &nbsp; for i in range(100):</p><p>&nbsp; &nbsp; &nbsp; &nbsp; url = &quot;https://www.hbue.edu.cn&quot;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; getHTML(url)</p><p>&nbsp; &nbsp; end = time.perf_counter()</p><p><br ></p><p>&nbsp; &nbsp; print(&quot;爬虫100次湖北经济学院官网用时为'{}'s&quot;.format(end - start))</p><p><br ></p><p>爬虫100次湖北经济学院官网用时为'20.27549691699994's<img src="https://mooc-image.nosdn.127.net/c01ee606ff364afc8c0947b7664465aa.png" /></p>
    小政今天也很困 发表于2023年03月20日
    添加评论
  • 6楼

  • huihui1996 发表于2023年03月22日
    0 | 0 | 举报
    <p><code class="brush:python;toolbar:false" >import&nbsp;requests import&nbsp;time def&nbsp;getHTMLText(url): &nbsp;&nbsp;&nbsp;&nbsp;try: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;requests.get(url,timeout&nbsp;=&nbsp;30) &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.text &nbsp;&nbsp;&nbsp;&nbsp;except: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;&quot;产生异常&quot; if&nbsp;__name__&nbsp;==&nbsp;&quot;__main__&quot;: &nbsp;&nbsp;&nbsp;&nbsp;url&nbsp;=&nbsp;&quot;https://www.bilibili.com/&quot; &nbsp;&nbsp;&nbsp;&nbsp;start_time&nbsp;=&nbsp;time.time() &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i&nbsp;in&nbsp;range(100): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getHTMLText(url) &nbsp;&nbsp;&nbsp;&nbsp;end_time&nbsp;=&nbsp;time.time() &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;The&nbsp;time&nbsp;is&nbsp;:&quot;,&nbsp;end_time&nbsp;-&nbsp;start_time)</code>爬取B站,总用时:52.18911814689636s</p>
    huihui1996 发表于2023年03月22日
    添加评论
  • 7楼

  • 无端ykt1460790672428 发表于2023年03月22日
    0 | 0 | 举报
    <p>import requests</p><p>import time</p><p><br ></p><p><br ></p><p>def getHtmlText(url):</p><p>&nbsp; &nbsp; try:</p><p>&nbsp; &nbsp; &nbsp; &nbsp; start_time=time.time()</p><p>&nbsp; &nbsp; &nbsp; &nbsp; hd={&quot;user-agent&quot;:&quot;Mozilla/5.0&quot;}</p><p>&nbsp; &nbsp; &nbsp; &nbsp; r=requests.get(url,headers=hd,timeout=30)</p><p>&nbsp; &nbsp; &nbsp; &nbsp; r.raise_for_status()</p><p>&nbsp; &nbsp; &nbsp; &nbsp; r.encoding=r.apparent_encoding</p><p>&nbsp; &nbsp; except:</p><p>&nbsp; &nbsp; &nbsp; &nbsp; return &quot;失败&quot;</p><p>def main():</p><p>&nbsp; &nbsp; url=&quot;https://movie.douban.com/top250&quot;</p><p>&nbsp; &nbsp; start_time=time.time()</p><p>&nbsp; &nbsp; for i in range(100):</p><p>&nbsp; &nbsp; &nbsp; &nbsp; getHtmlText(url)</p><p>&nbsp; &nbsp; finish_time=time.time()-start_time</p><p>&nbsp; &nbsp; print(finish_time)</p><p><br ></p><p>main()</p><p><code class="brush:python;toolbar:false" ><br ></code></p>
    无端ykt1460790672428 发表于2023年03月22日
    添加评论
  • 8楼

  • YD.k1262824198151654282 发表于2023年03月23日
    0 | 0 | 举报
    <p>import requests<br >import time<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() &nbsp;# 如果状态不是200,引发HTTPError异常<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 &quot;产生异常!&quot;<br ><br >if __name__ == &quot;__main__&quot;:<br > &nbsp; &nbsp;url = &quot;https://www.bilibili.com&quot;<br > &nbsp; &nbsp;start_time = time.time()<br > &nbsp; &nbsp;for i in range(100):<br > &nbsp; &nbsp; &nbsp; &nbsp;getHTMLText(url)<br > &nbsp; &nbsp;end_time = time.time()<br > &nbsp; &nbsp;print(start_time)<br > &nbsp; &nbsp;print(end_time)<br > &nbsp; &nbsp;print(&quot;爬取时间: &quot;, end_time - start_time)<br ></p><p><br ></p><p>1679542387.7249022</p><p>1679542438.832644</p><p>爬取时间:&nbsp; 51.107741832733154</p><p><br ></p>
    YD.k1262824198151654282 发表于2023年03月23日
    添加评论
  • 9楼

  • 1900300730石海弘 发表于2023年03月24日
    0 | 0 | 举报
    <p><code class="brush:python;toolbar:false" >import&nbsp;requests import&nbsp;time def&nbsp;HTMLtest(url): &nbsp;&nbsp;&nbsp;&nbsp;try: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r=requests.get(url,timeout=30) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.encoding=r.apparent_encoding &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.raise_for_status() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;r.text &nbsp;&nbsp;&nbsp;&nbsp;except: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;&quot;产生异常&quot; url=&quot;https://www.baidu.com&quot; start_time=time.time() for&nbsp;i&nbsp;in&nbsp;range(100): &nbsp;&nbsp;&nbsp;&nbsp;HTMLtest(url) end_time=time.time() print(&quot;爬取百度100次需要时间:%.10f&quot;%(end_time-start_time))</code>爬取百度100次需要时间:6.2275991440</p>
    1900300730石海弘 发表于2023年03月24日
    添加评论
  • 10楼

  • 城轨2004李宸泽 发表于2023年03月24日
    0 | 0 | 举报
    importrequestsimporttimedefHTMLtest(url):try:r=requests.get(url,timeout=30)r.encoding=r.apparent_encodingr.raise_for_status()returnr.textexcept:return&quot;产生异常&quot;url=&quot;https://www.baidu.com&quot;start_time=time.time()foriinrange(100):HTMLtest(url)end_time=time.time()print(&quot;爬取百度100次需要时间:%.10f&quot;%(end_time-start_time))爬取百度100次需要时间:6.2275991440
    城轨2004李宸泽 发表于2023年03月24日
    添加评论
  • 11楼

  • whyinxj110 发表于2023年03月26日
    0 | 0 | 举报
    <p>import requests<br >import time<br ><br >def gethtmltxt(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 &quot;爬取异常&quot;<br >if __name__==&quot;__main__&quot;:<br > &nbsp; &nbsp;url = &quot;https://www.baidu.com&quot;<br > &nbsp; &nbsp;star = time.time()<br > &nbsp; &nbsp;for i in range(100):<br > &nbsp; &nbsp; &nbsp; &nbsp;gethtmltxt(url)<br > &nbsp; &nbsp;end = time.time()<br > &nbsp; &nbsp;print(&quot;爬取百度首页100次耗时{}秒&quot;.format(end-star))</p><p><br ></p><p>爬取百度首页100次耗时8.04139757156372秒<br ></p>
    whyinxj110 发表于2023年03月26日
    添加评论
  • 12楼

  • 乌波乌波 发表于2023年03月27日
    0 | 0 | 举报
    <p>import requests<br >import time<br >def getHTMLText(url):<br > &nbsp; &nbsp;try:<br > &nbsp; &nbsp; &nbsp; &nbsp;r = request.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 &quot;产生异常&quot;<br >start_time=time.perf_counter()<br >url=&quot;https://www.icourse163.org/&quot;<br >for i in range(100):<br > &nbsp; &nbsp;getHTMLText(url)<br >end_time=time.perf_counter()<br >print('爬取&quot;{}&quot;网站100次耗时{:.6f} s。'.format(url,end_time-start_time))<br ><br ><br >爬取&quot;https://www.icourse163.org/&quot;网站100次耗时0.000056 s。<br ></p>
    乌波乌波 发表于2023年03月27日
    添加评论
  • 13楼

  • 沙漠绿洲k1229419850896922880 发表于2023年03月27日
    0 | 0 | 举报
    <p>import requests</p><p>import time</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_encoding</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>url=r'https://www.runoob.com/python3/python3-date-time.html#comment-35499'</p><p>a=time.perf_counter()</p><p>for i in range(100):</p><p>&nbsp; &nbsp; getHTMLText(url)</p><p>b=time.perf_counter()</p><p>print(&quot;爬取了{}网址一百次耗费时间{:.6f}&quot;.format(url,b-a))</p><p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</p><p><br ></p>
    沙漠绿洲k1229419850896922880 发表于2023年03月27日
    添加评论
  • 14楼

  • 收旧铜凹铁 发表于2023年03月28日
    0 | 0 | 举报
    <p># -*- coding:utf-8 -*-</p><p><br ></p><p>import requests</p><p>from datetime import datetime</p><p><br ></p><p>def get_page_content(url,ii):</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_encoding</p><p>&nbsp; &nbsp; &nbsp; &nbsp; return r.text</p><p>&nbsp; &nbsp; except:</p><p>&nbsp; &nbsp; &nbsp; &nbsp; return '第'+str(ii)+'次,爬取失败'</p><p><br ></p><p>def main():</p><p>&nbsp; &nbsp; url = 'https://www.csdn.net/'</p><p>&nbsp; &nbsp; print('爬取网站:'+url)</p><p><br ></p><p>&nbsp; &nbsp; t_start = datetime.now()</p><p>&nbsp; &nbsp;</p><p>&nbsp; &nbsp; for ii in range(1,101):</p><p>&nbsp; &nbsp; &nbsp; &nbsp; get_page_content(url,ii)</p><p>&nbsp; &nbsp;</p><p>&nbsp; &nbsp; t_end = datetime.now()</p><p><br ></p><p>&nbsp; &nbsp; print('爬取100次网页所需时间为',t_end-t_start)</p><p><br ></p><p>if __name__=='__main__':</p><p>&nbsp; &nbsp; main()</p><p><br ></p><p>结果:</p><p>&nbsp;&nbsp;&nbsp;&nbsp;爬取网站:https://www.csdn.net/</p><p>&nbsp;&nbsp;&nbsp;&nbsp;爬取100次网页所需时间为 0:02:41.812902</p>
    收旧铜凹铁 发表于2023年03月28日
    添加评论
  • 15楼

  • 2262410212陈文豪 发表于2023年03月28日
    0 | 0 | 举报
    <p>import requests<br >import time<br >url = 'https://baidu.com'<br >def gehttptext(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.encoding<br > &nbsp; &nbsp; &nbsp; &nbsp;return r.text<br > &nbsp; &nbsp;except:<br > &nbsp; &nbsp; &nbsp; &nbsp;print(&quot;error&quot;)<br >def main():<br > &nbsp; &nbsp;for i in range(1,100):<br > &nbsp; &nbsp; &nbsp; &nbsp;gehttptext(url)<br >start = time.perf_counter()<br >main()<br >end = time.perf_counter()<br >print(&quot;爬取{}网址所用的时间为{:.5f}&quot;.format(url,end-start))</p><p>结果为:19.54443s</p>
    2262410212陈文豪 发表于2023年03月28日
    添加评论
  • 16楼

  • 专升本监狱学一区罗递容 发表于2023年03月29日
    0 | 0 | 举报
    import requestsimport timeurl = &#39;https://baidu.com&#39;def gehttptext(url): try: r = requests.get(url,timeout=30) r.raise_for_status() r.encoding = r.encoding return r.text except: print(&quot;error&quot;)def main(): for i in range(1,100): gehttptext(url)start = time.perf_counter()main()end = time.perf_counter()print(&quot;爬取{}网址所用的时间为{:.5f}&quot;.format(url,end-start))结果为:19.54443s
    专升本监狱学一区罗递容 发表于2023年03月29日
    添加评论
  • 17楼

  • 心心若水 发表于2023年04月03日
    1 | 0 | 举报
    <p>try:</p><p>&nbsp; &nbsp; import time</p><p>&nbsp; &nbsp; import requests</p><p>&nbsp; &nbsp; start_time=time.perf_counter()</p><p>&nbsp; &nbsp; for i in range(100):</p><p>&nbsp; &nbsp; &nbsp; &nbsp; r=requests.get('https://www.baidu.com',timeout=30)</p><p>&nbsp; &nbsp; &nbsp; &nbsp; r.raise_for_status()</p><p>&nbsp; &nbsp; &nbsp; &nbsp; r.encoding=r.apparent_encoding</p><p>&nbsp; &nbsp; &nbsp; &nbsp; r.text</p><p>&nbsp; &nbsp; end_time=time.perf_counter()</p><p>&nbsp; &nbsp; all_time=end_time-start_time</p><p>&nbsp; &nbsp; print('成功爬取100次baidu网页首页的时间为:{:.6f}s'.format(all_time))</p><p><br ></p><p><br ></p><p>except:</p><p>&nbsp; &nbsp; print('程序异常!')</p><p>运行结果:成功爬取100次baidu网页首页的时间为:14.811744s</p>
    心心若水 发表于2023年04月03日
    添加评论
  • 18楼

  • 偶遇余温 发表于2023年04月05日
    0 | 0 | 举报
    <p>import requests</p><p>import time</p><p>try:</p><p>&nbsp; &nbsp; start_time = time.perf_counter()</p><p>&nbsp; &nbsp; for i in range(100):</p><p>&nbsp; &nbsp; &nbsp; &nbsp; r = requests.get(&quot;https://www.baidu.com&quot;,timeout = 30)</p><p>&nbsp; &nbsp; &nbsp; &nbsp; r.raise_for_status()</p><p>&nbsp; &nbsp; &nbsp; &nbsp; r.encoding = r.apparent_encoding</p><p>&nbsp; &nbsp; &nbsp; &nbsp; r.text</p><p>&nbsp; &nbsp; end_time = time.perf_counter()</p><p>&nbsp; &nbsp; all_time=end_time-start_time</p><p>&nbsp; &nbsp; print('成功爬取100次baidu网页首页的时间为:{}s'.format(all_time))</p><p>except:</p><p>&nbsp; &nbsp; print(&quot;程序异常&quot;)</p><p><br ></p><p>成功爬取100次baidu网页首页的时间为:10.521762099997432s</p><p><br ></p>
    偶遇余温 发表于2023年04月05日
    添加评论
  • 19楼

  • 黑大帅k 发表于2023年04月05日
    1 | 0 | 举报
    <p><code class="brush:python;toolbar:false" >import&nbsp;requests import&nbsp;time try: &nbsp;&nbsp;&nbsp;&nbsp;start_time=time.perf_counter() &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i&nbsp;in&nbsp;range(100): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r=requests.get(&quot;https://www.baidu.com&quot;,timeout=30) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.raise_for_status() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.encoding=r.apparent_encoding &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.text &nbsp;&nbsp;&nbsp;&nbsp;end_time=time.perf_counter() &nbsp;&nbsp;&nbsp;&nbsp;all_time=end_time-start_time &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;成功爬取100次百度网页首页的时间为:{}s&quot;.format(all_time)) except: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;程序异常&quot;)</code><code class="brush:python;toolbar:false" >成功爬取100次百度网页首页的时间为:11.2087748s</code></p>
    黑大帅k 发表于2023年04月05日
    添加评论
  • 20楼

  • 尔厘 发表于2023年04月05日
    0 | 0 | 举报
    <p>import requests<br >import time<br ><br ># 爬取bing首页100次计时<br ><br >try:<br > &nbsp; &nbsp;start_time = time.perf_counter() &nbsp;# 初始计时<br > &nbsp; &nbsp;for i in range(100):<br > &nbsp; &nbsp; &nbsp; &nbsp;r = requests.get(&quot;https://www.bing.com&quot;, timeout=10) &nbsp;# requests 在经过以 timeout 参数设定的秒数时间之后停止等待响应<br > &nbsp; &nbsp; &nbsp; &nbsp;r.raise_for_status()<br > &nbsp; &nbsp; &nbsp; &nbsp;r.encoding = r.apparent_encoding<br > &nbsp; &nbsp; &nbsp; &nbsp;r.text<br > &nbsp; &nbsp;end_time = time.perf_counter() &nbsp;# 末尾计时<br > &nbsp; &nbsp;all_time = end_time - start_time &nbsp;# 计算总时长<br > &nbsp; &nbsp;print(&quot;成功爬取100次bing网页首页的时间为:{}s&quot;.format(all_time))<br >except:<br > &nbsp; &nbsp;print(&quot;程序异常&quot;)<br ></p><p><br ></p><p>运行结果:成功爬取100次bing网页首页的时间为:42.14350469969213s</p>
    尔厘 发表于2023年04月05日
    添加评论
点击加载更多