python爬虫

以https://www.sciencedirect.com/为例,测试request网络100次网络爬虫所需要的时间

import requests 
import time
 
def getHTMLText(url):
    try:
        r=requests.get(url,timeout=30)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
    except:
        return "产生异常"
def main():
	start=time.time()
	for i in range(1,101):
		getHTMLText(url)
		print("第{}次爬取成功".format(i))
	end=time.time()
	return end - start

url="https://www.sciencedirect.com/"

if __name__ == '__main__':
	time = main()
	print("爬取'{}'100次所需时间:{:.3f}秒".format(url,time))
	print("每爬取一次'{}'100次所需时间:{:.3f}秒".format(url,time/100))

部分输出结果如图
python爬虫_第1张图片

你可能感兴趣的:(Study,Note,python,httpwebrequest)