import re
import requests
from bs4 import BeautifulSoup
def parse_benchmark_results(url):
try:
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
results = soup.find_all('div', class_='bench-result')
benchmarks = {}
for result in results:
text = result.get_text(strip=True)
match = re.search(r'(\d+)-Core (\d+)', text)
if match:
core = int(match.group(1))
speed = int(match.group(2))
benchmarks[core] = speed
return benchmarks
except requests.exceptions.RequestException as e:
print(f"Error fetching the webpage: {e}")
except Exception as e:
print(f"An error occurred: {e}")
# Use the function with the provided URL
benchmark_url = "https://cpu.userbenchmark.com/SpeedTest/2295306/IntelR-CoreTM-i9-14900KS"
results = parse_benchmark_results(benchmark_url)
if results:
for core, speed in results.items():
print(f"Core {core}: {speed}")
else:
print("No benchmark results found.")
This script fetches the webpage, parses the "Average Bench" section for each CPU core, and stores the results in a dictionary. If there was an error during the process, it will print the error message. Modify the benchmark_url variable to test with different processor searches.
My apologies for the confusion. Let's proceed with another approach to scrape the data efficiently and accurately without relying on the specific class names. We'll focus on the structure of the webpage and the format in which the data is presented. Here's a revised, more general and maintainable Python script that uses BeautifulSoup to scrape the "Average Bench" results:
python
import re
import requests
from bs4 import BeautifulSoup
def scrape_cpu_benchmarks(url):
try:
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
# Assuming there is a consistent pattern for the benchmarks
# and that each benchmark result is contained within a 'div' with a 'data-benchmark' attribute
# and the actual performance number is within a 'span' with a 'data-score' attribute
benchmarks = {}
for benchmark in soup.find_all('div', attrs={'data-benchmark': True}):
score_span = benchmark.find('span', attrs={'data-score': True})
if score_span:
# Assuming the score is prefixed with 'Score:' and we extract the numeric part
score_text = score_span.get_text(strip=True)
core_match = re.search(r'(\d+)-Core', score_text)
if core_match:
core_number = int(core_match.group(1))
speed = int(score_text.split(' ')[1]) # Taking the speed number as the second element
benchmarks[core_number] = speed
return benchmarks
except requests.exceptions.RequestException as e:
print(f"Error fetching the webpage: {e}")
except Exception as e:
print(f"An error occurred: {e}")
# Specify the URL for the CPU benchmark
benchmark_url = "https://cpu.userbenchmark.com/SpeedTest/2295306/IntelR-CoreTM-i9-14900KS"
results = scrape_cpu_benchmarks(benchmark_url)
if results:
for core, speed in results.items():
print(f"Core {core}: {speed}")
else:
print("No benchmark results found.")
This script assumes that each benchmark result is contained within a div with a data-benchmark attribute and that the actual performance number is within a span with a data-score attribute. The script parses the score text, extracts the core number and speed, and stores them in a dictionary. The function scrape_cpu_benchmarks can be used with any URL containing benchmark data.
Please ensure that the structure of the webpage you are scraping conforms to the assumptions made in this script. If the webpage structure is different, the approach for finding and parsing the benchmarks would need to be adjusted accordingly.