简体   繁体   中英

Python, BeautifulSoup - Extracting part of a string

I'm working on a side project to see if I can predict the wins on a website, however this is one of the first times I've used BeautifulSoup and I'm not entirely sure on how to cut a string down to size.

Here is the code, I basically want to grab the information that holds where it crashed.

from bs4 import BeautifulSoup
from urllib import urlopen

html = urlopen('https://www.csgocrash.com/game/1/1287324').read()
soup = BeautifulSoup(html)

for section in soup.findAll('div',{"class":"row panel radius"}):
    crashPoint = section.findChildren()[2]
    print crashPoint

Upon running it, I get this as the output.

<p> <b>Crashed At: </b> 1.47x </p>

I want to basically only want to grab the number value, which would require me to cut from both sides, I just don't know how to go about doing this, as well as removing the HTML tags.

Find the Crashed At label by text and get the next sibling :

soup = BeautifulSoup(html, "html.parser")

for section in soup.findAll('div', {"class":"row panel radius"}):
    crashPoint = section.find("b", text="Crashed At: ").next_sibling.strip()
    print(crashPoint)  # prints 1.47x

Also, not sure if you need a loop in this case since there is a single Crashed At value:

from bs4 import BeautifulSoup
from urllib import urlopen

html = urlopen('https://www.csgocrash.com/game/1/1287324').read()
soup = BeautifulSoup(html, "html.parser")

section = soup.find('div', {"class":"row panel radius"})
crashPoint = section.find("b", text="Crashed At: ").next_sibling.strip()
print(crashPoint)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM