Python 使用BeautifulSoup查找特定文本

Python 使用BeautifulSoup查找特定文本,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我正试图从本页中获取所有获奖者类别: 我用崇高的语言写了这篇文章: import urllib2 from bs4 import BeautifulSoup url = "http://www.chicagoreader.com/chicago/BestOf?category=4053660&year=2013" page = urllib2.urlopen(url) soup_package = BeautifulSoup(page) page.close() #find ever

我正试图从本页中获取所有获奖者类别:

我用崇高的语言写了这篇文章:

import urllib2
from bs4 import BeautifulSoup
url = "http://www.chicagoreader.com/chicago/BestOf?category=4053660&year=2013"
page = urllib2.urlopen(url)
soup_package = BeautifulSoup(page)
page.close()

#find everything in the div class="bestOfItem). This works.
all_categories = soup_package.findAll("div",class_="bestOfItem")
# print(all_categories)

#this part breaks it:
soup = BeautifulSoup(all_categories)
winner = soup.a.string
print(winner)
当我在终端中运行此命令时,出现以下错误:

Traceback (most recent call last):
  File "winners.py", line 12, in <module>
    soup = BeautifulSoup(all_categories)
  File "build/bdist.macosx-10.9-intel/egg/bs4/__init__.py", line 193, in __init__
  File "build/bdist.macosx-10.9-intel/egg/bs4/builder/_lxml.py", line 99, in prepare_markup
  File "build/bdist.macosx-10.9-intel/egg/bs4/dammit.py", line 249, in encodings
  File "build/bdist.macosx-10.9-intel/egg/bs4/dammit.py", line 304, in find_declared_encoding
TypeError: expected string or buffer
回溯(最近一次呼叫最后一次):
文件“winners.py”,第12行,在
汤=美汤(所有类别)
文件“build/bdist.macosx-10.9-intel/egg/bs4/_init__.py”,第193行,in__init__
文件“build/bdist.macosx-10.9-intel/egg/bs4/builder/_lxml.py”,第99行,在prepare_标记中
文件“build/bdist.macosx-10.9-intel/egg/bs4/dammit.py”,第249行,编码形式
文件“build/bdist.macosx-10.9-intel/egg/bs4/dammit.py”,第304行,采用find_声明的_编码
TypeError:应为字符串或缓冲区

有人知道那里发生了什么吗?

您正试图从元素列表中创建一个新的
BeautifulSoup
对象

soup = BeautifulSoup(all_categories)
在这里绝对没有必要这样做;只需在每场比赛上循环:

for match in all_categories:
    winner = match.a.string
    print(winner)