BeautifulSoup .find()给出TypeError

时间:2015-09-15 09:25:12

标签: python unicode find beautifulsoup typeerror

运行脚本时出现此错误。

Traceback (most recent call last):
  File "grabber_test.py", line 45, in <module>
    print child.find(class_ = "tip")
TypeError: find() takes no keyword arguments

这是强制此错误的脚本部分:

for games in last_games:
    print "Your Champion: %s" % (games.find("div", class_ = "championName").string)
    print "Your KDA: %s/%s/%s" % (games.find("span", class_ = "kill").string, games.find("span", class_ = "death").string, games.find("span", class_ = "assist").string)
    team1 = games.find("div", class_ = "teamId-100")
    team2 = games.find("div", class_ = "teamId-200")
    for player1 in team1:
        for child in player1:
            print type(child)
            print child
            print child.find(class_ = "tip")

.find()方法在前四次调用它时工作正常,但之后没有。

类型(子)提供&#34; unicode&#34;

我知道我无法在&#34; unicode&#34;上调用.find(),但为什么它出现在此处而不是之前,我该如何解决?

修改 这是完整的脚本:

#!/usr/bin/env python
# encoding=utf8
import sys
from selenium import webdriver
import time
import urllib2
from bs4 import BeautifulSoup
import requests

global name
global url
#name = raw_input("Summoner name? ")
url = str("http://euw.op.gg/summoner/userName=gotballsbro")
print url

global driver
driver = webdriver.Firefox()
driver.get(url)

#URL öffnen
response = driver.page_source
#verarbeitete URL in BeautifulSoup zur Weiterverarbeitung öffnen
global soup
soup = BeautifulSoup(response, "lxml")
print type(soup)

last_games = soup.findAll("div", class_ = "GameSimpleStats")

for games in last_games:
    print "Your Champion: %s" % (games.find("div", class_ = "championName").string)
    print "Your KDA: %s/%s/%s" % (games.find("span", class_ = "kill").string, games.find("span", class_ = "death").string, games.find("span", class_ = "assist").string)
    team1 = games.find("div", class_ = "teamId-100")
    team2 = games.find("div", class_ = "teamId-200")
    for player1 in team1:
        for child in player1:
            print type(child)
            print child
            print child.find(class_ = "tip")








driver.close()

我将vars设为全局,因为它是我更大脚本的测试文件,我使用defs。

EDIT2: 我将强制错误部分编辑成:

last_games = soup.findAll("div", class_ = "championIcon rawRectImage tip")
for games in last_games:
    print games["title"]

现在它正在工作:)

1 个答案:

答案 0 :(得分:2)

您正在遍历单个元素(<div class="teamId-100">元素的子标记);该元素可以包含其他元素和文本节点

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('''\
... <div>
...     Some text
...     <span>Another element</span>
... </div>
... ''')
>>> list(soup.find('div'))
['\n    Some text\n    ', <span>Another element</span>, '\n']

如果您想循环 标记,请使用team1.find_all()

for player1 in team1.find_all():
    for child in player1.find_all():

演示:

>>> soup.find('div')
<div>
    Some text
    <span>Another element</span>
</div>
>>> soup.find('div').find_all()
[<span>Another element</span>]