【问题标题】:AttributeError: 'NoneType' object has no attribute 'find' within tbodyAttributeError:'NoneType' 对象在 tbody 中没有属性 'find'
【发布时间】:2019-03-07 08:00:09
【问题描述】:

我是 python 领域的新手,并尝试设置一个网络抓取工具。所以我正在试验一些代码。

import requests
import bs4

website = requests.get("https://www.hltv.org/stats/teams")

soup = bs4.BeautifulSoup(website.text, "html.parser")

leaderboard = soup.find("table", {id: "stats-table player-ratings-table"})
tbody = leaderboard.find("tbody")

for tr in tbody.find.all('tr'):
    team = tr.find.all('td')[0].text.strip()
    maps = tr.find.all('td')[1].text.strip()
    kd = tr.find.all('td')[3].text.strip()
    rating = tr.find.all('td')[4].text.strip()

    print(team, maps, kd, rating)

我收到以下错误,有什么帮助吗?我用的是 2.7。

 File "/Users/*****/Python/New Webscraping/WebS.py", line 11, in <module>
    tbody = leaderboard.find("tbody")
AttributeError: 'NoneType' object has no attribute 'find'

Process finished with exit code 1

【问题讨论】:

标签: python web-scraping attributeerror


【解决方案1】:

尝试以下操作以获得所需的输出。我通过像[1:] 那样索引它来忽略第一个tr,因为其中没有td。而且BeautifulSoup中没有.find.all()这样的方法。您可以改用.find_all().findAll()

import requests
from bs4 import BeautifulSoup

res = requests.get("https://www.hltv.org/stats/teams")
soup = BeautifulSoup(res.text, "html.parser")
for tr in soup.find("table",class_="player-ratings-table").find_all("tr")[1:]:
    team = tr.find_all('td')[0].text.strip()
    maps = tr.find_all('td')[1].text.strip()
    kd = tr.find_all('td')[3].text.strip()
    rating = tr.find_all('td')[4].text.strip()
    print(team, maps, kd, rating)

【讨论】:

    【解决方案2】:

    查看您尝试抓取的网站的源代码,您查找的关键字似乎不是id,而是class

    <table class="stats-table player-ratings-table">
    

    所以你应该把你的代码改成:

    leaderboard = soup.find("table", {'class': "stats-table player-ratings-table"})
    

    您还应该将find.all 更改为findAll

    【讨论】:

      【解决方案3】:

      阅读错误是一项在编程时会大大受益的技能。对您而言,错误的重要部分是:

      AttributeError: 'NoneType' object has no attribute 'find' 上的line 11 ...WebS.py

      以及引发错误的实际代码行:tbody = leaderboard.find("tbody")

      属性find在该行的引用leaderboard上被调用,错误告诉你NoneTypes没有find,所以这意味着leaderboard == None

      为了进一步分解错误,如果您仍然感到困惑(对不同的错误),您应该阅读该冒号之前的内容,在本例中为 AttributeError。但最常见的情况是,您应该在 Google 上搜索错误。

      【讨论】:

        【解决方案4】:

        这意味着有些项目没有团队或地图或孩子或评级。 你应该做'错误处理'

        import requests
        import bs4
        
        website = requests.get("https://www.hltv.org/stats/teams")
        
        soup = bs4.BeautifulSoup(website.text, "html.parser")
        
        leaderboard = soup.find("table", {id: "stats-table player-ratings-table"})
        tbody = leaderboard.find("tbody")
        
        for tr in tbody.find.all('tr'):
            try:
                team = tr.find.all('td')[0].text.strip()
            except AttributError:
                team = ''
            
            try:
                maps = tr.find.all('td')[1].text.strip()
            except AttributError:
                maps = ''
        
            try:
                kd = tr.find.all('td')[3].text.strip()
            except AttributError:
                kd = ''
        
            try:
                rating = tr.find.all('td')[4].text.strip()
            except AttributError:
                rating = ''
        
            print(team, maps, kd, rating)
        

        【讨论】:

        • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
        • 你应该知道ErrorHandling方法。 !!!
        猜你喜欢
        • 1970-01-01
        • 2019-02-04
        • 1970-01-01
        • 1970-01-01
        • 2016-05-06
        • 2022-01-12
        • 1970-01-01
        • 2021-12-22
        • 1970-01-01
        相关资源
        最近更新 更多