立即学习:https://edu.csdn.net/course/play/25650/308706?utm_source=blogtoedu
import re with open('static/html/index.html','r',encoding='utf-8') as f: html = re.sub('\n','',f.read()) #print(html) section_pattern = '<section class="main_section">(.*?)</section>' section_s = re.findall(section_pattern,html) # print(section_s) # print(len(section_s)) category_pattern='<h1>(.*?)</h1>' course_pattern='<span class="course_name">(.*?)</span>' data_s=[] for section in section_s: category=re.findall(category_pattern,section)[0] course_s=re.findall(course_pattern,section) # print(category) # print(course_s) data_s.append( { 'category':category, 'course_s':course_s, } ) print(data_s) for data in data_s: print(data['category']) for course in course_s: print(' ',course)