2014-02-11 6 views
2

이상한 오류가 발생했습니다. 나는 기본적인 파싱을하려고 노력 중이다. 기본적으로 'x'형식의 데이터를 수집하고 사용할 수있는 형식으로 모든 것을 반환하려고합니다. 내 문제는 내 코드가 이상한 오류를 반환한다는 것입니다. 나는 같은 문제에 대해 다른 게시물/답변을 여기에서 보았지만 컨텍스트를 벗어나서 ... 문제를 정확하게 지적하기는 어렵다. 이 시점에서 분명히 TypeError : 'NoneType'개체를 호출 할 수 없습니다. BeautifulSoup

data = url.text 

soup = BeautifulSoup(data, "html5lib") 

results = [] # this is what my result set will end up as 

def parseDiv(text): 
    #function takes one input parameter - a single div for which it will parse for specific items, and return it all as a dictionary 
    soup2 = BeautifulSoup(text) 
    title = soup2.find("a", "yschttl spt") 
    print title.text 
    print 

    return title.text 

for result in soup.find_all("div", "res"): 
    """ 
    This is where the data is first handled - this would return a div with links, text, etc - 
    So, I pass the blurb of text into the parseDiv() function 
    """ 
    item = parseDiv(result) 
    results.append(item) 

, 내 필요한 라이브러리를 포함 시켰습니다 ... 나는 (텍스트 처리 할 내 새로운 선전에 BS4의 두 번째 인스턴스를) soup2에 대한 코드를 당긴 때 바로 인쇄 내 기능의 입력, 그것은 모두 작동합니다. 당신은 다시 한 번 div의 구문을 분석 할 필요가 없습니다

Traceback (most recent call last): 
    File "testdata.py", line 29, in <module> 
    item = parseDiv(result) 
    File "testdata.py", line 17, in parseDiv 
    soup2 = BeautifulSoup(text) 
    File "C:\Python27\lib\site-packages\bs4\__i 
    markup = markup.read() 
TypeError: 'NoneType' object is not callable 
+1

전체 오류 스택 추적을 게시하십시오. – thefourtheye

+1

"하지만 문맥을 벗어나서 ... 문제를 정확하게 지적하기는 어렵습니다."- 불완전한 역 추적에도 똑같이 적용됩니다. 모든 라인을 포함 할 수 있습니까? – mhlester

+0

스택 추적을 표시하도록 수정되었습니다. – Alpinestar22

답변

4

: 여기

는 오류입니다. 이것을 시도하십시오 :

for div in soup.find_all('div', 'res'): 
    a = div.find('a', 'yschttl spt') 
    if a: 
     print a.text 
     print 
     results.append(a) 
+0

고마워요. @Jayanth,이 문제는 해결되었지만 지금은 속성 오류가 발생했습니다. Traceback (가장 최근의 마지막 통화) : 파일 "testdata.py", 줄 24, 항목 [ 'title'] = a.text AttributeError : 'NoneType'객체에 'text'속성이 없습니다. – Alpinestar22

+0

@ Alpinestar22 그러면 분명히 무언가가 'None'을 반환 했으므로 확인해야합니다. – glglgl

+0

@ Alpinestar22 : 'divs'중 하나에 'yschttl spt'클래스의 'a'가 없으면 발생합니다. –

관련 문제