from requests import
from time import
def gettxturl(url):
try:
r=requests.get(url,timeout=10)
print(r.raise_for_status())
r.encoding=r.apparent_encoding
return r.text
except:
return "爬取失败!"
if name=="main":
url='https://www.baidu.com'
t1=float(time())
for i in range(10):
print(gettxturl(url))
t2=float(time())
print(t2-t1)
为啥是爬取失败?格式问题可以忽略
import requests
from time import *
def gettxturl(url):
try:
r=requests.get(url,timeout=10)
print(r.raise_for_status())
r.encoding=r.apparent_encoding
return r.text
except:
return "爬取失败!"
if __ name__ == '__ main__':
url='https://www.baidu.com'
t1=float(time())
for i in range(10):
print(gettxturl(url))
t2=float(time())
print(t2-t1)