Устранение неполадок AttributeError: объект ResultSet не имеет атрибута findAll - PullRequest
2 голосов
/ 27 апреля 2011

Я пытаюсь разобрать страницу http://www.ted.com/talks для всех имен разговора.Используя BeautifulSoup, вот что у меня есть:

import urllib2

from BeautifulSoup import BeautifulSoup

page = urllib2.urlopen("http://www.ted.com/talks")

soup = BeautifulSoup(page)

link = soup.findAll(lambda tag: tag.name == 'a' and tag.findParent('dt', 'thumbnail'))

for anchor in link.findAll('a', title = True):
    print anchor['title']

Начальная "ссылка" отображает хороший массив блока из восьми видео.Затем я пытаюсь пройти через это и вынуть заголовки в тегах, используя приведенный выше код, который выдает мне следующую ошибку:

for anchor in link.findAll('a', title=True):
AttributeError: 'ResultSet' object has no attribute 'findAll'

Что я делаю не так?

Ответы [ 2 ]

3 голосов
/ 27 апреля 2011

link - это коллекция Tag объектов, которые вам нужно перебрать.Например:

for anchor in link:
    print anchor['title']
0 голосов
/ 27 апреля 2011

Для сравнения, подход с разбиранием будет выглядеть следующим образом:

from contextlib import closing
import urllib2
from pyparsing import makeHTMLTags, withAttribute

# pull HTML from web page
with closing(urllib2.urlopen("http://www.ted.com/talks")) as page:
    html = page.read()

# define opening and closing tags
dt,dtEnd = makeHTMLTags("dt")
a,aEnd = makeHTMLTags("a")

# restrict <dt> tag matches to those with class='thumbnail'
dt.setParseAction(withAttribute(**{'class':'thumbnail'}))

# define pattern of <dt> tag followed immediately by <a> tag
patt =  dt + a("A")

# scan input html for matches of this pattern, and access
# attributes of the <A> tag
for match,s,e in patt.scanString(html):
    print match.A.title
    print match.A.href
    print

Предоставление:

Bruce Schneier: The security mirage
/talks/bruce_schneier.html

Harvey Fineberg: Are we ready for neo-evolution?
/talks/harvey_fineberg_are_we_ready_for_neo_evolution.html

Ric Elias: 3 things I learned while my plane crashed
/talks/ric_elias.html

Anil Ananthaswamy: What it takes to do extreme astrophysics
/talks/anil_ananthaswamy.html

John Hunter on the World Peace Game
/talks/john_hunter_on_the_world_peace_game.html

Kathryn Schulz: On being wrong
/talks/kathryn_schulz_on_being_wrong.html

Sam Richards: A radical experiment in empathy
/talks/sam_richards_a_radical_experiment_in_empathy.html

Susan Lim: Transplant cells, not organs
/talks/susan_lim.html

Marcin Jakubowski: Open-sourced blueprints for civilization
/talks/marcin_jakubowski.html

Roger Ebert: Remaking my voice
/talks/roger_ebert_remaking_my_voice.html
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...