2017-10-14 3 views
0

Beautiful Soup로 웹 사이트를 긁어 모으고 있습니다. 나는 테이블에서 "prtype"텍스트를 찾고 있습니다. 내 문제는이 열이 항상 존재하지 않는다는 것입니다.python beautifulsoup .text 없음 유형

'NoneType' object has no attribute 'text' 

내 시도 중 하나였다 : 나는 다음과 같은 오류를 얻을이 클래스와 컬럼이없는 경우,

prtyp = soup.find("dd", attrs={"class":"is_type g"}).text.strip() 

을하지만 :

열이 다음과 같은 코드가 존재하는 경우

잘 작동합니다 문제를 없애기 위해 prtyp은 str이고 전체 HTML 태그를 가져 오거나 .text가 작동하지 않습니다. 당연하지.

prtyp = soup.find("dd", attrs={"class":"is_type g"}) 
if prtyp is None: 
    prtyp = "no type" 
else: 
    whgtyp.text.strip() 
    print("prtype:", prtype) 
+0

가'보십시오 whgtyp.text.strip() 인쇄 ("prtype", prtype을); AttributeError 제외 : prtyp = "유형 없음" – davedwards

답변

0

당신은 당신이 할 수있는 문자열을 추출 싶은 경우 문자열이 HTML 본문에있는 경우

if "prtype" in soup.find("body").text: 
    prtyp = soup.find("dd", attrs={"class":"is_type g"}) 
    if prtyp is None: 
     prtyp = "no type" 
    else: 
     # whgtyp.text.strip() # I don't know what it does 
     print("prtype:", prtype) 

당신은 확인할 수에 따라, 어쨌든 나는 당신의 발견 방법에 당신이 찾고있는 것을 볼 수 "dd"태그는 테이블에서 검색하려는 경우 td 태그 (HTML 테이블 인 경우)에 있어야합니다.

0

답장을 보내 주셔서 감사합니다. 아직 그것을 시도하지만, 한 줄 응답 발견 didnt가 :

prtyp = soup.find("dd", attrs={"class":"is_type g"}).text.strip() if soup.find("dd", attrs={"class":"is_type g"}) else "no type"