【发布时间】:2015-09-15 09:25:11
【问题描述】:
运行脚本时出现此错误。
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() 方法在前四次调用它时工作正常,但之后就不行了。
type(child) 给出 "unicode"
我知道我不能在“unicode”上调用 .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。
编辑2: 我将强制错误部分编辑为:
last_games = soup.findAll("div", class_ = "championIcon rawRectImage tip")
for games in last_games:
print games["title"]
现在它正在工作:)
【问题讨论】:
-
您应该提供 html 和 minimal reproducible example 来重现您的问题。
-
不要将解决方案(答案)放入您的问题中。你可以post your own answer
标签: python unicode find beautifulsoup typeerror