" 'resultSet'을받는 이유는 Python에서 BeautifulSoup을 사용하여 'findall'에 속성이 없습니까?

StackOverflow https://stackoverflow.com/questions/992183

  •  13-09-2019
  •  | 
  •  

문제

그래서 나는 Python을 천천히 배우고 있으며 온라인 게임의 높은 점수 페이지에서 데이터를 그릴 간단한 기능을 만들려고 노력하고 있습니다. 이것은 내가 한 기능으로 다시 작성한 다른 사람의 코드이지만 (문제가 될 수 있음)이 오류가 발생합니다. 코드는 다음과 같습니다.

>>> from urllib2 import urlopen
>>> from BeautifulSoup import BeautifulSoup
>>> def create(el):
    source = urlopen(el).read()
    soup = BeautifulSoup(source)
    get_table = soup.find('table', {'id':'mini_player'})
    get_rows = get_table.findAll('tr')
    text = ''.join(get_rows.findAll(text=True))
    data = text.strip()
    return data

>>> create('http://hiscore.runescape.com/hiscorepersonal.ws?user1=bigdrizzle13')

Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    create('http://hiscore.runescape.com/hiscorepersonal.ws?user1=bigdrizzle13')
  File "<pyshell#17>", line 6, in create
    text = ''.join(get_rows.findAll(text=True))
AttributeError: 'ResultSet' object has no attribute 'findAll'

미리 감사드립니다.

도움이 되었습니까?

해결책

우와. Triptych는 a 엄청난 대답 관련 질문에.

우리는 볼 수있다, BeautifulSoup의 소스 코드에서, 저것 ResultSet 서브 클래스 list.

당신의 예에서 get_rows BS의 인스턴스입니다 ResultSet 수업,
그리고 BS 이후 ResultSet 서브 클래스 list, 그것의 의미는 get_rows는 목록입니다.

get_rows, 인스턴스로 ResultSet, 하다 ~ 아니다 가지고있다 findAll 구현 된 방법; 따라서 오류.
Triptych가 다르게 한 일은 반복 그 목록 위에.
Triptych의 방법은 get_rows 목록은 BS의 태그 클래스의 인스턴스입니다. 여기에는 findAll 방법.

따라서 코드를 수정하려면 마지막 세 줄을 교체 할 수 있습니다. create 이와 같은 방법 :

for row in get_rows:
    text = ''.join(row.findAll(text=True))
    data = text.strip()
    print data

Leonard Richardson에 대한 참고 사항 : 결코 BS라고함으로써 귀하의 작업의 질을 무시하려고하지 않습니다 .-)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top