如何网页抓取速度?

时间:2020-10-03 20:44:33

标签: python python-3.x web-scraping python-requests

我想知道如何使用python在Fast.com网站上抓取速度

我做了一些努力,这是我到目前为止所做的:

import requests
from bs4 import BeautifulSoup

response = requests.get('https://fast.com/', headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12"})
soup = BeautifulSoup(response.text, 'lxml')
speed = soup.find('span', {'id' : 'speed-value'}).text

print(speed)

输出始终为“ 0”,有时会给我一个错误

我的目标是获得扫描后网站上显示的MB / s速度编号。

我忘了做什么?

1 个答案:

答案 0 :(得分:2)

根据我的个人经验,

BeautifulSoups更适合用于静态页面。我建议使用Selenium进行更多动态使用。它将允许在加载javascript等之后访问,以便更轻松地进行网页抓取。

from selenium import webdriver
driver_path = r"C:\chromedriver.exe"
driver = webdriver.Chrome(driver_path)

MBPS_CLASS = "speed-results-container"

driver.get("https://fast.com/")
while True:
    print(driver.find_elements_by_class_name(MBPS_CLASS)[0].text)
    # driver.find_element_by_id("speed-value").text # This works with ID also
相关问题