I am currently having trouble with web scraping for one of my courses

def isenberg_ms(url):
    import requests
    from bs4 import BeautifulSoup

    course_titles = []
    isenberg_msba_curriculum = requests.get(url)

    if not isenberg_msba_curriculum.status_code == 200:
        print("The request was not successful.")
        return course_titles

    msba_curriculum = BeautifulSoup(isenberg_msba_curriculum.content, 'html.parser')

    all_courses = msba_curriculum.find_all('div', class_='course-info')
    for course in all_courses:
        course_titles.append(course.find('span', class_='course-title').get_text().strip())

    return course_titles

This code, is supposed to return the course titles for graduate courses available, for some reason I continue to not be able to complete this code could someone help.

Hi @JaredGordon4 , welcome to Replit Ask! Could you share a link to your repl?

Is this a school assignment?

Hey @JaredGordon4, welcome to the community!

By “courses” I’m assuming you mean a school assignment. Because of that, we can only give you hints!

First of all, please format your code using triple backticks. I’ll do it for you here:

Raw
```py
def isenberg_ms(url):
    import requests
    from bs4 import BeautifulSoup

    course_titles = []
    isenberg_msba_curriculum = requests.get(url)

    if not isenberg_msba_curriculum.status_code == 200:
        print("The request was not successful.")
        return course_titles

    msba_curriculum = BeautifulSoup(isenberg_msba_curriculum.content, 'html.parser')

    all_courses = msba_curriculum.find_all('div', class_='course-info')
    for course in all_courses:
        course_titles.append(course.find('span', class_='course-title').get_text().strip())

    return course_titles
```

Second, you are importing inside of a function, which is bad practice. Imports should always be at the top of the file.

2 Likes