python websocket的Beautifullsoup

时间:2019-01-14 11:50:23

标签: python button websocket beautifulsoup

我在stackoverflow中的第一个味精... :-) 我的项目:我想使用beautifullsoup,使用日常的python例程,以网站http://www.bredbandskollen.se/给出的互联网速度检索数据。

虽然我找到了使用bs检索对我感兴趣的测试结果的不同方法,但我因无法成功启动/激活速度测试(基本上是单击按钮)而被困扰。 / p>

我可以注意到没有发送常规的HTML5表单。相反,我看到一个“ ws”请求,但我不知道它是什么。

有人知道如何启动速度测试,模拟按钮单击或建议我如何处理ws请求吗?

thx。

1 个答案:

答案 0 :(得分:1)

硒将是模拟打开页面然后单击按钮的过程的好方法。然后等待测试完成并获取结果。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

driver = webdriver.Chrome("C:/chromedriver_win32/chromedriver.exe")

driver.get("http://www.bredbandskollen.se/")
driver.find_element_by_id("mainStartTest").click()

# Waits until test is complete. Timesout after 60 seconds
WebDriverWait(driver, 60).until(ec.presence_of_element_located((By.XPATH, './/span[@class = "bbk-test-info-value" and text() != ""]')))


# Get the results
results = driver.find_elements_by_xpath('.//span[@class = "bbk-test-box-result"]')
dlSpeed = results[0].text
ulSpeed = results[1].text
ltSpeed = results[2].text

print ('Results\nDownloading: %s\nUploading: %s\nLatency: %s' %(dlSpeed, ulSpeed, ltSpeed))

driver.close()

输出:

Results
Downloading: 39,86 Mbit/s
Uploading: 4,16 Mbit/s
Latency: 240,15 ms

您仍然可以使用BeautifulSoup,但这要等到测试运行之后才能进行。但是并不是真正需要的,但至少可以让您了解如何使用硒和beautifulsoup查找具有所需数据的标签:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import bs4

driver = webdriver.Chrome("C:/chromedriver_win32/chromedriver.exe")

driver.get("http://www.bredbandskollen.se/")
driver.find_element_by_id("mainStartTest").click()

# Waits until test is complete. Timesout after 60 seconds
WebDriverWait(driver, 60).until(ec.presence_of_element_located((By.XPATH, './/span[@class = "bbk-test-info-value" and text() != ""]')))


# Get the results
soup = bs4.BeautifulSoup(driver.page_source, 'html.parser')
results = soup.find_all('span', {'class':'bbk-test-box-result'})

dlSpeed = results[0].text
ulSpeed = results[1].text
ltSpeed = results[2].text

print ('Results\nDownloading: %s\nUploading: %s\nLatency: %s' %(dlSpeed, ulSpeed, ltSpeed))

driver.close()