python - 使用 webscrapig 对网站进行压力测试
问题描述
我的朋友用node.js写了一个网站,我想帮他做压力测试,以防万一有一天它崩溃了。我用beautifulsoup测试了一下,效果不错。
代码:
for i in range(0,1000):
#i=i+1
l=randint(2008, 2018)
first="year=%d" %l
k=randint(1600, 2400)
second="cc=%d" %k
url="http://xx.xxx.xxx.xxx:xxxx/outputData?{0}&{1}&submit=Submit".format(first,second)
res = requests.get(url)
soup=BeautifulSoup(res.text,'lxml')
print(soup)
如果我想使用python同时运行1000个代码,而不是依次运行1000次,还有其他方法可以测试它吗?谢谢!
解决方案
你可以用threading
. 但我不建议创建 1000 个线程并同时运行它。
import threading
def testit():
l=randint(2008, 2018)
first="year=%d" %l
k=randint(1600, 2400)
second="cc=%d" %k
url="http://xx.xxx.xxx.xxx:xxxx/outputData?{0}&{1}&submit=Submit".format(first,second)
res = requests.get(url)
soup=BeautifulSoup(res.text,'lxml')
print(soup)
threads = [threading.Thread(target=testit) for i in range(1000)] # Not recommend to use 1000 here
for t in thread:
t.start()
推荐阅读
- python - Groupby、Shift 和 Sum
- python - 使用 pandas DataFrame 的多列的 relplot() 连续误差条图
- javascript - 我无法弄清楚为什么我的 ajax 没有将数据发送到我的 php 文件
- java - 在java环境中,如何关联进程以在特定监视器上运行?
- c++ - vc142 和 2019 可再发行包与 vc141 的比较
- css - Bootstrap 4.3 card-deck 不设置列数
- hdfs - 如何 Sqoop 从 HDFS 中的不同数据库导入数据
- python - 如何从文本文件中获取多个坐标?
- python - 如何在python中创建三角形内核?
- java - 测试服结束后刷新eclipse工作区