Python—Requests庫的爬取效能分析
“任意”找個url,測試一下成功爬取100次網頁的時間。(某些網站對於連續爬取頁面將採取遮蔽IP的策略,所以,要避開這類網站。)
import requests
import time
def getHtmlText(url):
try: # try except:用於異常處理
r = requests.get(url, timeout=30) # get到網站 timeout=30:如果get時間超過30s,則停止等待響應
r.raise_for_status() # 檢測是否連線成功
r.encoding = r.apparent_encoding
return r.text
except:
return `執行異常`
if __name__ == "__main__": # Python 模擬的程式入口
url = `https://www.baidu.com`
totaltime = 0
for i in range(100):
starttime = time.perf_counter()
getHtmlText(url)
endtime = time.perf_counter()
print(`第{0}次爬取,用時{1:.4f}秒`.format(i+1, endtime-starttime))
totaltime=totaltime+endtime-starttime
print(`總共用時{:.4f}秒`.format(totaltime))
這是用百度做的測試,有興趣的可以試試別的網站,小心被封ip哦,特別是某些直播網站,封了的話可能一段時間全寢室都看不了直播了!!!哼哼if __name__ == "__main__":
對這條程式碼有疑問的可轉至:http://blog.konghy.cn/2017/04/24/python-entry-program/
網路爬蟲有風險,爬取資料需謹慎
相關文章
- python爬蟲之一:requests庫Python爬蟲
- BeautifulSoup + requests 爬取扇貝 python 單詞書Python
- Python 爬蟲入門 (二) 使用Requests來爬取圖片Python爬蟲
- python爬蟲常用庫之requests詳解Python爬蟲
- 爬蟲之requests庫爬蟲
- Python2爬蟲利器:requests庫的基本用法Python爬蟲
- 使用Python和requests庫的簡單爬蟲程式Python爬蟲
- python+requests爬取B站影片儲存到本地Python
- python爬蟲requests模組Python爬蟲
- Python爬蟲學習筆記-2.Requests庫Python爬蟲筆記
- Python requests爬蟲例項Python爬蟲
- Python 中 Requests 庫的用法Python
- Python-爬取CVE漏洞庫?Python
- python 爬蟲之requests爬取頁面圖片的url,並將圖片下載到本地Python爬蟲
- python使用requests獲取cookiePythonCookie
- JB的Python之旅-爬蟲篇--requests&ScrapyPython爬蟲
- python requests庫的簡單使用Python
- [實戰演練]python3使用requests模組爬取頁面內容Python
- 利用requests+BeautifulSoup爬取網頁關鍵資訊網頁
- Python3爬蟲實戰(requests模組)Python爬蟲
- Python Beautiful Soup+requests實現爬蟲Python爬蟲
- Python網路爬蟲資料採集實戰:Requests和Re庫Python爬蟲
- 爬蟲——Requests模組爬蟲
- 爬蟲-Requests模組爬蟲
- 爬蟲入門系列(二):優雅的HTTP庫requests爬蟲HTTP
- 6.爬蟲 requests庫講解 總結爬蟲
- 使用requests庫獲取這個URL
- Python----Requests庫基本使用Python
- python爬蟲:爬蟲的簡單介紹及requests模組的簡單使用Python爬蟲
- Python爬蟲十六式 - 第三式:Requests的用法Python爬蟲
- 基於bs4+requests的python爬蟲偽裝Python爬蟲
- python爬蟲利用requests製作代理池sPython爬蟲
- Python 爬蟲實戰(二):使用 requests-htmlPython爬蟲HTML
- Python 爬蟲實戰(一):使用 requests 和 BeautifulSoupPython爬蟲
- Python爬取分析豆瓣電影Top250Python
- 5.爬蟲 requests庫講解 高階用法爬蟲
- python入門012~使用requests爬取網路圖片並儲存到本地Python
- Python HTTP庫:requests快速入門PythonHTTP