29 lines
656 B
Python
29 lines
656 B
Python
import requests
|
|
import time
|
|
|
|
#爬取100次网页所需时间
|
|
def get_HTML_Text():
|
|
i = 0
|
|
while i < 100:
|
|
try:
|
|
r = requests.get(url)
|
|
r.raise_for_status()
|
|
r.encoding = r.apparent_encoding
|
|
return r.status_code
|
|
except:
|
|
print("出现异常")
|
|
|
|
i += 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
time1 = time.time()
|
|
url = "http://www.baidu.com"
|
|
u = 0 # 记录爬取异常次数
|
|
for i in range(100 + u):
|
|
if get_HTML_Text() == 200:
|
|
u = u
|
|
else:
|
|
u += 1
|
|
time2 =time.time()
|
|
print(f"爬取100次所需要的时间为{time2-time1}秒") |