首页 > 解决方案 > 使用 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次,还有其他方法可以测试它吗?谢谢!

标签: pythonweb-scrapingbeautifulsoupstress-testing

解决方案


你可以用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()

推荐阅读