{"id":108,"date":"2025-10-25T07:18:30","date_gmt":"2025-10-25T07:18:30","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=108"},"modified":"2025-12-17T07:49:58","modified_gmt":"2025-12-17T07:49:58","slug":"lesson-30-web-scraping-in-python","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=108","title":{"rendered":"Lesson 30: Web Scraping in Python"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To understand how to extract data from websites using Python and use it for <strong>data analysis, automation, and reporting<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 <strong>1. What Is Web Scraping?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Web scraping is the process of <strong>automatically extracting information from websites<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example Uses:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Collecting product prices from e-commerce sites<\/li>\n\n\n\n<li>Extracting news headlines or articles<\/li>\n\n\n\n<li>Monitoring stock prices or cryptocurrency rates<\/li>\n\n\n\n<li>Gathering weather data<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f <strong>2. Python Libraries for Web Scraping<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Library<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td><code>requests<\/code><\/td><td>Send HTTP requests to get website content<\/td><\/tr><tr><td><code>BeautifulSoup (bs4)<\/code><\/td><td>Parse HTML and XML content<\/td><\/tr><tr><td><code>lxml<\/code><\/td><td>Fast HTML and XML parser (optional)<\/td><\/tr><tr><td><code>pandas<\/code><\/td><td>Store and manipulate scraped data<\/td><\/tr><tr><td><code>selenium<\/code><\/td><td>Automate browser interaction for dynamic websites<\/td><\/tr><tr><td><code>re<\/code><\/td><td>Extract specific patterns using regular expressions<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 <strong>3. Making HTTP Requests<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before scraping, fetch the HTML content:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\n\nurl = \"https:\/\/example.com\"\nresponse = requests.get(url)\n\nprint(response.status_code)  # 200 means success\nprint(response.text&#91;:500])   # Print first 500 characters of HTML\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Tip:<\/strong> Always check the website\u2019s <strong>robots.txt<\/strong> and terms of service before scraping.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 <strong>4. Parsing HTML with BeautifulSoup<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>from bs4 import BeautifulSoup\n\nhtml_content = response.text\nsoup = BeautifulSoup(html_content, \"html.parser\")\n\n# Print pretty formatted HTML\nprint(soup.prettify())\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1 \u2014 Extracting Titles and Links<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>for link in soup.find_all(\"a\"):  # Find all &lt;a&gt; tags\n    print(link.get(\"href\"), \"-\", link.text)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2 \u2014 Extract Specific Elements<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>headlines = soup.find_all(\"h2\")  # Extract all headlines inside &lt;h2&gt; tags\nfor headline in headlines:\n    print(headline.text.strip())\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 <strong>5. Using CSS Selectors<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># Extract elements using class or id\narticles = soup.select(\".article-title\")  # Class\nfor article in articles:\n    print(article.get_text())\n\nmain_section = soup.select_one(\"#main-content\")  # ID\nprint(main_section.text)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 <strong>6. Handling Tables and Structured Data<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd\n\ntable = soup.find(\"table\", {\"id\": \"data-table\"})\nrows = table.find_all(\"tr\")\n\ndata = &#91;]\nfor row in rows:\n    cols = row.find_all(\"td\")\n    cols = &#91;col.text.strip() for col in cols]\n    data.append(cols)\n\ndf = pd.DataFrame(data, columns=&#91;\"Name\", \"Age\", \"City\"])\nprint(df)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Converts scraped HTML tables into a <strong>pandas DataFrame<\/strong> for analysis.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 <strong>7. Scraping JSON Data<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Some websites provide data via <strong>APIs in JSON format<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\n\nurl = \"https:\/\/api.example.com\/data\"\nresponse = requests.get(url)\ndata = response.json()\n\nfor item in data&#91;\"items\"]:\n    print(item&#91;\"name\"], \"-\", item&#91;\"price\"])\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Easier and cleaner than parsing HTML.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 <strong>8. Scraping Dynamic Websites Using Selenium<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dynamic sites load content via JavaScript:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from selenium import webdriver\nfrom selenium.webdriver.common.by import By\n\ndriver = webdriver.Chrome()  # Ensure ChromeDriver is installed\ndriver.get(\"https:\/\/example.com\")\n\ntitles = driver.find_elements(By.CLASS_NAME, \"product-title\")\nfor title in titles:\n    print(title.text)\n\ndriver.quit()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Can handle <strong>login pages, infinite scrolling, and button clicks<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 <strong>9. Example \u2014 Scraping Product Prices<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\nfrom bs4 import BeautifulSoup\n\nurl = \"https:\/\/books.toscrape.com\/\"\nresponse = requests.get(url)\nsoup = BeautifulSoup(response.text, \"html.parser\")\n\nbooks = soup.find_all(\"h3\")\nprices = soup.find_all(\"p\", class_=\"price_color\")\n\nfor book, price in zip(books, prices):\n    print(f\"{book.a&#91;'title']} - {price.text}\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Outputs <strong>book titles and prices<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 <strong>10. Exporting Scraped Data<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>df.to_csv(\"scraped_data.csv\", index=False)\ndf.to_excel(\"scraped_data.xlsx\", index=False)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Save data for <strong>further analysis or reporting<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 <strong>11. Best Practices<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Respect the website:<\/strong> Avoid overloading with requests. Use <code>time.sleep()<\/code><\/li>\n\n\n\n<li><strong>Check robots.txt:<\/strong> Ensure scraping is allowed<\/li>\n\n\n\n<li><strong>Use headers:<\/strong> Some sites block default Python requests<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>headers = {\"User-Agent\": \"Mozilla\/5.0\"}\nresponse = requests.get(url, headers=headers)\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Handle errors:<\/strong> Use <code>try-except<\/code> for failed requests<\/li>\n\n\n\n<li><strong>Paginate:<\/strong> Automate scraping across multiple pages<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To understand how to extract data from websites using Python and use it for data analysis, automation, and reporting. \ud83e\udde9 1. What Is Web Scraping? Web scraping is the process of automatically extracting information from websites. Example Uses: \u2699\ufe0f 2. Python Libraries for Web Scraping Library Purpose requests Send HTTP requests to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-108","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-outline","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=108"}],"version-history":[{"count":2,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/108\/revisions"}],"predecessor-version":[{"id":111,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/108\/revisions\/111"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}