Reputation: 516
My goal is to make a dictionary with the title of a content section and the link related to it (on a page of khan academy).
Here is my code:
from bs4 import BeautifulSoup
import re
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://www.khanacademy.org/computing/computer-programming/programming#intro-to-programming')
r.html.render(sleep=5)
soup=BeautifulSoup(r.html.html,'html.parser')
#find course steps links
courses_links = soup.find_all(class_='link_1uvuyao-o_O-nodeStyle_cu2reh-o_O-nodeStyleIcon_4udnki')
list_courses={}
#print(courses_links)
for links in courses_links:
courses = links.extract()
link_course = courses['href']
#print(link_course)
title_course= links.find(class_='nodeTitle_145jbuf')
#print(title_course)
span_title_course=title_course.span
#print(span_title_course)
text_span=span_title_course.text.strip()
#print(text_span)
final_link_course ='https://www.khanacademy.org'+link_course
#print(final_link_course)
list_courses[text_span]=final_link_course
print(list_courses) #showing weird things
I would like to get something like {title1 : link1, title2: link2...}
. But instead I get a multiple dictionaries (with multiple {}
).
Upvotes: 0
Views: 42
Reputation: 33384
I would like to use pandas which very good data analysis tool for python programming.
Just need to install pandas
using pip
import pandas as pd
Pass your dictionary like below.
df=pd.DataFrame.from_dict(list_courses,orient='index')
print(df)
It is just an another options.Please don't take it otherwise.
Upvotes: 1