Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
BeautifulSoup attribute problem
#1
In line 16 where it says "for tr in soup.find("tbody").children:", it keeps telling me that there is no such attribute. The code in the example video works just fine. Can someone please help?


import requests
from bs4 import BeautifulSoup
import bs4

def getHtmlText(url):
    try:
        r = requests.get(url)
        r.raise_for_status()
        r.encoding = r.appearent_encoding
        return r.text
    except:
        return ""

def fillUniversList(ulist, html):
    soup = BeautifulSoup(html, "html.parser")
    for tr in soup.find("tbody").children:
        if isinstance(tr, bs4.element.Tag):
            tds = tr("td")
            ulist.append([tds[1].children.string, tds[2].string, tds[3].string, tds[4].string])
               
def printUniversList(ulist, num):
    print("{:^10}\t{:^10}\t{:10}".format("rank", "name", "location"))
    for i in range(num):
        u = ulist[i]
        print("{:^10}\t{:^10}\t{:^10}".format(u[0], u[1], u[2]))
    print("Suc" + str(num))

def main():
    url = 'https://www.shanghairanking.cn/rankings/bcur/2020'
    uinfo = []
    html = getHtmlText(url)
    fillUniversList(uinfo, html)
    printUniversList(uinfo, 20)
main()
    
Reply


Messages In This Thread
BeautifulSoup attribute problem - by zzy - Dec-06-2020, 10:56 PM
RE: BeautifulSoup attribute problem - by zzy - Dec-07-2020, 11:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to extract style attribute with BeautifulSoup knight2000 1 3,188 Dec-28-2022, 03:06 AM
Last Post: knight2000
  Python 3.9 : BeautifulSoup: 'NoneType' object has no attribute 'text' fudgemasterultra 1 8,977 Mar-03-2021, 09:40 AM
Last Post: Larz60+
  form.populate_obj problem "object has no attribute translate" pascale 0 3,685 Jun-12-2019, 07:30 PM
Last Post: pascale
  BeautifulSoup 'NoneType' object has no attribute 'text' bmccollum 9 14,760 Sep-14-2018, 12:56 PM
Last Post: bmccollum

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020