python - BeautifulSoup .find() 给出 TypeError

标签 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() 方法在前四次调用时工作正常,但之后就不行了。

类型(子)给出“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()

我将变量设置为全局变量,因为它是我的较大脚本的测试文件,我在其中使用 defs。

编辑2: 我将强制错误部分编辑为:

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

现在它可以工作了:)

最佳答案

您正在循环单个元素(<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>]

关于python - BeautifulSoup .find() 给出 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32582408/

相关文章:

python - 将 Bottle FORMSDICT 转换为 Python 字典(以线程安全的方式)

python - 使用 Django 将 Assets 通过管道传输到 CDN 的最佳方式是什么?

string - Lua中阿拉伯字母的长度

bash - xargs -0 后如何执行多个命令?

shell - 'find -exec' 或 'find | xargs -0' 哪个更快?

python - 我可以在用 savez 保存时注释一个 numpy 数组吗

python - 如何在 ListView 中包含 CreateView 模板?

php - 在php中读取unicode文件

regex - 如何替换除西​​类牙语以外的所有 unicode 字符?

bash - 根据文本文件将文件移回原始位置