2014-12-01 2 views
0

나는 현재 Beautifulsoup로 크롤러에서 일하고 있습니다. 정렬되지 않은 목록의 특정 하위 데이터를 가져오고 싶습니다.Beautifulsoup 특정 어린이의 콘텐츠를 가져 오는 중

그래서 웹 페이지는 다음과 같이 기본적으로 :

<div class= product-list-item--usp-list> 

    <ul class="unordered-list"> 
     <li>a</li> 
     <li>b</li> 
     <li>c</li> 
    </ul> 

나는 현재는 0 번째 아이의 콘텐츠를 수신하고 있습니다 (A). 나는 첫 번째와 두 번째 자녀의 내용 (b & c)을 얻고 싶다. 내 코드는 다음과 같다 :

a = item.find("ul", class_="unordered-list").li 
    b = item.find("ul", class_="unordered-list").li 

, 그래서이 시도 :. A = item.find ("UL"을, 클래스 _ = "정렬되지 않은리스트") 리 [1] B = item.find ("UL", 클래스 _ = "정렬되지 않은 목록") 리 [2]

이이었다 내 오류 :.

a = item.find("ul", class_="unordered-list").li[1] 
    File "/usr/local/lib/python2.7/dist-packages/bs4/element.py", line 905, in __getitem__ 
    return self.attrs[key] 
KeyError: 1 
[Finished in 2.9s with exit code 1] 

내 질문은 : 내가 child[1]child[2의 컨텐츠를 수신 할 방법]? 미리 감사드립니다.

답변

0

다음과 같이 할 수 있습니다.

>>> from bs4 import BeautifulSoup 
>>> s = """<div class= product-list-item--usp-list> 

    <ul class="unordered-list"> 
     <li>a</li> 
     <li>b</li> 
     <li>c</li> 
    </ul> """ 
>>> soup = BeautifulSoup(s) 
>>> foo = soup.find("ul", class_="unordered-list") 
>>> [i.text for i in foo.find_all('li')[1:]] 
['b', 'c'] 
+0

@henk'[1 :] '목록 조각. 그래서 두 번째 요소에서 가져옵니다. –

관련 문제