So I'm trying to scrape data from a website and get specific values that I will use later in calculations but I am having trouble taking the data I scrape and pulling just the values I want from it. Currently, this is what I have:
import requests
from bs4 import BeautifulSoup
header = {
'User-Agent': 'Mozilla/5.0 (X11; Linux i686 on x86_64)'
}
url = 'https://cars.usnews.com/cars-trucks/ram/1500/2021/specs/1500-tradesman-4x2-quad-cab-6-4-box-414114'
page = requests.get(url, headers=header) # change headers or get blocked
soup = BeautifulSoup(page.content, 'html.parser')
specs = soup.find_all('div', class_="trim-specs columns small-12")
spec_values = []
for spec in specs:
spec_values.extend(spec.find_all('li'))
towing = [x for x in spec_values if 'Maximum Trailering Capacity (lbs.)' in x.string]
print(towing)
From here I get this output:
[<li>Maximum Trailering Capacity (lbs.): 7730</li>]
How could I just pull the value of 7330 from here?
This is one way I found of doing this but it won't work for values that are not integers
towing_num = [int(i) for i in str(towing) if i.isdigit()]
towing_cap = int(''.join(map(str, towing_num)))
print(towing_cap)
This gives me 7730 as an output but this method does not work for any number with a decimal. Is there a more straightforward way of obtaining this value?
Thanks in advance