2016-07-20 2 views
1

웹 사이트에서 이길 확률을 예측할 수 있는지 알아보기 위해 부 프로젝트에서 작업하고 있습니다.하지만 BeautifulSoup를 사용한 첫 번째 사례 중 하나입니다. 크기를 줄이는 방법에 대해 전적으로 확신합니다.Python, BeautifulSoup - 문자열 부분 추출 중

여기 코드가 있습니다. 기본적으로 충돌이 발생한 위치의 정보를 얻고 싶습니다.

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 

실행하면이 결과가 출력됩니다. 나는 기본적으로 나만 양쪽에서 잘라 필요 숫자 값을 잡아하고자 할

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

, 난 그냥이 일을뿐만 아니라 HTML 태그를 제거하는 방법에 대한 이동하는 방법을 모르겠어요.

답변

2

텍스트로 Crashed At 라벨을 찾아 다음 형제 수 :

: 단일 Crashed At 값이 있으므로이 경우 루프를 필요로하는 경우, 또한 확실하지

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 

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) 
+0

지금 오류가있는 것 같습니다 추적 (가장 최근 통화 마지막) : 파일 "C : /Python27/CSGOCrash/2xProgram.py ", 줄 8, crashPoint = section.find ("b ", text ="오류 발생 : ") .next_sibling.strip() AttributeError : 'NoneType'개체에 특성이 없습니다. 'strip' –

+0

@BenParry 명시 적으로 파서를 지정하고 대답을 업데이트해야한다고 생각합니다. – alecxe

+0

아직도 나와 함께 일하고 싶지 않습니다. 나는 두려워합니다. –