简体   繁体   中英

I can't find table in beautifulsoup

So I'm really new to this stuff so this might be stupid. But I can't seem to figure out why this very basic line of code can't find any tables... Also trying to find a table to get each row basically. The url : https://uwflow.com/course/cs136

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = "https://uwflow.com/course/cs136"

# opening up connection, grabbing the page
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()

# html parser
page_soup = soup(page_html, "html.parser")

    enter code here

table = page_soup.findAll('table')
print(table)
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import pandas as pd

options = Options()
options.add_argument('--headless')

driver = webdriver.Firefox(options=options)
driver.get("https://uwflow.com/course/cs136")

df = pd.read_html(driver.page_source)[0]

df.to_csv('out.csv', index=False)

driver.quit()

Output: View-Online

I've taken Winter 2020 , In case if you want Spring 2020 so you need to change [0] to [1]

The data is stored inside the page inside <script> tag. If you want solution without selenium you can use re and json modules to parse the data out.

For example:

import re
import json
import requests

url = 'https://uwflow.com/course/cs136'

txt = requests.get(url).text

data = json.loads(re.findall(r'window.pageData.courseObj = (\{.*?});', txt)[0])

# print(json.dumps(data, indent=4)) # <-- uncomment this to see all data

print(data['code'] + ' - ' + data['name'])
print(data['description'])

print('{:<10} {:<10} {:<10}'.format('Class', 'Enrolled', 'Campus'))
for section in data['sections']:
    print('{:<10} {:<10} {:<10}'.format(section['class_num'],
        str(section['enrollment_total']) + '/' + str(section['enrollment_capacity']),
        section['campus']))

Prints:

CS 136 - Elementary Algorithm Design and Data Abstraction
This course builds on the techniques and patterns learned in CS 135 while making the transition to use an imperative language. It introduces the design and analysis of algorithms, the management of information, and the programming mechanisms and methodologies required in implementations. Topics discussed include iterative and recursive sorting algorithms; lists, stacks, queues, trees, and their application; abstract data types and their implementations.
Class      Enrolled   Campus    
6214       90/90      UW U      
6011       59/65      UW U      
5914       46/90      UW U      
6004       90/90      UW U      
6048       90/90      UW U      
6109       90/90      UW U      
6215       87/90      UW U      
6260       90/90      UW U      
6261       67/90      UW U      
6005       64/65      UW U      

... and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM