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|
+
from bs4 import BeautifulSoup
|
| 3 |
+
import csv
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def extract_course_info(html_content, url):
|
| 7 |
+
soup = BeautifulSoup(html_content, 'html.parser')
|
| 8 |
+
|
| 9 |
+
# Extract course name
|
| 10 |
+
course_name = soup.title.string if soup.title else "Course name not found"
|
| 11 |
+
|
| 12 |
+
# Extract key takeaways - Updated selector to match the provided HTML
|
| 13 |
+
key_takeaways = []
|
| 14 |
+
checklist_container = soup.find('div', class_='checklist__container')
|
| 15 |
+
if checklist_container:
|
| 16 |
+
takeaway_items = checklist_container.find_all('li', class_='checklist__list-item')
|
| 17 |
+
for item in takeaway_items:
|
| 18 |
+
p_tag = item.find('p')
|
| 19 |
+
if p_tag:
|
| 20 |
+
# Remove the icon and get clean text
|
| 21 |
+
takeaway_text = p_tag.text.replace('\uf00c', '').strip()
|
| 22 |
+
# Remove "fa fa-check" text if present
|
| 23 |
+
takeaway_text = takeaway_text.replace('fa fa-check', '').strip()
|
| 24 |
+
key_takeaways.append(takeaway_text)
|
| 25 |
+
|
| 26 |
+
# Extract course time, ratings, and difficulty level - FIXED PART
|
| 27 |
+
# Use safer method to handle NoneType and avoid errors
|
| 28 |
+
course_time = soup.find('li', class_='text-icon__list-item')
|
| 29 |
+
course_time_text = course_time.find('h4').text if course_time else "Course time not found"
|
| 30 |
+
|
| 31 |
+
ratings = course_time.find_next_sibling('li').find('h4').text if course_time and course_time.find_next_sibling('li') else "Ratings not found"
|
| 32 |
+
|
| 33 |
+
difficulty_sibling = course_time.find_next_sibling('li').find_next_sibling('li') if course_time and course_time.find_next_sibling('li') else None
|
| 34 |
+
difficulty = difficulty_sibling.find('h4').text if difficulty_sibling else "Difficulty level not found"
|
| 35 |
+
|
| 36 |
+
# Extract course description
|
| 37 |
+
description = "Description not found"
|
| 38 |
+
description_section = soup.find('div', class_='course-description')
|
| 39 |
+
if description_section:
|
| 40 |
+
first_p = description_section.find('p')
|
| 41 |
+
if first_p:
|
| 42 |
+
description = first_p.text.strip()
|
| 43 |
+
|
| 44 |
+
return {
|
| 45 |
+
"course_name": course_name,
|
| 46 |
+
"key_takeaways": ', '.join(key_takeaways) if key_takeaways else "No key takeaways found",
|
| 47 |
+
"course_time": course_time_text,
|
| 48 |
+
"ratings": ratings,
|
| 49 |
+
"difficulty": difficulty,
|
| 50 |
+
"description": description,
|
| 51 |
+
"website": url
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
def append_to_csv(course_info, csv_filename="course_data.csv"):
|
| 55 |
+
file_exists = os.path.isfile(csv_filename)
|
| 56 |
+
|
| 57 |
+
with open(csv_filename, mode='a', newline='', encoding='utf-8') as file:
|
| 58 |
+
writer = csv.writer(file)
|
| 59 |
+
|
| 60 |
+
if not file_exists:
|
| 61 |
+
writer.writerow(["Course Name", "Key Takeaways", "Course Time", "Ratings", "Difficulty", "Description", "Website"])
|
| 62 |
+
|
| 63 |
+
writer.writerow([
|
| 64 |
+
course_info["course_name"],
|
| 65 |
+
course_info["key_takeaways"],
|
| 66 |
+
course_info["course_time"],
|
| 67 |
+
course_info["ratings"],
|
| 68 |
+
course_info["difficulty"],
|
| 69 |
+
course_info["description"],
|
| 70 |
+
course_info["website"]
|
| 71 |
+
])
|
| 72 |
+
|
| 73 |
+
# Add headers to help avoid blocking
|
| 74 |
+
headers = {
|
| 75 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
# Updated list of URLs to process
|
| 79 |
+
urls = [
|
| 80 |
+
"https://courses.analyticsvidhya.com/courses/genai-applied-to-quantitative-finance-for-control-implementation",
|
| 81 |
+
"https://courses.analyticsvidhya.com/courses/navigating-llm-tradeoffs-techniques-for-speed-cost-scale-and-accuracy",
|
| 82 |
+
"https://courses.analyticsvidhya.com/courses/creating-problem-solving-agents-using-genai-for-action-composition",
|
| 83 |
+
"https://courses.analyticsvidhya.com/courses/improving-real-world-rag-systems-key-challenges",
|
| 84 |
+
"https://courses.analyticsvidhya.com/courses/choosing-the-right-LLM-for-your-business",
|
| 85 |
+
"https://courses.analyticsvidhya.com/courses/building-smarter-llms-with-mamba-and-state-space-model",
|
| 86 |
+
"https://courses.analyticsvidhya.com/courses/genai-a-way-of-life",
|
| 87 |
+
"https://courses.analyticsvidhya.com/courses/building-llm-applications-using-prompt-engineering-free",
|
| 88 |
+
"https://courses.analyticsvidhya.com/courses/building-your-first-computer-vision-model",
|
| 89 |
+
|
| 90 |
+
# New URLs
|
| 91 |
+
"https://courses.analyticsvidhya.com/courses/bagging-boosting-ML-Algorithms",
|
| 92 |
+
"https://courses.analyticsvidhya.com/courses/midjourney_from_inspiration_to_implementation",
|
| 93 |
+
"https://courses.analyticsvidhya.com/courses/free-understanding-linear-regression",
|
| 94 |
+
"https://courses.analyticsvidhya.com/courses/The%20Working%20of%20Neural%20Networks",
|
| 95 |
+
"https://courses.analyticsvidhya.com/courses/free-unsupervised-ml-guide",
|
| 96 |
+
"https://courses.analyticsvidhya.com/courses/building-first-rag-systems-using-llamaindex",
|
| 97 |
+
"https://courses.analyticsvidhya.com/courses/data-preprocessing",
|
| 98 |
+
"https://courses.analyticsvidhya.com/courses/exploring-stability-ai",
|
| 99 |
+
"https://courses.analyticsvidhya.com/courses/free-building-textclassification-natural-language-processing",
|
| 100 |
+
|
| 101 |
+
"https://courses.analyticsvidhya.com/courses/getting-started-with-llms",
|
| 102 |
+
"https://courses.analyticsvidhya.com/courses/introduction-to-generative-ai",
|
| 103 |
+
"https://courses.analyticsvidhya.com/courses/nano-course-dreambooth-stable-diffusion-for-custom-images",
|
| 104 |
+
"https://courses.analyticsvidhya.com/courses/a-comprehensive-learning-path-for-deep-learning-in-2023",
|
| 105 |
+
"https://courses.analyticsvidhya.com/courses/a-comprehensive-learning-path-to-become-a-data-scientist-in-twenty-twenty-four",
|
| 106 |
+
"https://courses.analyticsvidhya.com/courses/building-large-language-models-for-code",
|
| 107 |
+
"https://courses.analyticsvidhya.com//bundles/certified-ai-ml-blackbelt-plus",
|
| 108 |
+
"https://courses.analyticsvidhya.com/courses/machine-learning-summer-training",
|
| 109 |
+
"https://courses.analyticsvidhya.com/courses/ai-ethics-fractal",
|
| 110 |
+
|
| 111 |
+
"https://courses.analyticsvidhya.com/courses/a-comprehensive-learning-path-to-become-a-data-engineer-in-2022",
|
| 112 |
+
"https://courses.analyticsvidhya.com/bundles/certified-business-analytics-program",
|
| 113 |
+
"https://courses.analyticsvidhya.com/bundles/certified-machine-learning-master-s-program-mlmp",
|
| 114 |
+
"https://courses.analyticsvidhya.com/bundles/certified-natural-language-processing-master-s-program",
|
| 115 |
+
"https://courses.analyticsvidhya.com/bundles/certified-computer-vision-masters-program",
|
| 116 |
+
"https://courses.analyticsvidhya.com/courses/applied-machine-learning-beginner-to-professional",
|
| 117 |
+
"https://courses.analyticsvidhya.com/courses/ace-data-science-interviews",
|
| 118 |
+
"https://courses.analyticsvidhya.com/courses/writing-powerful-data-science-articles",
|
| 119 |
+
"https://courses.analyticsvidhya.com/courses/machine-learning-certification-course-for-beginners",
|
| 120 |
+
|
| 121 |
+
"https://courses.analyticsvidhya.com/courses/data-science-career-conclave",
|
| 122 |
+
"https://courses.analyticsvidhya.com/courses/top-data-science-projects-for-analysts-and-data-scientists",
|
| 123 |
+
"https://courses.analyticsvidhya.com/courses/getting-started-with-git-and-github-for-data-science-professionals",
|
| 124 |
+
"https://courses.analyticsvidhya.com/courses/machine-learning-starter-program",
|
| 125 |
+
"https://courses.analyticsvidhya.com/courses/data-science-hacks-tips-and-tricks",
|
| 126 |
+
"https://courses.analyticsvidhya.com/courses/introduction-to-analytics",
|
| 127 |
+
"https://courses.analyticsvidhya.com/courses/introduction-to-pytorch-for-deeplearning",
|
| 128 |
+
"https://courses.analyticsvidhya.com/courses/introductory-data-science-for-business-managers",
|
| 129 |
+
"https://courses.analyticsvidhya.com/courses/intro-to-nlp",
|
| 130 |
+
|
| 131 |
+
"https://courses.analyticsvidhya.com/courses/getting-started-with-decision-trees",
|
| 132 |
+
"https://courses.analyticsvidhya.com/courses/introduction-to-data-science",
|
| 133 |
+
"https://courses.analyticsvidhya.com/courses/loan-prediction-practice-problem-using-python",
|
| 134 |
+
"https://courses.analyticsvidhya.com/courses/big-mart-sales-prediction-using-r",
|
| 135 |
+
"https://courses.analyticsvidhya.com/courses/twitter-sentiment-analysis",
|
| 136 |
+
"https://courses.analyticsvidhya.com/courses/pandas-for-data-analysis-in-python",
|
| 137 |
+
"https://courses.analyticsvidhya.com/courses/support-vector-machine-svm-in-python-and-r",
|
| 138 |
+
"https://courses.analyticsvidhya.com/courses/evaluation-metrics-for-machine-learning-models"
|
| 139 |
+
]
|
| 140 |
+
|
| 141 |
+
# Process each URL
|
| 142 |
+
for url in urls:
|
| 143 |
+
try:
|
| 144 |
+
print(f"Processing {url}...")
|
| 145 |
+
response = requests.get(url, headers=headers)
|
| 146 |
+
response.raise_for_status() # Check for HTTP errors
|
| 147 |
+
|
| 148 |
+
html = response.content
|
| 149 |
+
course_info = extract_course_info(html, url)
|
| 150 |
+
append_to_csv(course_info)
|
| 151 |
+
|
| 152 |
+
print(f"Data for {url} has been successfully appended.")
|
| 153 |
+
except Exception as e:
|
| 154 |
+
print(f"Failed to process {url}: {e}")
|