<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bhavya Arora</title>
    <description>The latest articles on DEV Community by Bhavya Arora (@bhavya_arora).</description>
    <link>https://dev.to/bhavya_arora</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3961140%2Ffdef02d3-f925-4430-affa-eec5a12bc10f.png</url>
      <title>DEV Community: Bhavya Arora</title>
      <link>https://dev.to/bhavya_arora</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bhavya_arora"/>
    <language>en</language>
    <item>
      <title>Cleaning Scraped Data with pandas: A Practical Guide</title>
      <dc:creator>Bhavya Arora</dc:creator>
      <pubDate>Sun, 22 Nov 2026 08:14:47 +0000</pubDate>
      <link>https://dev.to/bhavya_arora/cleaning-scraped-data-with-pandas-a-practical-guide-1ode</link>
      <guid>https://dev.to/bhavya_arora/cleaning-scraped-data-with-pandas-a-practical-guide-1ode</guid>
      <description>&lt;p&gt;Your scraper finished and you have a CSV. Then you open it. Prices are text like &lt;code&gt;"$1,299.00"&lt;/code&gt;, the same product appears twice, dates arrive in five formats, and one category is spelled three ways. Scraped data is rarely ready to use, and the cleaning step decides whether your charts and averages can be trusted.&lt;/p&gt;

&lt;p&gt;This guide builds a small, rerunnable &lt;code&gt;clean.py&lt;/code&gt; that turns a messy scrape into a tidy table. Along the way you'll see the mistakes that fail &lt;em&gt;silently&lt;/em&gt;, such as a price that quietly becomes &lt;code&gt;NaN&lt;/code&gt;, because those are the ones that hurt.&lt;/p&gt;

&lt;p&gt;One rule first: &lt;strong&gt;never overwrite your raw file.&lt;/strong&gt; Read the raw CSV, write a new clean one, and rerun the script whenever you find a bug.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Version note:&lt;/strong&gt; tested with Python 3.13 and pandas 3.1.1. The same code also ran unchanged on pandas 2.3.3 and produced the same cleaned table. In pandas 3, text columns show as &lt;code&gt;str&lt;/code&gt; instead of &lt;code&gt;object&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  What Goes Wrong
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;Example from a scrape&lt;/th&gt;
&lt;th&gt;Fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Hidden characters and stray spaces&lt;/td&gt;
&lt;td&gt;"  Wireless Mouse\xa0M300 "&lt;/td&gt;
&lt;td&gt;str.replace, str.strip&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Placeholder text&lt;/td&gt;
&lt;td&gt;"N/A", "-", "No reviews yet"&lt;/td&gt;
&lt;td&gt;Convert to NaN&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Duplicates&lt;/td&gt;
&lt;td&gt;The same product on two pages, or with ?ref=home&lt;/td&gt;
&lt;td&gt;Build a stable key, then drop_duplicates&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Numbers stored as text&lt;/td&gt;
&lt;td&gt;"$1,299.00", "4.5 out of 5"&lt;/td&gt;
&lt;td&gt;Extract, then to_numeric&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mixed date formats&lt;/td&gt;
&lt;td&gt;"03/11/2026", "3 days ago"&lt;/td&gt;
&lt;td&gt;Explicit formats&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Inconsistent labels&lt;/td&gt;
&lt;td&gt;"ELECTRONICS", "electronics "&lt;/td&gt;
&lt;td&gt;Normalize case&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  The Messy Data
&lt;/h2&gt;

&lt;p&gt;To follow along, run &lt;code&gt;pip install pandas&lt;/code&gt;, then save this as &lt;code&gt;make_raw.py&lt;/code&gt; and run it. It writes 12 rows of a made-up shop's scrape, with every problem from the table built in. The invisible characters are written as escapes so you can see them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# make_raw.py&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="no"&gt;NBSP&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"\u00a0"&lt;/span&gt;   &lt;span class="c1"&gt;# non-breaking space&lt;/span&gt;
&lt;span class="no"&gt;ZWSP&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"\u200b"&lt;/span&gt;   &lt;span class="c1"&gt;# zero-width space (invisible)&lt;/span&gt;
&lt;span class="no"&gt;SHOP&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"https://shop.example/p/"&lt;/span&gt;

&lt;span class="n"&gt;columns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"rating"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"stock"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"date_listed"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"category"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"scraped_at"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"  Wireless Mouse"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;NBSP&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"M300 "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$24.99"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"4.5 out of 5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"In stock (12 available)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-03"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Electronics"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SHOP&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"1001"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-20T09:00:00"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Mechanical Keyboard K8\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$89.00"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"4/5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Only 3 left"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"03/11/2026"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"electronics "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SHOP&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"1002?ref=home"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-20T09:00:00"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'4K Monitor 32"'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$1,299.00"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"4.8 out of 5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"In stock"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Nov 3, 2026"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"ELECTRONICS"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SHOP&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"1003/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-20T09:00:00"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Cast Iron Skillet"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$ 34.50"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"No reviews yet"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Out of stock"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"3 days ago"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Home &amp;amp; Kitchen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SHOP&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"2001"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-20T09:00:00"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Glass Water Bottle"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;ZWSP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Free"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"4.1 out of 5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"In stock (240 available)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"yesterday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"home &amp;amp; kitchen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SHOP&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"2002"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-20T09:00:00"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Notebook Set (3-pack)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"N/A"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"N/A"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"-"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SHOP&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"3001"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-20T09:00:00"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Desk Lamp"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$19.99"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"3.9 out of 5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"In stock (7 available)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"25/10/2026"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Home &amp;amp; Kitchen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SHOP&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"2003"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-20T09:00:00"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Standing Desk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$449.00"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"4.7 out of 5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"In stock (5 available)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2026-10-28"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Furniture"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SHOP&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"4001"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-20T09:00:00"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Ergonomic Chair"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"from $199.00"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"4.2 out of 5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"In stock (9 available)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"1 week ago"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Furniture"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SHOP&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"4002"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-20T09:00:00"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SHOP&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"9999"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-20T09:00:00"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;# the same product scraped twice because two result pages overlapped&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Glass Water Bottle"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;ZWSP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Free"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"4.1 out of 5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"In stock (240 available)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"yesterday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"home &amp;amp; kitchen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SHOP&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"2002"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-20T09:00:00"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;# the mouse again, from a later pass: new tracking parameter, new price&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Wireless Mouse M300"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$22.99"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"4.5 out of 5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"In stock (11 available)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-03"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Electronics"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SHOP&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"1001?ref=search"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-20T09:30:00"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"raw_products.csv"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"wrote raw_products.csv with"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"rows"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 1: Load as Text and Look First
&lt;/h2&gt;

&lt;p&gt;Don't let pandas guess types. Read everything as strings and decide yourself what each column means:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# explore.py&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"raw_products.csv"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dtype&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;keep_default_na&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"category"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;value_counts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;head&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;repr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"identical rows:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;duplicated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;category
Electronics       2
Home &amp;amp; Kitchen    2
home &amp;amp; kitchen    2
Furniture         2
electronics       1
ELECTRONICS       1
-                 1
                  1
Name: count, dtype: int64
0    '  Wireless Mouse\xa0M300 '
1     'Mechanical Keyboard K8\n'
2               '4K Monitor 32"'
3            'Cast Iron Skillet'
4     'Glass Water Bottle\u200b'
identical rows: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two arguments matter here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;dtype=str&lt;/code&gt; stops pandas from converting columns on its own. A SKU like &lt;code&gt;00123&lt;/code&gt; would otherwise become the number &lt;code&gt;123&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;keep_default_na=False&lt;/code&gt; keeps empty cells and &lt;code&gt;"N/A"&lt;/code&gt; as visible text. pandas' built-in list of missing markers covers &lt;code&gt;"N/A"&lt;/code&gt; but not &lt;code&gt;"-"&lt;/code&gt; or &lt;code&gt;"No reviews yet"&lt;/code&gt;, and you want to choose what counts as missing.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice the habit in the second print: &lt;code&gt;.map(repr)&lt;/code&gt; &lt;strong&gt;shows hidden characters.&lt;/strong&gt; &lt;code&gt;\xa0&lt;/code&gt; is a non-breaking space that passes for an ordinary one, and &lt;code&gt;\u200b&lt;/code&gt; is a zero-width space that shows as nothing at all. Both break matching.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Tidy Text and Placeholders
&lt;/h2&gt;

&lt;p&gt;Start &lt;code&gt;clean.py&lt;/code&gt; with a loader and a text cleaner:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# clean.py&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="c1"&gt;# Placeholders this (imaginary) site uses for "no value". Adjust per site.&lt;/span&gt;
&lt;span class="no"&gt;MISSING&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"N/A"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"n/a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"NA"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"-"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"--"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"null"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"None"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"No reviews yet"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Read everything as text so pandas can't guess a type for you."""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dtype&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;keep_default_na&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;tidy_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Strip invisible junk, collapse whitespace, turn placeholders into NaN."""&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;col&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;col&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"[\u200b\ufeff]"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;regex&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# zero-width characters&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"\s+"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;regex&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="c1"&gt;# newlines, tabs, nbsp, runs of spaces&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;col&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;MISSING&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;             &lt;span class="c1"&gt;# placeholders become NaN&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The regex &lt;code&gt;\s+&lt;/code&gt; covers newlines, tabs and non-breaking spaces, but &lt;strong&gt;not&lt;/strong&gt; zero-width spaces or byte-order marks, which is why they get their own line. Whitespace is cleaned &lt;em&gt;before&lt;/em&gt; placeholders are checked, so &lt;code&gt;"N/A "&lt;/code&gt; with a trailing space still matches &lt;code&gt;"N/A"&lt;/code&gt;. We use &lt;code&gt;mask&lt;/code&gt; instead of &lt;code&gt;df.replace(MISSING, np.nan)&lt;/code&gt; because &lt;code&gt;replace&lt;/code&gt; raises a downcasting &lt;code&gt;FutureWarning&lt;/code&gt; on pandas 2.3.3 when a whole column is placeholders.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Remove Duplicates
&lt;/h2&gt;

&lt;p&gt;Scrapes produce two kinds of duplicates: identical rows (overlapping result pages) and the &lt;em&gt;same item&lt;/em&gt; with different details (a later pass with a new price or a tracking parameter in the URL). &lt;code&gt;drop_duplicates()&lt;/code&gt; alone only catches the first kind.&lt;/p&gt;

&lt;p&gt;For the second, build a key from the most stable identifier you have. Product URLs work well, but &lt;strong&gt;do not remove every query parameter&lt;/strong&gt;: some sites identify products using values such as &lt;code&gt;?id=1001&lt;/code&gt;. Drop only parameters you have confirmed are tracking parameters, and preserve path case because some servers distinguish uppercase from lowercase:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# clean.py (continued)&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;urllib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parse&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;parse_qsl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;urlencode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;urlsplit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;urlunsplit&lt;/span&gt;

&lt;span class="no"&gt;TRACKING_PARAMS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"ref"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;  &lt;span class="c1"&gt;# confirmed tracking parameter on this example site&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;canonical_url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Remove known tracking parameters, fragments and trailing slashes."""&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;clean_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isna&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;NA&lt;/span&gt;
        &lt;span class="n"&gt;parts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;urlsplit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;urlencode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;parse_qsl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;keep_blank_values&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;TRACKING_PARAMS&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;urlunsplit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scheme&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;netloc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rstrip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;clean_one&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;drop_duplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dropna&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;# empty records from failed parses&lt;/span&gt;
    &lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;drop_duplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="c1"&gt;# identical rows (overlapping pages)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;canonical_url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort_values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"scraped_at"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"stable"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;drop_duplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"url_key"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;keep&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"last"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# newest scrape wins&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The order matters. Tidying comes first, so two rows that differed only by a stray space now count as identical. This site's &lt;code&gt;ref&lt;/code&gt; parameter is tracking-only; adjust &lt;code&gt;TRACKING_PARAMS&lt;/code&gt; for your source rather than applying the same rule to every website.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Turn Prices into Numbers
&lt;/h2&gt;

&lt;p&gt;The obvious one-liner looks right and isn't:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# pitfall_price.py&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"raw_products.csv"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dtype&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;keep_default_na&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;naive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_numeric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"$"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"coerce"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loc&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;naive&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isna&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tolist&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;['$1,299.00', 'Free', 'N/A', 'from $199.00', '', 'Free']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;errors="coerce"&lt;/code&gt; turns anything it can't read into &lt;code&gt;NaN&lt;/code&gt; without a word. Two of those six are genuinely missing. The other four are real information, and the loss that hurts most is the &lt;strong&gt;$1,299.00 monitor, your most expensive product, vanishing because of one comma.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A safer version validates that each value contains &lt;strong&gt;one nonnegative amount&lt;/strong&gt; before removing thousands separators. It also treats &lt;code&gt;"Free"&lt;/code&gt; as a real zero:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# clean.py (continued)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;parse_amount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""'$1,299.00' -&amp;gt; 1299.0. Pass decimal=',' for '1.299,00 €'."""&lt;/span&gt;
    &lt;span class="n"&gt;patterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"."&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;","&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"(?:\d{1,3}(?:\.\d{3})+|\d+)(?:,\d+)?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;patterns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"decimal must be '.' or ','"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;thousands&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;","&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"."&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s2"&gt;"."&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"(?i)^from\s+"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;regex&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;rf&lt;/span&gt;&lt;span class="s2"&gt;"^(?:[$€£₹]\s*)?({patterns[decimal]})(?:\s*[$€£₹])?$"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;expand&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thousands&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;regex&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;regex&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_numeric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"coerce"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;clean_price&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parse_amount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"free"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;# "Free" is a real zero&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The full-match check rejects unsupported values rather than turning &lt;code&gt;-$25.00&lt;/code&gt; into &lt;code&gt;25.00&lt;/code&gt; or &lt;code&gt;$20-$30&lt;/code&gt; into &lt;code&gt;2030.00&lt;/code&gt;. Step 8 reports those rejected values so you can decide how to handle them.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;decimal&lt;/code&gt; argument exists because number formats differ by country, and a string like &lt;code&gt;"1.299"&lt;/code&gt; is ambiguous. &lt;strong&gt;Choose the format once per source, from knowledge of the site, never per value.&lt;/strong&gt; A mismatched format can make a valid price unparseable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# locale_demo.py&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;clean&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;parse_amount&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;parse_amount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"1.299,00 €"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"€ 24,99"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;","&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tolist&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;parse_amount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"1,299.00"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;","&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tolist&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# wrong locale&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1299.0, 24.99]
[nan]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;"from $199.00"&lt;/code&gt; is a starting price, not &lt;em&gt;the&lt;/em&gt; price, so Step 9 records it in a &lt;code&gt;price_is_from&lt;/code&gt; flag you can filter on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Ratings, Stock and Categories
&lt;/h2&gt;

&lt;p&gt;Text that holds a number needs &lt;code&gt;str.extract&lt;/code&gt;. With &lt;code&gt;expand=False&lt;/code&gt; and a single capture group it returns a plain Series:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# clean.py (continued)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;clean_rating&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_numeric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"(\d+(?:\.\d+)?)\s*(?:out of|/)\s*5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expand&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;clean_stock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;qty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_numeric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"(\d+)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expand&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;qty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"out of stock"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;na&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;astype&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Int64"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                            &lt;span class="c1"&gt;# whole numbers, with gaps allowed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For stock, &lt;code&gt;"Out of stock"&lt;/code&gt; is a real &lt;code&gt;0&lt;/code&gt;, while a bare &lt;code&gt;"In stock"&lt;/code&gt; stays missing because you were never told how many. The capital-I &lt;code&gt;Int64&lt;/code&gt; is pandas' nullable integer, which can hold a gap; a plain &lt;code&gt;int64&lt;/code&gt; can't, and a float column would show &lt;code&gt;3.0&lt;/code&gt; instead of &lt;code&gt;3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Categories need no function of their own, because tidy text plus &lt;code&gt;str.title()&lt;/code&gt; turns &lt;code&gt;"ELECTRONICS"&lt;/code&gt;, &lt;code&gt;"electronics "&lt;/code&gt; and &lt;code&gt;"Electronics"&lt;/code&gt; into one label.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Dates in Many Formats
&lt;/h2&gt;

&lt;p&gt;Our dates arrive as &lt;code&gt;2026-11-03&lt;/code&gt;, &lt;code&gt;03/11/2026&lt;/code&gt;, &lt;code&gt;Nov 3, 2026&lt;/code&gt;, &lt;code&gt;3 days ago&lt;/code&gt; and &lt;code&gt;yesterday&lt;/code&gt;. The first attempt fails loudly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# pitfall_dates.py&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"raw_products.csv"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dtype&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;keep_default_na&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_datetime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"date_listed"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splitlines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;time data "03/11/2026" doesn't match format "%Y-%m-%d". You might want to try:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;pandas fixes the format from the first value, then rejects the rest. The error message suggests &lt;code&gt;format="mixed"&lt;/code&gt;, but that guesses, and it guesses &lt;strong&gt;month first&lt;/strong&gt;: &lt;code&gt;03/11/2026&lt;/code&gt; becomes 11 March. &lt;code&gt;dayfirst=True&lt;/code&gt; isn't a safe cure either: in my test on pandas 3.1.1, &lt;code&gt;format="mixed", dayfirst=True&lt;/code&gt; read the ISO date &lt;code&gt;2026-11-03&lt;/code&gt; as 11 March, which pandas 2.3.3 did not do.&lt;/p&gt;

&lt;p&gt;The reliable approach has three parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Find the evidence for the order.&lt;/strong&gt; &lt;code&gt;25/10/2026&lt;/code&gt; in the data can't be month-first, so this site writes day first.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;List explicit formats and try each in turn,&lt;/strong&gt; with &lt;code&gt;errors="coerce"&lt;/code&gt; so a miss becomes &lt;code&gt;NaT&lt;/code&gt; instead of an exception.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Anchor relative dates to the scrape time, not today.&lt;/strong&gt; &lt;code&gt;"3 days ago"&lt;/code&gt; written on 20 November means 17 November forever. If you compute it a week later with &lt;code&gt;today()&lt;/code&gt;, you're a week off.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# clean.py (continued)&lt;/span&gt;
&lt;span class="no"&gt;DATE_FORMATS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"%Y-%m-%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"%d/%m/%Y"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"%b %d, %Y"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="no"&gt;UNIT_DAYS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"hour"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"day"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"week"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;relative_date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scraped_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""'3 days ago' -&amp;gt; a date, counted back from when the page was scraped."""&lt;/span&gt;
    &lt;span class="n"&gt;low&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;parts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;low&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"^(\d+) (hour|day|week)s? ago$"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;days&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_numeric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;UNIT_DAYS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;days&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;days&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;low&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"yesterday"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;low&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"today"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"just now"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scraped_at&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_timedelta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;days&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"D"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;clean_dates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scraped_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_datetime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;DATE_FORMATS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"coerce"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;DATE_FORMATS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillna&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_datetime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"coerce"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillna&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;relative_date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scraped_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On a default English setup, &lt;code&gt;%b&lt;/code&gt; only understands English month abbreviations like &lt;code&gt;Nov&lt;/code&gt;, so translate other languages' month names first. Phrases the code doesn't know, such as &lt;code&gt;"an hour ago"&lt;/code&gt; or &lt;code&gt;"3. Nov 2026"&lt;/code&gt;, come out as &lt;code&gt;NaT&lt;/code&gt;, and Step 8 reports them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7: Decide What "Missing" Means
&lt;/h2&gt;

&lt;p&gt;Missing data isn't one thing:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;th&gt;What we do&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;"Free"&lt;/td&gt;
&lt;td&gt;A real price of zero&lt;/td&gt;
&lt;td&gt;0.0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;"N/A" price&lt;/td&gt;
&lt;td&gt;Unknown&lt;/td&gt;
&lt;td&gt;Keep NaN&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bare "In stock"&lt;/td&gt;
&lt;td&gt;Quantity unknown&lt;/td&gt;
&lt;td&gt;Keep missing, don't guess 0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;"No reviews yet"&lt;/td&gt;
&lt;td&gt;No rating exists&lt;/td&gt;
&lt;td&gt;Keep NaN, not 0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Blank category&lt;/td&gt;
&lt;td&gt;A label is missing&lt;/td&gt;
&lt;td&gt;Fill with "Unknown"&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Fill labels, not measurements. Replacing a missing price with the average, or a missing rating with &lt;code&gt;0&lt;/code&gt;, invents data. Keep the gap, and remove those rows only at analysis time, for example with &lt;code&gt;df.dropna(subset=["price"])&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;One pandas 3 trap while you fill: &lt;code&gt;df["category"].fillna("Unknown", inplace=True)&lt;/code&gt; &lt;strong&gt;no longer changes your data.&lt;/strong&gt; In my test pandas 3.1.1 raised a &lt;code&gt;ChainedAssignmentError&lt;/code&gt; warning and left the column untouched, where pandas 2.3.3 still applied the change with a &lt;code&gt;FutureWarning&lt;/code&gt;. Assign the result back instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"category"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"category"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillna&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Unknown"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 8: Report What Failed, Then Validate
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;errors="coerce"&lt;/code&gt; is only safe if you check what it swallowed. This helper lists values that had text but ended up empty:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# clean.py (continued)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;report_unparsed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parsed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Print values that had text but still ended up as NaN/NaT."""&lt;/span&gt;
    &lt;span class="n"&gt;bad&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;notna&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;parsed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isna&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bad&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"WARNING {label}: {len(bad)} value(s) not parsed: {bad.unique().tolist()}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the site adds a price range one day, you hear about it instead of losing it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# try_unparsed.py&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;clean&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;clean_price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;report_unparsed&lt;/span&gt;

&lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"$24.99"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$1,299.00 - $1,499.00"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;report_unparsed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;clean_price&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WARNING price: 1 value(s) not parsed: ['$1,299.00 - $1,499.00']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, check for values that parsed but are impossible. Treat these four rules as a starting point:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# clean.py (continued)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;problems&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;duplicated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;problems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"duplicate products"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;problems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"negative price"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"rating"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dropna&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;between&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;problems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"rating outside 0-5"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"date_listed"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"scraped_at"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;problems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"listing date after scrape time"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;problems&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"; "&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;problems&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A rating of 7.5 plus a listing date after the scrape time would raise &lt;code&gt;ValueError: rating outside 0-5; listing date after scrape time&lt;/code&gt;, with every problem named at once. Comparisons against &lt;code&gt;NaN&lt;/code&gt; come out &lt;code&gt;False&lt;/code&gt;, so missing values don't trigger these checks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 9: Put It Together and Save
&lt;/h2&gt;

&lt;p&gt;One function builds the clean table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# clean.py (continued)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;clean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;tidy_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"scraped_at"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_datetime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"scraped_at"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;drop_duplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;clean_price&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"price_is_from"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"(?i)^from\b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;na&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"rating"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;clean_rating&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"rating"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"stock_qty"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;clean_stock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"stock"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"date_listed"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;clean_dates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"date_listed"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"scraped_at"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"category"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"category"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillna&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Unknown"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"url_key"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"scraped_at"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"scraped_at"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;report_unparsed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;report_unparsed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"rating"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"rating"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"rating"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;report_unparsed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"date_listed"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"date_listed"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"date_listed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reset_index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;drop&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"__main__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;raw_rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"raw_products.csv"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;clean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"raw_products.csv"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{raw_rows} rows read, {len(df)} kept\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;drop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"scraped_at"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"clean_products.csv"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;python make_raw.py&lt;/code&gt; and then &lt;code&gt;python clean.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;12 rows read, 9 kept

                     name    price  price_is_from  rating  stock_qty date_listed        category
0  Mechanical Keyboard K8    89.00          False     4.0          3  2026-11-03     Electronics
1          4K Monitor 32"  1299.00          False     4.8       &amp;lt;NA&amp;gt;  2026-11-03     Electronics
2       Cast Iron Skillet    34.50          False     NaN          0  2026-11-17  Home &amp;amp; Kitchen
3      Glass Water Bottle     0.00          False     4.1        240  2026-11-19  Home &amp;amp; Kitchen
4   Notebook Set (3-pack)      NaN          False     NaN       &amp;lt;NA&amp;gt;         NaT         Unknown
5               Desk Lamp    19.99          False     3.9          7  2026-10-25  Home &amp;amp; Kitchen
6           Standing Desk   449.00          False     4.7          5  2026-10-28       Furniture
7         Ergonomic Chair   199.00           True     4.2          9  2026-11-13       Furniture
8     Wireless Mouse M300    22.99          False     4.5         11  2026-11-03     Electronics
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Twelve rows became nine: the empty record, the identical duplicate and the older mouse scrape are gone, and the mouse kept its newer price of 22.99. Every gap is now an honest &lt;code&gt;NaN&lt;/code&gt;, &lt;code&gt;NaT&lt;/code&gt; or &lt;code&gt;&amp;lt;NA&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A CSV forgets types.&lt;/strong&gt; Read &lt;code&gt;clean_products.csv&lt;/code&gt; back with plain &lt;code&gt;read_csv&lt;/code&gt; and your dates return as text and &lt;code&gt;stock_qty&lt;/code&gt; as floats. Restore them when you load:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# reload.py&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;"clean_products.csv"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;parse_dates&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"date_listed"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"scraped_at"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;dtype&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"stock_qty"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Int64"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you install &lt;code&gt;pyarrow&lt;/code&gt;, &lt;code&gt;df.to_parquet("clean_products.parquet")&lt;/code&gt; and &lt;code&gt;pd.read_parquet(...)&lt;/code&gt; keep these types without any extra arguments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Troubleshooting
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Symptom&lt;/th&gt;
&lt;th&gt;Likely cause&lt;/th&gt;
&lt;th&gt;What to try&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ValueError: time data ... doesn't match format&lt;/td&gt;
&lt;td&gt;Mixed formats; pandas locked onto the first one&lt;/td&gt;
&lt;td&gt;Parse with explicit formats and errors="coerce"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dates wrong by a month or a day&lt;/td&gt;
&lt;td&gt;format="mixed" guesses month first, and dayfirst can flip ISO dates&lt;/td&gt;
&lt;td&gt;Use explicit formats&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Prices of 1,000 or more become NaN&lt;/td&gt;
&lt;td&gt;The thousands separator breaks to_numeric&lt;/td&gt;
&lt;td&gt;Remove separators first (parse_amount)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;fillna(inplace=True) does nothing&lt;/td&gt;
&lt;td&gt;pandas 3 no longer modifies a column that way&lt;/td&gt;
&lt;td&gt;Reassign: df[col] = df[col].fillna(...)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Integer column shows 3.0&lt;/td&gt;
&lt;td&gt;A gap forced it to float&lt;/td&gt;
&lt;td&gt;astype("Int64")&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IDs lose their leading zeros&lt;/td&gt;
&lt;td&gt;read_csv guessed integers&lt;/td&gt;
&lt;td&gt;Read with dtype=str&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Same product still appears twice&lt;/td&gt;
&lt;td&gt;Names or prices differ between scrapes&lt;/td&gt;
&lt;td&gt;Deduplicate on a stable key such as a URL path&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A regex can't find text that looks present&lt;/td&gt;
&lt;td&gt;Hidden characters&lt;/td&gt;
&lt;td&gt;Print with .map(repr)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Where to Go from Here
&lt;/h2&gt;

&lt;p&gt;You now have a script that reads raw scrapes, cleans them predictably and refuses to continue when something looks wrong. To build on it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Load the clean table into SQLite or PostgreSQL instead of a CSV.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the whole pipeline on a schedule and keep each raw file, so a bug fix can be rerun over old data.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy scraping, and happy cleaning!&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Found this helpful? Share it with a fellow developer or leave your questions in the comments.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Published via &lt;a href="https://zyvop.com/cleaning-scraped-data-with-pandas-a-practical-guide-h5fpi?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=syndication" rel="noopener noreferrer"&gt;ZyVOP&lt;/a&gt; — Write once in Markdown, auto-backup to GitHub, and syndicate to Dev.to, Medium &amp;amp; Hashnode in 1 click.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>csv</category>
      <category>datacleaning</category>
      <category>pandas</category>
      <category>python</category>
    </item>
    <item>
      <title>Terraform vs OpenTofu in 2026: A Safe Migration Walkthrough</title>
      <dc:creator>Bhavya Arora</dc:creator>
      <pubDate>Fri, 20 Nov 2026 05:37:06 +0000</pubDate>
      <link>https://dev.to/bhavya_arora/terraform-vs-opentofu-in-2026-a-safe-migration-walkthrough-3b5n</link>
      <guid>https://dev.to/bhavya_arora/terraform-vs-opentofu-in-2026-a-safe-migration-walkthrough-3b5n</guid>
      <description>&lt;p&gt;Your Terraform repo has worked fine for years. Then someone on the team asks, "Should we be on OpenTofu?" and nobody has a good answer.&lt;/p&gt;

&lt;p&gt;The honest answer is "maybe, and the move is usually easier than people fear." But there are a few places where it can bite you, and several of them involve &lt;strong&gt;state&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this post you'll see how the two tools differ today, how to migrate one project step by step (with a demo that runs on your laptop and needs no cloud account), how to handle state and its encryption, and which gotchas trip people up most often.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For most projects, migrating means swapping the &lt;code&gt;terraform&lt;/code&gt; binary for &lt;code&gt;tofu&lt;/code&gt; and checking that &lt;code&gt;tofu plan&lt;/code&gt; shows &lt;strong&gt;no changes&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;OpenTofu can read Terraform state, but Terraform may not reliably read state written by OpenTofu. Treat the switch as a &lt;strong&gt;one-way door&lt;/strong&gt; and back up first.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The biggest technical difference today is &lt;strong&gt;built-in state encryption&lt;/strong&gt;, which OpenTofu has and Terraform doesn't.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Migrate first, adopt OpenTofu-only features &lt;strong&gt;later&lt;/strong&gt;. Changing two things at once makes failures hard to diagnose.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The usual traps are version constraints, the provider registry and lock file, &lt;code&gt;terraform_remote_state&lt;/code&gt; ordering, HCP Terraform, and CI tooling that still calls &lt;code&gt;terraform&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How we got here
&lt;/h2&gt;

&lt;p&gt;A short timeline, so the rest makes sense:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;August 2023:&lt;/strong&gt; HashiCorp announces that it is moving Terraform to the Business Source License (BUSL 1.1), which is source-available but not open source. A community fork, first called OpenTF, follows. It is later renamed OpenTofu and moved under the Linux Foundation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;September 2023:&lt;/strong&gt; Terraform 1.5.7 is the last release under the open-source MPL 2.0 license. From 1.6.0 onward, Terraform ships under the BUSL.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;April 2024:&lt;/strong&gt; OpenTofu 1.7 ships with client-side state encryption.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;February 2025:&lt;/strong&gt; IBM completes its acquisition of HashiCorp, so IBM is now the licensor of Terraform.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;September 2026:&lt;/strong&gt; OpenTofu 1.13.0 is released. Its security support runs until August 2027.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Does the license matter to you? It depends. The BUSL mainly restricts people who build products that compete with HashiCorp's offerings. If you only use Terraform to manage your own infrastructure, you probably aren't affected today. It matters more if you're a vendor or consultancy embedding the tool, if your company requires open-source licenses, or if you simply don't want to depend on one company's future licensing decisions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Terraform vs OpenTofu at a glance
&lt;/h2&gt;

&lt;p&gt;The core workflow is the same: write HCL, run &lt;code&gt;init&lt;/code&gt;, &lt;code&gt;plan&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt;, keep state somewhere. Here's where they differ.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Terraform&lt;/th&gt;
&lt;th&gt;OpenTofu&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;License&lt;/td&gt;
&lt;td&gt;BUSL 1.1 (since 1.6.0)&lt;/td&gt;
&lt;td&gt;MPL 2.0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Steward&lt;/td&gt;
&lt;td&gt;HashiCorp, now part of IBM&lt;/td&gt;
&lt;td&gt;Linux Foundation project, community governed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Binary&lt;/td&gt;
&lt;td&gt;terraform&lt;/td&gt;
&lt;td&gt;tofu&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Providers&lt;/td&gt;
&lt;td&gt;Terraform Registry&lt;/td&gt;
&lt;td&gt;Same providers, separate registry (registry.opentofu.org)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;State encryption&lt;/td&gt;
&lt;td&gt;Not built in; you rely on backend encryption or external tools&lt;/td&gt;
&lt;td&gt;Built in since 1.7, for state and plan files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;for_each on provider configs&lt;/td&gt;
&lt;td&gt;Not available in regular configurations (only in Stacks)&lt;/td&gt;
&lt;td&gt;Since 1.9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ephemeral values and write-only attributes&lt;/td&gt;
&lt;td&gt;Available&lt;/td&gt;
&lt;td&gt;Available since 1.11&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;enabled meta-argument (alternative to count = x ? 1 : 0)&lt;/td&gt;
&lt;td&gt;Not available&lt;/td&gt;
&lt;td&gt;Since 1.11&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HCP Terraform and Stacks&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Not part of OpenTofu&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Heads up:&lt;/strong&gt; The two projects keep diverging. OpenTofu doesn't promise to track every Terraform release, and Terraform doesn't track OpenTofu. A config that works in both tools today can stop doing so once you use a feature that only one of them has.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Should you move?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Staying on Terraform makes sense if:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You rely on HCP Terraform features, enterprise agreements or Stacks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Your team's training and documentation are built around Terraform and nothing is pushing you to change.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You don't need anything OpenTofu offers and don't mind the BUSL.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Moving to OpenTofu makes sense if:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You need an open-source license or vendor-neutral governance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You want state encryption without extra tooling.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You want features such as provider &lt;code&gt;for_each&lt;/code&gt; or the &lt;code&gt;enabled&lt;/code&gt; meta-argument.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both are reasonable. The rest of this post assumes you've decided to try the move.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pre-flight checklist
&lt;/h2&gt;

&lt;p&gt;Do this before you touch anything:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Get to a clean Terraform plan.&lt;/strong&gt; &lt;code&gt;terraform plan&lt;/code&gt; should show no changes. Switching tools with pending changes is a bad idea.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Standardize versions.&lt;/strong&gt; If your configs run different Terraform versions, bring them to the same version first. It narrows down what could go wrong.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Inventory your root modules.&lt;/strong&gt; List every config, its backend, and which ones read each other's state with &lt;code&gt;terraform_remote_state&lt;/code&gt;. (&lt;code&gt;grep -r terraform_remote_state .&lt;/code&gt; is a good start.)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Check your providers and modules.&lt;/strong&gt; Search for each at &lt;a href="https://search.opentofu.org" rel="noopener noreferrer"&gt;search.opentofu.org&lt;/a&gt;. Most are there, but a missing one will stop &lt;code&gt;tofu init&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Look at your tooling.&lt;/strong&gt; Note every place that runs &lt;code&gt;terraform&lt;/code&gt;: CI pipelines, Makefiles, pre-commit hooks, Docker images, Terragrunt or Atlantis configs and IDE plugins.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Check whether you use HCP Terraform (Terraform Cloud).&lt;/strong&gt; That is a bigger migration. See the gotchas below.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Back up state.&lt;/strong&gt; More on this in the walkthrough.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The migration at a glance
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNpFkcFu2zAQRH9lu2cqQYGehCJFbMe1D0mAOi1QiD6spJVMmOIKJBXXdfzvBSVYPZJvdjizvGAlNWOOjZVTdSAf4W2lHQDAY6FxQdURhh5CpMhAroYk17iHLHuARaFx60Ika-G1Z_cmzaBxP40vRsmy0BilGcA4E2e2HNnqxnpLbmarkT1dNL4IVAdyLYdvGq8TfUr0Q-NvDho_YH2zoL6359njpnqRJPpeaNxF6e9gJeAkTuK7r6W_f9i6dw7RtKmeePBiLZRUHWer9RhnU2h8piODOIbQpcJTtNEk7SV1uB-NgVoy__tsRoNtofHV2TM4OeVAtfRxXlkmCTRMcfCp1h4Vduw7MjXmF4wH7tIP1dzQYCOq6eYXeUOl5ZA0jbi4ps7YM-aYpRichXOI3ClYWOOOz1TtxvNaXFSgccetMPzcalTwQ0qJomDD9p2jqUjBozdkFQRyIQvsTYNqfGRn_qYsn7_0f_B6VVi2S7HiMcdPp4OJjNd_SOa_Bg%3Ftype%3Dpng" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNpFkcFu2zAQRH9lu2cqQYGehCJFbMe1D0mAOi1QiD6spJVMmOIKJBXXdfzvBSVYPZJvdjizvGAlNWOOjZVTdSAf4W2lHQDAY6FxQdURhh5CpMhAroYk17iHLHuARaFx60Ika-G1Z_cmzaBxP40vRsmy0BilGcA4E2e2HNnqxnpLbmarkT1dNL4IVAdyLYdvGq8TfUr0Q-NvDho_YH2zoL6359njpnqRJPpeaNxF6e9gJeAkTuK7r6W_f9i6dw7RtKmeePBiLZRUHWer9RhnU2h8piODOIbQpcJTtNEk7SV1uB-NgVoy__tsRoNtofHV2TM4OeVAtfRxXlkmCTRMcfCp1h4Vduw7MjXmF4wH7tIP1dzQYCOq6eYXeUOl5ZA0jbi4ps7YM-aYpRichXOI3ClYWOOOz1TtxvNaXFSgccetMPzcalTwQ0qJomDD9p2jqUjBozdkFQRyIQvsTYNqfGRn_qYsn7_0f_B6VVi2S7HiMcdPp4OJjNd_SOa_Bg%3Ftype%3Dpng" alt="Mermaid Diagram" width="360" height="1011"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Hands-on: migrate a demo project
&lt;/h2&gt;

&lt;p&gt;You'll create a tiny project with Terraform, then move it to OpenTofu. It uses the &lt;code&gt;random&lt;/code&gt; provider and the built-in &lt;code&gt;terraform_data&lt;/code&gt; resource, so it needs no cloud account.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 0: Create the starting point
&lt;/h3&gt;

&lt;p&gt;Create a folder and a &lt;code&gt;main.tf&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="n"&gt;terraform&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;required_version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"&amp;gt;= 1.5.0"&lt;/span&gt;

  &lt;span class="n"&gt;required_providers&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;source&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"hashicorp/random"&lt;/span&gt;
      &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"~&amp;gt; 3.6"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"random_pet"&lt;/span&gt; &lt;span class="s2"&gt;"app"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"terraform_data"&lt;/span&gt; &lt;span class="s2"&gt;"release"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"release-${random_pet.app.id}"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="s2"&gt;"app_name"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;random_pet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Apply it with Terraform so you have real state to migrate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;terraform init
terraform apply -auto-approve
terraform plan   &lt;span class="c"&gt;# should say there are no changes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 1: Back up state and code
&lt;/h3&gt;

&lt;p&gt;For local state, as in this demo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mkdir -p backup
cp terraform.tfstate* .terraform.lock.hcl backup/
git add -A &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; git commit -m &lt;span class="s2"&gt;"Pre-OpenTofu snapshot"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For remote state, use your backend's own backup. For S3 that means turning on bucket versioning. You can also save a copy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;terraform state pull &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; backup/state-before-tofu.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Install OpenTofu
&lt;/h3&gt;

&lt;p&gt;Follow the &lt;a href="https://opentofu.org/docs/intro/install/" rel="noopener noreferrer"&gt;install guide&lt;/a&gt; for your platform, then check it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tofu --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep Terraform installed on your own machine for now. You'll want it for rollback.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Initialize
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tofu init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This downloads providers from the OpenTofu registry, initializes your backend, and prepares the working directory. Check what changed in the lock file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git diff .terraform.lock.hcl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see provider entries for the OpenTofu registry. Commit the updated lock file along with the migration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Plan and look for "no changes"
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tofu plan
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see &lt;strong&gt;"No changes."&lt;/strong&gt; If you see changes, do &lt;strong&gt;not&lt;/strong&gt; apply them. Find out why first. If you can't explain it, roll back (see below).&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: Apply
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tofu apply
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even with no infrastructure changes, this step lets OpenTofu write the state in its own format.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;This is the point of no easy return.&lt;/strong&gt; After this, the state has been written by OpenTofu. If you need to go back to Terraform, restore your backup rather than assuming Terraform can read the new state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Step 6: Make one small change
&lt;/h3&gt;

&lt;p&gt;Edit &lt;code&gt;main.tf&lt;/code&gt; and change the &lt;code&gt;terraform_data&lt;/code&gt; input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"terraform_data"&lt;/span&gt; &lt;span class="s2"&gt;"release"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"release-${random_pet.app.id}-v2"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tofu plan
tofu apply
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An in-place update with no surprises tells you OpenTofu can manage this project from here on.&lt;/p&gt;

&lt;h2&gt;
  
  
  State handling in depth
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Compatibility goes one way
&lt;/h3&gt;

&lt;p&gt;OpenTofu reads Terraform 1.x state files without modification. The reverse isn't guaranteed. Terraform may not reliably read state written by OpenTofu, especially if you've used OpenTofu-only features.&lt;/p&gt;

&lt;p&gt;Practical rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;One tool per state file.&lt;/strong&gt; After you migrate a state, nobody runs &lt;code&gt;terraform apply&lt;/code&gt; against it again.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Keep backups and versioning on&lt;/strong&gt; until you've lived with the new setup for a while.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Treat saved plan files as tool-specific.&lt;/strong&gt; Apply a plan with the same tool that created it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Remote backends
&lt;/h3&gt;

&lt;p&gt;Your backend block stays the same. &lt;code&gt;tofu init&lt;/code&gt; reads it as is. Two tips:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Don't change the backend and the tool in the same PR.&lt;/strong&gt; If something breaks, you won't know which change caused it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Change locking separately.&lt;/strong&gt; OpenTofu 1.10 added native S3 state locking, so you can drop DynamoDB. That's a worthwhile cleanup, but do it as its own change, after the migration:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="n"&gt;terraform&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;backend&lt;/span&gt; &lt;span class="s2"&gt;"s3"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;bucket&lt;/span&gt;       &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"my-state-bucket"&lt;/span&gt;
    &lt;span class="n"&gt;key&lt;/span&gt;          &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"prod/network/terraform.tfstate"&lt;/span&gt;
    &lt;span class="n"&gt;region&lt;/span&gt;       &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"us-east-1"&lt;/span&gt;
    &lt;span class="n"&gt;use_lockfile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Multiple configs that read each other's state
&lt;/h3&gt;

&lt;p&gt;If one config reads another's outputs through &lt;code&gt;terraform_remote_state&lt;/code&gt;, &lt;strong&gt;order matters&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A  (VPC)            producer: others read A's state
       / \
      B   C                 consumers of A
      |
      D                     consumer of B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The rule from the OpenTofu docs is to &lt;strong&gt;migrate consumers before the configs they read from.&lt;/strong&gt; Here that means D first, then B and C, and A last.&lt;/p&gt;

&lt;p&gt;Why? If you migrate A first, B and C (still on Terraform) would have to read state that OpenTofu wrote, and that direction isn't guaranteed to work. Migrating consumers first avoids the problem.&lt;/p&gt;

&lt;p&gt;After each config, run a plan on the configs that depend on it and confirm there are no unexpected changes. Avoid OpenTofu-only features until the whole chain has moved.&lt;/p&gt;

&lt;h3&gt;
  
  
  State encryption (do it after the migration)
&lt;/h3&gt;

&lt;p&gt;This is OpenTofu's headline feature. State files often contain secrets, such as passwords and access keys. OpenTofu can encrypt state and plan files at rest, before they reach the backend.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do this as a separate step from the migration.&lt;/strong&gt; Once you encrypt, Terraform can't read the state, and your rollback path gets harder.&lt;/p&gt;

&lt;p&gt;Create &lt;code&gt;encryption.tf&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="n"&gt;variable&lt;/span&gt; &lt;span class="s2"&gt;"state_passphrase"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;type&lt;/span&gt;      &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
  &lt;span class="n"&gt;sensitive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;terraform&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;encryption&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;# Lets OpenTofu read your existing unencrypted state during the transition&lt;/span&gt;
    &lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="s2"&gt;"unencrypted"&lt;/span&gt; &lt;span class="s2"&gt;"migrate"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;key_provider&lt;/span&gt; &lt;span class="s2"&gt;"pbkdf2"&lt;/span&gt; &lt;span class="s2"&gt;"main"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;passphrase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state_passphrase&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="s2"&gt;"aes_gcm"&lt;/span&gt; &lt;span class="s2"&gt;"main"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;keys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key_provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pbkdf2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;aes_gcm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;

      &lt;span class="n"&gt;fallback&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unencrypted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;migrate&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;plan&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;aes_gcm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;

      &lt;span class="n"&gt;fallback&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unencrypted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;migrate&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set the passphrase outside your code, then apply:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;TF_VAR_state_passphrase&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'use-a-long-random-passphrase-here'&lt;/span&gt;
tofu apply
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OpenTofu reads the old plain state through the fallback and writes the new state encrypted. With local state you can see the difference: &lt;code&gt;terraform.tfstate&lt;/code&gt; no longer contains your resource values in plain text, only encrypted data plus metadata such as the key provider's name. Run &lt;code&gt;tofu plan&lt;/code&gt; once to confirm OpenTofu can read it back.&lt;/p&gt;

&lt;p&gt;Once that works, &lt;strong&gt;remove the fallback&lt;/strong&gt; and require encryption from now on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;method&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;aes_gcm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;
      &lt;span class="n"&gt;enforced&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;plan&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;method&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;aes_gcm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;
      &lt;span class="n"&gt;enforced&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remove the &lt;code&gt;method "unencrypted"&lt;/code&gt; block too, then run &lt;code&gt;tofu plan&lt;/code&gt; to confirm everything still works. Run it with the wrong passphrase and OpenTofu stops with &lt;code&gt;decryption failed: cipher: message authentication failed&lt;/code&gt;. That is exactly what you want.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rules that will save you:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Back up your key and your unencrypted state first.&lt;/strong&gt; Without the key, encrypted state is unrecoverable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Don't rename key providers or methods after encrypting.&lt;/strong&gt; The encrypted data stores their names. Use a &lt;code&gt;fallback&lt;/code&gt; block when you change keys, or set &lt;code&gt;encrypted_metadata_alias&lt;/code&gt; on the key provider.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pick the key type on purpose.&lt;/strong&gt; A static passphrase must be long and complex (at least 16 characters). With a key management service (AWS KMS, GCP KMS, Azure Key Vault or OpenBao), use a separate key per state file and make sure keys rotate automatically. AES-GCM can suffer from key saturation if you reuse a short static key too long.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Know what encryption doesn't do.&lt;/strong&gt; It protects data at rest. It doesn't protect against data loss, replay of an old state or plan file, or the person running &lt;code&gt;tofu&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use&lt;/strong&gt; &lt;code&gt;enforced = true&lt;/code&gt; &lt;strong&gt;in code&lt;/strong&gt; if you pass encryption config through the &lt;code&gt;TF_ENCRYPTION&lt;/code&gt; environment variable, so a missing variable can't silently write unencrypted state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Let other configs decrypt it.&lt;/strong&gt; If a config reads this state with &lt;code&gt;terraform_remote_state&lt;/code&gt;, add a &lt;code&gt;remote_state_data_sources&lt;/code&gt; block so it can decrypt it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Know the way back.&lt;/strong&gt; To return to unencrypted state, reverse the process: make the &lt;code&gt;unencrypted&lt;/code&gt; method primary and move your old method into &lt;code&gt;fallback&lt;/code&gt;. Don't remove the old method until the migration finishes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Gotchas
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;code&gt;required_version&lt;/code&gt; means different things in the two tools
&lt;/h3&gt;

&lt;p&gt;OpenTofu 1.13 is not "older" or "newer" than a given Terraform release. The version numbers are independent, so a constraint written for one tool says nothing reliable about the other.&lt;/p&gt;

&lt;p&gt;Older OpenTofu releases read &lt;code&gt;required_version&lt;/code&gt; as an OpenTofu constraint everywhere, which was a headache for modules written for Terraform. Current releases treat it differently: in a plain &lt;code&gt;.tf&lt;/code&gt; file it is read as a &lt;strong&gt;Terraform&lt;/strong&gt; constraint, and OpenTofu 1.13.0 doesn't enforce it. This config plans fine on OpenTofu 1.13.0 even though it asks for Terraform 1.14 or newer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="n"&gt;terraform&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;required_version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"&amp;gt;= 1.14.0"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That removes an old migration blocker, but it also removes a guardrail: nothing stops a teammate from running an OpenTofu version that is too old for your code.&lt;/p&gt;

&lt;p&gt;The OpenTofu docs recommend splitting the constraint into two small files, one per tool:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="c1"&gt;# versions.tf (read by Terraform)&lt;/span&gt;
&lt;span class="n"&gt;terraform&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;required_version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"&amp;gt;= 1.14.0"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# versions.tofu (read by OpenTofu)&lt;/span&gt;
&lt;span class="n"&gt;terraform&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;required_version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"&amp;gt;= 1.9.0"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When both files exist, OpenTofu ignores &lt;code&gt;versions.tf&lt;/code&gt;. According to the OpenTofu docs, Terraform doesn't read &lt;code&gt;.tofu&lt;/code&gt; files at all. On OpenTofu 1.13.0, a constraint in &lt;code&gt;versions.tofu&lt;/code&gt; that isn't met stops the run with an &lt;em&gt;Incompatible module&lt;/em&gt; error. OpenTofu 1.12 and later also offer a &lt;code&gt;language&lt;/code&gt; block for this, for example &lt;code&gt;compatible_with { opentofu = "&amp;gt;= 1.12" }&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Check the &lt;code&gt;required_version&lt;/code&gt; in your root modules &lt;strong&gt;and the modules you consume&lt;/strong&gt;, and test against the exact OpenTofu version your CI will pin, because older releases behave differently.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Registry and lock file changes
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;tofu init&lt;/code&gt; pulls providers from the OpenTofu registry by default, so &lt;code&gt;.terraform.lock.hcl&lt;/code&gt; changes. Commit it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;If you build the lock file on one machine and run CI on another OS, add hashes for every platform you use:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tofu providers lock -platform=linux_amd64 -platform=darwin_arm64
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Source addresses that explicitly point to &lt;code&gt;registry.terraform.io&lt;/code&gt; may need updating.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If a provider or module isn't in the OpenTofu registry, &lt;code&gt;tofu init&lt;/code&gt; fails with a message like &lt;em&gt;Failed to query available provider packages&lt;/em&gt; or &lt;em&gt;Module not found&lt;/em&gt;. Roll back and ask for it to be added to the registry.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Don't mix migration and new features
&lt;/h3&gt;

&lt;p&gt;The docs say to avoid OpenTofu-only features during the migration. If you use &lt;code&gt;enabled&lt;/code&gt;, provider &lt;code&gt;for_each&lt;/code&gt; or encryption on day one, you lose both an easy rollback and the ability to tell which change caused a problem.&lt;/p&gt;

&lt;p&gt;When you're ready, &lt;code&gt;.tofu&lt;/code&gt; files give you a way to add OpenTofu-only code to a module that Terraform users also consume. OpenTofu loads a &lt;code&gt;.tofu&lt;/code&gt; file &lt;strong&gt;instead of&lt;/strong&gt; the &lt;code&gt;.tf&lt;/code&gt; file with the same name, and loads a &lt;code&gt;.tofu&lt;/code&gt; file with a different name &lt;strong&gt;in addition to&lt;/strong&gt; your &lt;code&gt;.tf&lt;/code&gt; files. Terraform doesn't read &lt;code&gt;.tofu&lt;/code&gt; files (see gotcha 1), so it never sees them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;main.tf        # works in both tools
main.tofu      # OpenTofu loads this instead of main.tf
extra.tofu     # OpenTofu loads this as well; Terraform ignores it
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Version hops
&lt;/h3&gt;

&lt;p&gt;Don't combine the migration with a big version jump. The older OpenTofu migration guides were written per Terraform version (for example 1.7.x) and said to migrate to the matching OpenTofu version first, then upgrade OpenTofu afterwards. The current docs have a single general guide, and a clean &lt;code&gt;tofu plan&lt;/code&gt; is what tells you the move worked. Either way, migrate first and upgrade later.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Plans can change after an OpenTofu upgrade
&lt;/h3&gt;

&lt;p&gt;Even after a clean migration, a later OpenTofu upgrade can show harmless-looking changes. For example, in 1.13 the &lt;code&gt;base64gzip&lt;/code&gt; function produces results that are equivalent but not byte-for-byte identical to earlier versions. If you use it in a resource argument, your plan may show a change. The OpenTofu team's workaround is to add &lt;code&gt;ignore_changes&lt;/code&gt; temporarily, until you're ready to accept the new value.&lt;/p&gt;

&lt;p&gt;Read the release notes before every upgrade.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. HCP Terraform is a bigger move
&lt;/h3&gt;

&lt;p&gt;If your state lives in HCP Terraform (Terraform Cloud), this isn't a binary swap. You're also changing where state is stored and where runs happen. Plan for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;A new home for state&lt;/strong&gt;, such as S3, GCS or Azure Blob. A &lt;code&gt;cloud {}&lt;/code&gt; block won't simply carry over: OpenTofu has its own &lt;code&gt;cloud&lt;/code&gt; backend that requires an explicit &lt;code&gt;hostname&lt;/code&gt; and has no default, so it won't point at HCP Terraform on its own.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Workspace variables, run triggers and policy checks&lt;/strong&gt; (for example Sentinel) that don't move with the state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;A CI-based or third-party workflow&lt;/strong&gt; to replace remote runs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The conservative way to move the state itself is to pull it while you're still on Terraform, point the config at the new backend, then push it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;terraform state pull &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; backup/state.json    &lt;span class="c"&gt;# still on Terraform&lt;/span&gt;
&lt;span class="c"&gt;# edit your backend config to the new backend, then:&lt;/span&gt;
tofu init
tofu state push backup/state.json
tofu plan                                   &lt;span class="c"&gt;# expect: No changes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Treat it as its own project with its own rollback plan.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Tooling still calls &lt;code&gt;terraform&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Search for it everywhere:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;CI pipelines. Use the &lt;code&gt;opentofu/setup-opentofu&lt;/code&gt; action and &lt;strong&gt;pin the version&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Makefiles, shell scripts and pre-commit hooks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Docker images and dev containers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Terragrunt, Atlantis and similar wrappers. Point them at the &lt;code&gt;tofu&lt;/code&gt; binary (check their docs for the current setting).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Linters and scanners. Most work on HCL files and don't care, but check.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  8. The stray &lt;code&gt;terraform apply&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The most realistic way to hurt yourself after a migration is a teammate running &lt;code&gt;terraform apply&lt;/code&gt; out of habit. Make the switch loud:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Remove Terraform from CI images and shared dev containers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add a note to the repo's README and PR template.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Apply only through CI where you can.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Rolling back
&lt;/h2&gt;

&lt;p&gt;If something goes wrong &lt;strong&gt;before your first&lt;/strong&gt; &lt;code&gt;tofu apply&lt;/code&gt;, you've changed nothing. Just go back to Terraform.&lt;/p&gt;

&lt;p&gt;If it goes wrong &lt;strong&gt;after&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stop using OpenTofu.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Restore your state backup&lt;/strong&gt; if OpenTofu changed the state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run &lt;code&gt;terraform init&lt;/code&gt; and &lt;code&gt;terraform plan&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Confirm there are &lt;strong&gt;no unexpected changes&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Carry on with Terraform.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Where you are&lt;/th&gt;
&lt;th&gt;Can you roll back?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Before the first tofu apply&lt;/td&gt;
&lt;td&gt;Yes, nothing changed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;After apply, no OpenTofu-only features&lt;/td&gt;
&lt;td&gt;Yes, restore the backed-up state and re-plan with Terraform&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;After using OpenTofu-only features&lt;/td&gt;
&lt;td&gt;Harder. Remove those features first, then restore and re-plan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;After enabling state encryption&lt;/td&gt;
&lt;td&gt;Roll back encryption first (or restore your unencrypted backup), then go back to Terraform&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Migration checklist
&lt;/h2&gt;

&lt;p&gt;Copy this into your ticket:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;terraform plan&lt;/code&gt; is clean on every root module&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;All configs on the same Terraform version&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Providers and modules checked at search.opentofu.org&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Version constraints sorted: &lt;code&gt;versions.tf&lt;/code&gt; for Terraform, &lt;code&gt;versions.tofu&lt;/code&gt; for OpenTofu&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dependency graph mapped, &lt;code&gt;terraform_remote_state&lt;/code&gt; order decided (consumers first)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;State backed up (versioning on, &lt;code&gt;state pull&lt;/code&gt; saved)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Code committed on a migration branch&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;tofu init&lt;/code&gt; run, lock file diff reviewed and committed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;tofu plan&lt;/code&gt; shows no changes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;tofu apply&lt;/code&gt; done, then one small change planned and applied&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CI, scripts and wrappers switched to &lt;code&gt;tofu&lt;/code&gt;, version pinned&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;terraform&lt;/code&gt; removed from shared images and docs updated&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;State encryption enabled in a &lt;strong&gt;separate&lt;/strong&gt; change (optional)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Recap
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;OpenTofu is a Terraform fork under MPL 2.0, governed by the Linux Foundation. It runs the same HCL and uses the same providers through its own registry.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The migration is back up, &lt;code&gt;tofu init&lt;/code&gt;, &lt;code&gt;tofu plan&lt;/code&gt; (no changes), &lt;code&gt;tofu apply&lt;/code&gt;, then one small change.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;OpenTofu reads Terraform state, but not necessarily the other way around. Treat the first apply as a one-way door, and migrate consumers of &lt;code&gt;terraform_remote_state&lt;/code&gt; before their producers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;State encryption is OpenTofu's strongest differentiator. Add it &lt;strong&gt;after&lt;/strong&gt; the migration, guard your key, and use &lt;code&gt;enforced = true&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Before you start, check your version constraints (&lt;code&gt;versions.tofu&lt;/code&gt;), the lock file, any HCP Terraform usage and any tooling that still runs &lt;code&gt;terraform&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Migrate boringly first, then get fancy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://opentofu.org/docs/intro/migration/migration-guide/" rel="noopener noreferrer"&gt;OpenTofu: Migration Guide&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://opentofu.org/docs/intro/migration/multiple-configurations/" rel="noopener noreferrer"&gt;OpenTofu: Migrating interdependent configurations&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://opentofu.org/docs/language/state/encryption/" rel="noopener noreferrer"&gt;OpenTofu: State and Plan Encryption&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://opentofu.org/blog/opentofu-1-13-0/" rel="noopener noreferrer"&gt;OpenTofu v1.13.0 release post&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://opentofu.org/docs/intro/whats-new" rel="noopener noreferrer"&gt;OpenTofu: What's new in 1.11&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://opentofu.org/faq/" rel="noopener noreferrer"&gt;OpenTofu FAQ&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://opentofu.org/docs/language/settings/" rel="noopener noreferrer"&gt;OpenTofu: Language and compatibility settings&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.hashicorp.com/terraform/language/meta-arguments/lifecycle" rel="noopener noreferrer"&gt;Terraform: lifecycle reference (v1.17.x)&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.hashicorp.com/terraform/language/meta-arguments/for_each" rel="noopener noreferrer"&gt;Terraform: for_each meta-argument (v1.17.x)&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://spacelift.io/blog/hashicorps-license-change" rel="noopener noreferrer"&gt;Spacelift: HashiCorp's license change&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Details checked against the OpenTofu and Terraform documentation on November 18, 2026. The OpenTofu-side snippets (state encryption,&lt;/em&gt; &lt;code&gt;.tofu&lt;/code&gt; &lt;em&gt;files, version constraints, state pull and push) were run against OpenTofu 1.13.0. Tooling moves quickly, so if a command or behavior differs on your version, the official docs win. Try the demo on a throwaway project before you use it as a template for production.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Published via &lt;a href="https://zyvop.com/terraform-vs-opentofu-in-2026-a-safe-migration-walkthrough-na9jj?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=syndication" rel="noopener noreferrer"&gt;ZyVOP&lt;/a&gt; — Write once in Markdown, auto-backup to GitHub, and syndicate to Dev.to, Medium &amp;amp; Hashnode in 1 click.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>devops</category>
      <category>infrastructureascode</category>
      <category>migration</category>
      <category>opentofu</category>
    </item>
    <item>
      <title>Rotating User Agents and Headers in Python: A Practical Guide</title>
      <dc:creator>Bhavya Arora</dc:creator>
      <pubDate>Thu, 19 Nov 2026 15:57:41 +0000</pubDate>
      <link>https://dev.to/bhavya_arora/rotating-user-agents-and-headers-in-python-a-practical-guide-537e</link>
      <guid>https://dev.to/bhavya_arora/rotating-user-agents-and-headers-in-python-a-practical-guide-537e</guid>
      <description>&lt;p&gt;Every request your scraper sends carries a small introduction: &lt;em&gt;who am I, and what can I handle?&lt;/em&gt; Left alone, a Python script introduces itself as &lt;code&gt;python-requests/2.34.2&lt;/code&gt;, and some sites reject that on sight, even when they would happily serve a polite scraper.&lt;/p&gt;

&lt;p&gt;The usual advice is to "rotate your user agent", and the usual code picks a random string from a list. That is a start, but on its own it is often &lt;em&gt;worse&lt;/em&gt; than doing nothing. A Chrome user agent that arrives without the other headers Chrome sends is a client that doesn't exist in the real world. This guide shows a better approach: rotate &lt;strong&gt;complete, consistent header profiles&lt;/strong&gt;, &lt;strong&gt;per session&lt;/strong&gt; rather than per request, and &lt;strong&gt;check what you actually send&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Version note:&lt;/strong&gt; tested with Python 3.13, &lt;code&gt;requests&lt;/code&gt; 2.34, &lt;code&gt;httpx&lt;/code&gt; 0.28, Scrapy 2.20, &lt;code&gt;curl_cffi&lt;/code&gt; 0.17 and &lt;code&gt;fake-useragent&lt;/code&gt; 2.2. The browser versions in the code (Chrome 152, Firefox 155) are a few releases behind the current ones (Chrome 157, with 158 rolling out, and Firefox 160 in mid-November 2026). Browsers update every few weeks, so treat the versions as placeholders to refresh.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  What the Server Sees
&lt;/h2&gt;

&lt;p&gt;You can't improve what you can't see, so start by building a tiny server that echoes back the headers it receives. Save this as &lt;code&gt;echo_server.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;BaseHTTPRequestHandler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HTTPServer&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Echo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BaseHTTPRequestHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;do_GET&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;indent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send_response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send_header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send_header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Content-Length"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;end_headers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wfile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;log_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="c1"&gt;# keep the terminal quiet&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="nc"&gt;HTTPServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"127.0.0.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Echo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;serve_forever&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it in one terminal with &lt;code&gt;python echo_server.py&lt;/code&gt;, install the libraries in another (&lt;code&gt;pip install requests httpx&lt;/code&gt;), and look at what a plain &lt;code&gt;requests&lt;/code&gt; call sends:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"http://127.0.0.1:8000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"Host"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"127.0.0.1:8000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"User-Agent"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"python-requests/2.34.2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"Accept-Encoding"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"gzip, deflate, br, zstd"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"Accept"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"*/*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"Connection"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"keep-alive"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Your &lt;code&gt;Accept-Encoding&lt;/code&gt; depends on which compression libraries are installed. That detail matters later.) Compare this with what Chrome sends for an ordinary page visit: a dozen headers, including language preferences, a detailed &lt;code&gt;Accept&lt;/code&gt; list and several &lt;code&gt;sec-&lt;/code&gt; headers. A server that looks at all of them sees an obvious difference.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a Browser Sends
&lt;/h2&gt;

&lt;p&gt;You don't need to memorize every header, but you should know the ones that matter:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Header&lt;/th&gt;
&lt;th&gt;What it says&lt;/th&gt;
&lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;User-Agent&lt;/td&gt;
&lt;td&gt;Browser, major version and OS family&lt;/td&gt;
&lt;td&gt;Chrome freezes the minor version (152.0.0.0) and Safari freezes the macOS version (10_15_7), so little else varies&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Accept&lt;/td&gt;
&lt;td&gt;Content types the client can handle&lt;/td&gt;
&lt;td&gt;Browsers send a long, browser-specific list; libraries send &lt;em&gt;/&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Accept-Language&lt;/td&gt;
&lt;td&gt;Preferred languages&lt;/td&gt;
&lt;td&gt;Libraries often send none at all&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Accept-Encoding&lt;/td&gt;
&lt;td&gt;Compression the client can decode&lt;/td&gt;
&lt;td&gt;Must match what your library can actually decompress&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;sec-ch-ua, sec-ch-ua-mobile, sec-ch-ua-platform&lt;/td&gt;
&lt;td&gt;Client Hints repeating the browser, device type and OS&lt;/td&gt;
&lt;td&gt;Sent by Chrome and other Chromium browsers, not by Firefox or Safari&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sec-Fetch-Site, -Mode, -Dest, -User&lt;/td&gt;
&lt;td&gt;How the request was triggered&lt;/td&gt;
&lt;td&gt;none for a typed URL, same-origin for a click within a site&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Referer&lt;/td&gt;
&lt;td&gt;The page you came from&lt;/td&gt;
&lt;td&gt;Absent on a first visit&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The important idea is &lt;strong&gt;consistency&lt;/strong&gt;. The user agent, the Client Hints, the platform and the browser version must all tell the same story.&lt;/p&gt;

&lt;h2&gt;
  
  
  Level 1: Rotate the User-Agent
&lt;/h2&gt;

&lt;p&gt;The simplest version picks a user agent at random for each request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;

&lt;span class="no"&gt;USER_AGENTS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/152.0.0.0 Safari/537.36"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/152.0.0.0 Safari/537.36"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:155.0) Gecko/20100101 Firefox/155.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"User-Agent"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;USER_AGENTS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"http://127.0.0.1:8000"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"User-Agent"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works as far as it goes, and a lenient site may need nothing more. But look at what else arrives: &lt;code&gt;Accept: */*&lt;/code&gt;, no &lt;code&gt;Accept-Language&lt;/code&gt;, no &lt;code&gt;sec-ch-ua&lt;/code&gt;. Every one of those requests claims to be a modern browser while sending the headers of a Python library.&lt;/p&gt;

&lt;h2&gt;
  
  
  Level 2: Rotate Whole Header Profiles
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;profile&lt;/strong&gt; is a complete header set copied from a real browser. To capture one, open your browser's developer tools, go to the &lt;strong&gt;Network&lt;/strong&gt; tab, load a page, right-click the first request and choose &lt;strong&gt;Copy as cURL&lt;/strong&gt;. Keep headers like the ones in the profile below, and drop &lt;code&gt;Cookie&lt;/code&gt;, &lt;code&gt;Host&lt;/code&gt; and &lt;code&gt;Accept-Encoding&lt;/code&gt;, which your library should manage itself.&lt;/p&gt;

&lt;p&gt;Create &lt;code&gt;profiles.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;

&lt;span class="no"&gt;CHROME_WINDOWS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"sec-ch-ua"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'"Chromium";v="152", "Not?A_Brand";v="24", "Google Chrome";v="152"'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"sec-ch-ua-mobile"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"?0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"sec-ch-ua-platform"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'"Windows"'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"Upgrade-Insecure-Requests"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"User-Agent"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/152.0.0.0 Safari/537.36"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"Accept"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"Sec-Fetch-Site"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"none"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"Sec-Fetch-Mode"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"navigate"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"Sec-Fetch-User"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"?1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"Sec-Fetch-Dest"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"document"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"Accept-Language"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"en-US,en;q=0.9"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="no"&gt;CHROME_MACOS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="no"&gt;CHROME_WINDOWS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"sec-ch-ua-platform"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'"macOS"'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"User-Agent"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/152.0.0.0 Safari/537.36"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="no"&gt;PROFILES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;CHROME_WINDOWS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;CHROME_MACOS&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These two are the headers that the Chrome 152 profile of &lt;code&gt;curl_cffi&lt;/code&gt; (more on that library below) sent to my echo server, so use them as a starting point and replace them with a capture from your own browser when you can. Add Firefox or Safari profiles the same way. They have no &lt;code&gt;sec-ch-ua&lt;/code&gt; headers, which is exactly why you copy them whole instead of building them by hand.&lt;/p&gt;

&lt;p&gt;Hand-built or mixed-up profiles are easy to get wrong, so add a quick linter to the same file. It catches the mismatches that give a fake identity away:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;check_profile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Return a list of ways this header set contradicts itself."""&lt;/span&gt;
    &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;ua&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"user-agent"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;problems&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s2"&gt;"Chrome/"&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;ua&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s2"&gt;"sec-ch-ua"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;problems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"sec-ch-ua headers without a Chrome user agent"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;problems&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s2"&gt;"sec-ch-ua"&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Chrome user agent but no sec-ch-ua headers"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="n"&gt;major&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"Chrome/(\d+)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ua&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;group&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s1"&gt;'v="{major}"'&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"sec-ch-ua"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;problems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"user agent says Chrome {major} but sec-ch-ua disagrees"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;platform&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"sec-ch-ua-platform"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'"'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Windows"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s2"&gt;"Windows"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;ua&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s2"&gt;"macOS"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s2"&gt;"Macintosh"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;ua&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s2"&gt;"Linux"&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;platform&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;problems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"user agent says {expected} but sec-ch-ua-platform says {platform or 'nothing'}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;problems&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;profile&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;PROFILES&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="nf"&gt;check_profile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Level 3: Rotate Sessions, Not Requests
&lt;/h2&gt;

&lt;p&gt;A real visitor doesn't change browsers between page views. If you rotate on &lt;em&gt;every&lt;/em&gt; request, one cookie-carrying session appears to switch from Windows Chrome to a Mac a second later. Rotate per &lt;strong&gt;session&lt;/strong&gt; instead: choose a profile, keep it for a few dozen requests, then start over with a fresh identity and fresh cookies. Save this as &lt;code&gt;browser_session.py&lt;/code&gt; next to &lt;code&gt;profiles.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;urllib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parse&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;urlsplit&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;

&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;profiles&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="no"&gt;PROFILES&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BrowserSession&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""A requests session that behaves like one consistent browser."""&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;profiles&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;PROFILES&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rotate_after&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;profiles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;profiles&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rotate_after&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rotate_after&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_new_identity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;_new_identity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;profile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;profiles&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Session&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;               &lt;span class="c1"&gt;# a new session means new cookies&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# replaces User-Agent and Accept&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;limit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rotate_after&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_new_identity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last_url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;                               &lt;span class="c1"&gt;# behave like a click from the last page&lt;/span&gt;
            &lt;span class="n"&gt;same_site&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;urlsplit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;netloc&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nf"&gt;urlsplit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last_url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;netloc&lt;/span&gt;
            &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Referer"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last_url&lt;/span&gt;
            &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Sec-Fetch-Site"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"same-origin"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;same_site&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s2"&gt;"cross-site"&lt;/span&gt;

        &lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setdefault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"timeout"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"__main__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;browser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BrowserSession&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;reply&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;browser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"http://127.0.0.1:8000/page-{page}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"User-Agent"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"| Referer:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Referer"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three details are worth noticing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;session.headers.update(profile)&lt;/code&gt; overwrites the library's &lt;code&gt;User-Agent&lt;/code&gt; and &lt;code&gt;Accept&lt;/code&gt; and adds everything else. It leaves &lt;code&gt;Accept-Encoding&lt;/code&gt; to &lt;code&gt;requests&lt;/code&gt; for a reason covered below.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The first request has no&lt;/strong&gt; &lt;code&gt;Referer&lt;/code&gt; and &lt;code&gt;Sec-Fetch-Site: none&lt;/code&gt;, like a typed address. Later requests look like clicks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;A new&lt;/strong&gt; &lt;code&gt;Session&lt;/code&gt; &lt;strong&gt;is a new identity.&lt;/strong&gt; Cookies don't leak from one "visitor" to the next.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you also use proxies (see our guide to rotating proxies in Python), give each identity its own proxy and keep the pair together. Five different browsers behind one IP address looks just as odd as one browser jumping between five IP addresses mid-session.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Same Idea in httpx
&lt;/h2&gt;

&lt;p&gt;The profile dictionaries work unchanged with &lt;code&gt;httpx&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;httpx&lt;/span&gt;

&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;profiles&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="no"&gt;PROFILES&lt;/span&gt;

&lt;span class="n"&gt;profile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;PROFILES&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;httpx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"http://127.0.0.1:8000/page-1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"User-Agent"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;httpx&lt;/code&gt; keeps its own defaults (&lt;code&gt;Connection&lt;/code&gt;, &lt;code&gt;Accept-Encoding&lt;/code&gt;) and replaces &lt;code&gt;User-Agent&lt;/code&gt; and &lt;code&gt;Accept&lt;/code&gt; with yours. Like &lt;code&gt;requests&lt;/code&gt;, it sends headers in its own order rather than a browser's.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rotating Profiles in Scrapy
&lt;/h2&gt;

&lt;p&gt;In Scrapy, a downloader middleware is the right place. Put &lt;code&gt;profiles.py&lt;/code&gt; inside your project package (here it's called &lt;code&gt;myproject&lt;/code&gt;), then create the middleware in &lt;code&gt;myproject/middlewares.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;

&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;myproject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;profiles&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="no"&gt;PROFILES&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RotateProfileMiddleware&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Give every request a coherent browser header set."""&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process_request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;profile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setdefault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"profile"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;PROFILES&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enable it in &lt;code&gt;settings.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="no"&gt;DOWNLOADER_MIDDLEWARES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"myproject.middlewares.RotateProfileMiddleware"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;543&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The number matters: 543 runs &lt;em&gt;after&lt;/em&gt; Scrapy's built-in header middlewares (at 400 and 500), so your values replace their defaults. This also overrides the &lt;code&gt;USER_AGENT&lt;/code&gt; setting. Because the chosen profile is stored in &lt;code&gt;request.meta["profile"]&lt;/code&gt;, a retried or redirected request keeps the same identity. New requests created with &lt;code&gt;response.follow()&lt;/code&gt; don't inherit &lt;code&gt;meta&lt;/code&gt;, so to pin a whole chain of requests to one identity, pass it along explicitly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;follow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parse_page&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;meta&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"profile"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"profile"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Scrapy normalizes header names (&lt;code&gt;sec-ch-ua&lt;/code&gt; goes out as &lt;code&gt;Sec-Ch-Ua&lt;/code&gt;), one more small difference from a real browser that headers alone can't fix.&lt;/p&gt;

&lt;h2&gt;
  
  
  What About fake-useragent?
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://pypi.org/project/fake-useragent/" rel="noopener noreferrer"&gt;fake-useragent&lt;/a&gt; package gives you a ready-made list of user agents, and it's handy for quick experiments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fake_useragent&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;UserAgent&lt;/span&gt;

&lt;span class="n"&gt;ua&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;UserAgent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;browsers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Chrome"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Firefox"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Windows"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Mac OS X"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Linux"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;platforms&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"desktop"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ua&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Treat it with care. When I tested version 2.2.0:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The default is mostly mobile.&lt;/strong&gt; Mobile devices make up about 69% of its weighted data; in 3,000 draws from plain &lt;code&gt;UserAgent().random&lt;/code&gt;, 2,070 were mobile, including in-app browsers such as the Google and Facebook apps. Always pass &lt;code&gt;platforms=["desktop"]&lt;/code&gt; unless you want mobile pages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The data is old.&lt;/strong&gt; Its newest Chrome is 136 and its newest Firefox 138, both from spring 2025, while real browsers were at 157 and 160. It also contains Chrome versions as old as 65.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Failures are easy to miss.&lt;/strong&gt; Asking for a &lt;code&gt;min_version&lt;/code&gt; it has no data for doesn't raise an exception. It logs a short error and returns a fallback Edge 122 user agent.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And a user agent alone is only one line of a profile. Drop one of its strings into a profile and the linter shows the problem:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fake_useragent&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;UserAgent&lt;/span&gt;

&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;profiles&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="no"&gt;CHROME_WINDOWS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;check_profile&lt;/span&gt;

&lt;span class="n"&gt;ua&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;UserAgent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;browsers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Chrome"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Windows"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;platforms&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"desktop"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;mixed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="no"&gt;CHROME_WINDOWS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"User-Agent"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ua&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;check_profile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mixed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# ['user agent says Chrome 135 but sec-ch-ua disagrees']   (the version varies)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your own captured profiles, refreshed every few months, beat a random list.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Accept-Encoding Trap
&lt;/h2&gt;

&lt;p&gt;This is the one header you shouldn't copy from your browser. Chrome advertises &lt;code&gt;gzip, deflate, br, zstd&lt;/code&gt;. If you paste that into a &lt;code&gt;requests&lt;/code&gt; setup that doesn't have the &lt;code&gt;brotli&lt;/code&gt; package installed, the server may answer in Brotli and &lt;code&gt;response.text&lt;/code&gt; comes back as binary garbage. I reproduced exactly this. Leave &lt;code&gt;Accept-Encoding&lt;/code&gt; to the library, as the code above does, or install &lt;code&gt;brotli&lt;/code&gt; and &lt;code&gt;zstandard&lt;/code&gt; first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Beyond Headers
&lt;/h2&gt;

&lt;p&gt;Headers are only one signal. Sites can also look at the TLS handshake your client performs, HTTP/2 details, the order and capitalization of headers, your IP reputation and whether you run their JavaScript. Python's &lt;code&gt;requests&lt;/code&gt; can't change most of that, and no header profile can hide it. Some libraries address the lower layers. &lt;code&gt;curl_cffi&lt;/code&gt;, for example, impersonates a browser's connection and sends a matching, complete header set:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;curl_cffi&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;cffi_requests&lt;/span&gt;

&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cffi_requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"http://127.0.0.1:8000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;impersonate&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"chrome"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"user-agent"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a site has gone to the trouble of blocking automated traffic, more disguise only starts an arms race.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using This Responsibly
&lt;/h2&gt;

&lt;p&gt;Header rotation is a tool for being &lt;em&gt;compatible&lt;/em&gt;, not for being &lt;em&gt;invisible&lt;/em&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Prefer honesty where you can.&lt;/strong&gt; A descriptive agent such as &lt;code&gt;MyBot/1.0 (+https://example.com/bot; me@example.com)&lt;/code&gt; lets site owners contact you instead of blocking you. Use it unless a site rejects it for no good reason.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Never rotate to dodge a rule.&lt;/strong&gt; &lt;code&gt;robots.txt&lt;/code&gt; rules are addressed to crawlers by name, and a &lt;code&gt;*&lt;/code&gt; group applies to everyone. Changing your identity to slip past a &lt;code&gt;Disallow&lt;/code&gt;, a rate limit or a ban you were given is circumvention, and it can turn a terms-of-service problem into a bigger one. Our guide "Is Web Scraping Legal?" explains why.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Don't raise your volume.&lt;/strong&gt; Rotation doesn't change how many requests you send. Keep your delays and respect &lt;code&gt;429&lt;/code&gt; responses.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Legitimate uses exist.&lt;/strong&gt; Making a permitted scraper work with a site that rejects library agents, or testing how your own site responds to different browsers.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Troubleshooting
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Symptom&lt;/th&gt;
&lt;th&gt;Likely cause&lt;/th&gt;
&lt;th&gt;What to try&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;403 even with a full browser profile&lt;/td&gt;
&lt;td&gt;The block uses your IP, TLS fingerprint or a JavaScript check, not your headers&lt;/td&gt;
&lt;td&gt;Slow down and re-read the site's rules before trying anything more&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;response.text is binary garbage&lt;/td&gt;
&lt;td&gt;You advertised br or zstd without the matching package&lt;/td&gt;
&lt;td&gt;Drop Accept-Encoding or install brotli and zstandard&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Parser breaks on some requests&lt;/td&gt;
&lt;td&gt;You're getting mobile and desktop HTML at random&lt;/td&gt;
&lt;td&gt;Use desktop-only profiles&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CAPTCHAs appear after rotating&lt;/td&gt;
&lt;td&gt;Identity changes mid-session, or one IP shows many browsers&lt;/td&gt;
&lt;td&gt;Rotate per session and pair each identity with one proxy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;check_profile reports a mismatch&lt;/td&gt;
&lt;td&gt;Header set was mixed from different sources&lt;/td&gt;
&lt;td&gt;Recapture the whole set from one browser&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scrapy ignores your USER_AGENT setting&lt;/td&gt;
&lt;td&gt;The middleware overwrites it&lt;/td&gt;
&lt;td&gt;That's intended; edit the profiles instead&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Where to Go from Here
&lt;/h2&gt;

&lt;p&gt;You now have a pattern you can trust: capture real header sets, lint them, rotate them per session, and check what actually leaves your machine with the echo server. From here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Combine profiles with &lt;strong&gt;rotating proxies&lt;/strong&gt; and a polite delay (see our guides on both).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If a site needs JavaScript, use a real browser instead of imitating one (see our Playwright guide).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy scraping, and please do it politely!&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Found this helpful? Share it with a fellow developer or leave your questions in the comments.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Published via &lt;a href="https://zyvop.com/rotating-user-agents-and-headers-in-python-a-practical-guide-7p6sc?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=syndication" rel="noopener noreferrer"&gt;ZyVOP&lt;/a&gt; — Write once in Markdown, auto-backup to GitHub, and syndicate to Dev.to, Medium &amp;amp; Hashnode in 1 click.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>httpheaders</category>
      <category>python</category>
      <category>requests</category>
      <category>useragent</category>
    </item>
    <item>
      <title>Web Scraping with Scrapy: Building a Spider from Scratch</title>
      <dc:creator>Bhavya Arora</dc:creator>
      <pubDate>Wed, 18 Nov 2026 07:51:07 +0000</pubDate>
      <link>https://dev.to/bhavya_arora/web-scraping-with-scrapy-building-a-spider-from-scratch-gn</link>
      <guid>https://dev.to/bhavya_arora/web-scraping-with-scrapy-building-a-spider-from-scratch-gn</guid>
      <description>&lt;p&gt;A &lt;code&gt;requests&lt;/code&gt; and BeautifulSoup script is the right way to start scraping. But as soon as a job grows, you find yourself writing the same plumbing again and again: a loop over pages, retries, delays, de-duplication, saving results. Soon your 30-line script is 300 lines and mostly not about the data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scrapy&lt;/strong&gt; is a framework that supplies that plumbing. You describe &lt;em&gt;what to extract&lt;/em&gt; in a small class called a spider, and Scrapy handles requests, concurrency, retries, throttling, &lt;code&gt;robots.txt&lt;/code&gt;, logging, and exporting results.&lt;/p&gt;

&lt;p&gt;In this guide you'll build a complete Scrapy project from scratch. It will crawl every page of a bookstore, follow each book to its detail page, clean the data in a pipeline, and export it to JSON or CSV, politely.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Version note:&lt;/strong&gt; this guide targets &lt;strong&gt;Scrapy 2.20&lt;/strong&gt; (released November 2026), which requires &lt;strong&gt;Python 3.11 or newer&lt;/strong&gt;. Scrapy has changed a lot recently, and many tutorials online are out of date. The biggest example: the old &lt;code&gt;start_requests()&lt;/code&gt; method was &lt;strong&gt;removed in Scrapy 2.16&lt;/strong&gt;, and spiders now use &lt;code&gt;async def start()&lt;/code&gt;. If you copy a spider from an older tutorial and it crawls nothing, that's probably why.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Why Scrapy?
&lt;/h2&gt;

&lt;p&gt;Here's what you get without writing it yourself:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Need&lt;/th&gt;
&lt;th&gt;In a script&lt;/th&gt;
&lt;th&gt;In Scrapy&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Many requests at once&lt;/td&gt;
&lt;td&gt;Threads or asyncio by hand&lt;/td&gt;
&lt;td&gt;Built-in concurrency&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Retrying failures&lt;/td&gt;
&lt;td&gt;Your own loop&lt;/td&gt;
&lt;td&gt;Built-in retry middleware&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Being polite&lt;/td&gt;
&lt;td&gt;time.sleep()&lt;/td&gt;
&lt;td&gt;Download delay and AutoThrottle&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Obeying robots.txt&lt;/td&gt;
&lt;td&gt;Extra code&lt;/td&gt;
&lt;td&gt;One setting, on by default&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Skipping repeated URLs&lt;/td&gt;
&lt;td&gt;A set() you maintain&lt;/td&gt;
&lt;td&gt;Automatic de-duplication&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cleaning and saving data&lt;/td&gt;
&lt;td&gt;Mixed into the scraping code&lt;/td&gt;
&lt;td&gt;Item pipelines and feed exports&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Knowing what happened&lt;/td&gt;
&lt;td&gt;print()&lt;/td&gt;
&lt;td&gt;Logs and crawl statistics&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;When not to use it.&lt;/strong&gt; For a single page or a quick one-off, &lt;code&gt;requests&lt;/code&gt; and BeautifulSoup are simpler. For sites that build their content with JavaScript, you need a browser: see our guide to scraping JavaScript-heavy sites with Playwright. Scrapy shines when you need to crawl &lt;em&gt;many&lt;/em&gt; pages reliably.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up
&lt;/h2&gt;

&lt;p&gt;Check that you have Python 3.11 or newer, then create a virtual environment and install Scrapy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python --version                 &lt;span class="c"&gt;# 3.11 or newer&lt;/span&gt;
python -m venv venv
&lt;span class="nb"&gt;source&lt;/span&gt; venv/bin/activate         &lt;span class="c"&gt;# On Windows: venv\Scripts\activate&lt;/span&gt;
pip install scrapy
scrapy version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now create a project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;scrapy startproject bookscraper
&lt;span class="nb"&gt;cd&lt;/span&gt; bookscraper
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You get this layout:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bookscraper/
├── scrapy.cfg                # project marker (run commands from this folder)
└── bookscraper/
    ├── items.py              # the shape of the data you extract
    ├── middlewares.py        # hooks into the request/response flow
    ├── pipelines.py          # clean, validate and store items
    ├── settings.py           # all configuration
    └── spiders/              # your spiders live here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things in the generated &lt;code&gt;settings.py&lt;/code&gt; are worth noticing right away: &lt;code&gt;ROBOTSTXT_OBEY = True&lt;/code&gt;, so Scrapy respects &lt;code&gt;robots.txt&lt;/code&gt; by default, and a &lt;code&gt;DOWNLOAD_DELAY&lt;/code&gt; of 1 second with one request at a time per domain. New projects start out polite.&lt;/p&gt;

&lt;h2&gt;
  
  
  Explore the Page with the Scrapy Shell
&lt;/h2&gt;

&lt;p&gt;Before writing any spider, experiment on the live page. The shell downloads a URL and gives you a &lt;code&gt;response&lt;/code&gt; object to play with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;scrapy shell &lt;span class="s2"&gt;"https://books.toscrape.com/"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll use the sandbox site &lt;a href="https://books.toscrape.com/" rel="noopener noreferrer"&gt;books.toscrape.com&lt;/a&gt;, which exists for practicing scraping. Inside the shell, try:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; response.css("article.product_pod h3 a::attr(title)").get()
'A Light in the Attic'
&amp;gt;&amp;gt;&amp;gt; response.css("p.price_color::text").getall()[:3]
['£51.77', '£53.74', '£50.10']
&amp;gt;&amp;gt;&amp;gt; response.xpath("//li[@class='next']/a/@href").get()
'catalogue/page-2.html'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few things to learn from this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;response.css()&lt;/code&gt; takes CSS selectors, extended with &lt;code&gt;::text&lt;/code&gt; (the text of an element) and &lt;code&gt;::attr(name)&lt;/code&gt; (an attribute's value).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;response.xpath()&lt;/code&gt; does the same job with XPath, which is handy for odd cases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;.get()&lt;/code&gt; returns the first match, or &lt;code&gt;None&lt;/code&gt; if there isn't one. &lt;code&gt;.getall()&lt;/code&gt; returns a list of every match.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The shell is the fastest way to get selectors right, so use it constantly. Type &lt;code&gt;exit()&lt;/code&gt; to leave.&lt;/p&gt;

&lt;h2&gt;
  
  
  Your First Spider
&lt;/h2&gt;

&lt;p&gt;A spider is a class with a name, a place to start, and a &lt;code&gt;parse&lt;/code&gt; method that turns a response into data. You can generate a skeleton with &lt;code&gt;scrapy genspider books books.toscrape.com&lt;/code&gt;, but it's short enough to write by hand. Create &lt;code&gt;bookscraper/spiders/books.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;scrapy&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BooksSpider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scrapy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Spider&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"books"&lt;/span&gt;
    &lt;span class="n"&gt;allowed_domains&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"books.toscrape.com"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;start_urls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"https://books.toscrape.com/"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"article.product_pod"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="s2"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"h3 a::attr(title)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"p.price_color::text"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s2"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urljoin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"h3 a::attr(href)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How it works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;name&lt;/code&gt; is how you run the spider.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;allowed_domains&lt;/code&gt; stops the spider from wandering off to other sites.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;start_urls&lt;/code&gt; lists the first pages to download. Scrapy requests each one and passes the response to &lt;code&gt;parse&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;yield&lt;/code&gt; hands each result back to Scrapy. A spider doesn't &lt;code&gt;return&lt;/code&gt; a list: it yields items one at a time, and Scrapy streams them to wherever they're going.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Run it and export to a file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;scrapy crawl books -O books.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;-O&lt;/code&gt; (capital) &lt;strong&gt;overwrites&lt;/strong&gt; the output file, so you always start fresh. Open &lt;code&gt;books.json&lt;/code&gt; and you'll have the first page of books.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Watch out for&lt;/strong&gt; &lt;code&gt;-o&lt;/code&gt;&lt;strong&gt;.&lt;/strong&gt; In projects created with Scrapy 2.20, the generated settings include &lt;code&gt;FEED_MODE = "create"&lt;/code&gt;. If the file already exists, lowercase &lt;code&gt;-o&lt;/code&gt; then logs an error (&lt;code&gt;Not writing ... because it already exists&lt;/code&gt;) and throws that run's items away, yet the command still exits normally, so it's easy to miss. In older setups, and outside a project, &lt;code&gt;-o&lt;/code&gt; &lt;em&gt;appends&lt;/em&gt;, and appending a second run to a JSON file leaves you with a corrupted file. Use &lt;code&gt;-O&lt;/code&gt; while you're developing, or pick the behavior explicitly with the &lt;code&gt;FEED_MODE&lt;/code&gt; setting.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you just want to try a spider quickly without creating a project, &lt;code&gt;scrapy runspider books.py -O books.json&lt;/code&gt; runs a single file.&lt;/p&gt;

&lt;h3&gt;
  
  
  About &lt;code&gt;start_urls&lt;/code&gt; and &lt;code&gt;start()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;start_urls&lt;/code&gt; is the simplest way to say where to begin. When you need more control, such as generating a list of URLs or adding headers, write an &lt;code&gt;async def start()&lt;/code&gt; method instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BooksSpider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scrapy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Spider&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"books"&lt;/span&gt;
    &lt;span class="n"&gt;allowed_domains&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"books.toscrape.com"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"https://books.toscrape.com/catalogue/page-{page}.html"&lt;/span&gt;
            &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;scrapy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This replaces &lt;code&gt;start_requests()&lt;/code&gt; from older tutorials. Note that &lt;code&gt;async def&lt;/code&gt; and &lt;code&gt;yield&lt;/code&gt; together make it an asynchronous generator, which Scrapy knows how to run. A leftover &lt;code&gt;start_requests()&lt;/code&gt; isn't an error: the spider simply starts, reports &lt;code&gt;Crawled 0 pages&lt;/code&gt;, and finishes without a word of complaint.&lt;/p&gt;

&lt;h2&gt;
  
  
  Following Pagination
&lt;/h2&gt;

&lt;p&gt;Scraping one page isn't much of a crawler. Add this to the end of &lt;code&gt;parse&lt;/code&gt; to follow the "Next" link:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;next_page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"li.next a::attr(href)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;next_page&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;follow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next_page&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;response.follow()&lt;/code&gt; creates a new request for the link and sends its response back to &lt;code&gt;parse&lt;/code&gt;. It handles &lt;strong&gt;relative URLs for you&lt;/strong&gt; (no &lt;code&gt;urljoin&lt;/code&gt; needed), and Scrapy automatically skips any URL it has already requested. When there's no Next link on the last page, &lt;code&gt;get()&lt;/code&gt; returns &lt;code&gt;None&lt;/code&gt; and the crawl ends.&lt;/p&gt;

&lt;h2&gt;
  
  
  Following Links to Detail Pages
&lt;/h2&gt;

&lt;p&gt;The listing doesn't have everything: the description and product code live on each book's own page. So the spider needs two callbacks: one for listing pages and one for book pages. It will also pass the star rating (which we only read from the listing) along to the second callback.&lt;/p&gt;

&lt;p&gt;First, define the shape of your data in &lt;code&gt;bookscraper/items.py&lt;/code&gt;. Recent Scrapy templates use a plain dataclass:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;dataclasses&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dataclass&lt;/span&gt;

&lt;span class="nd"&gt;@dataclass&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
    &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
    &lt;span class="n"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
    &lt;span class="n"&gt;availability&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
    &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
    &lt;span class="n"&gt;upc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
    &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now rewrite the spider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;scrapy&lt;/span&gt;

&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;bookscraper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Book&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BooksSpider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scrapy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Spider&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"books"&lt;/span&gt;
    &lt;span class="n"&gt;allowed_domains&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"books.toscrape.com"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;start_urls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"https://books.toscrape.com/"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="s2"&gt;"""Listing page: follow every book, then the Next link."""&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"article.product_pod"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;rating&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"p.star-rating::attr(class)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;   &lt;span class="c1"&gt;# "star-rating Three" -&amp;gt; "Three"&lt;/span&gt;
            &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;follow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"h3 a::attr(href)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parse_book&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;cb_kwargs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"rating"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;next_page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"li.next a::attr(href)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;next_page&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;follow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next_page&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;parse_book&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="s2"&gt;"""Book page: build the item."""&lt;/span&gt;
        &lt;span class="n"&gt;availability&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"p.availability::text"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"h1::text"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"p.price_color::text"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;rating&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;availability&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;availability&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#product_description ~ p::text"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;upc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"table tr:first-child td::text"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notes on what's new:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Callbacks chain the crawl.&lt;/strong&gt; &lt;code&gt;parse&lt;/code&gt; yields &lt;em&gt;requests&lt;/em&gt; for book pages, and each response goes to &lt;code&gt;parse_book&lt;/code&gt;, which yields &lt;em&gt;items&lt;/em&gt;. A callback can yield both requests and items.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;cb_kwargs&lt;/code&gt; passes extra data to the next callback as keyword arguments. That's how &lt;code&gt;rating&lt;/code&gt; travels from the listing page to the book page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Whitespace cleanup.&lt;/strong&gt; The availability text has line breaks and padding around it, so we collapse it with &lt;code&gt;" ".join(text.split())&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Missing data.&lt;/strong&gt; Some pages will lack a field, a description for instance. &lt;code&gt;.get()&lt;/code&gt; returns &lt;code&gt;None&lt;/code&gt; instead of crashing, and the dataclass defaults to &lt;code&gt;None&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cleaning Data with Pipelines
&lt;/h2&gt;

&lt;p&gt;Right now &lt;code&gt;price&lt;/code&gt; is a string like &lt;code&gt;£51.77&lt;/code&gt; and &lt;code&gt;rating&lt;/code&gt; is a word like &lt;code&gt;Three&lt;/code&gt;. Item &lt;strong&gt;pipelines&lt;/strong&gt; are the right place to clean and validate items, keeping that logic out of the spider. Each pipeline is a class with a &lt;code&gt;process_item&lt;/code&gt; method. Open &lt;code&gt;bookscraper/pipelines.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;

&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;itemadapter&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;ItemAdapter&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;scrapy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;DropItem&lt;/span&gt;

&lt;span class="no"&gt;RATINGS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"One"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Two"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Three"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Four"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Five"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ValidateAndCleanPipeline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process_item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;adapter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ItemAdapter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;adapter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;adapter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;DropItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"Missing title or price: {adapter.get('url')}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;adapter&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"[^\d.]"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;adapter&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# "£51.77" -&amp;gt; 51.77&lt;/span&gt;
        &lt;span class="n"&gt;adapter&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"rating"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;RATINGS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;adapter&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"rating"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                   &lt;span class="c1"&gt;# "Three" -&amp;gt; 3&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DuplicatesPipeline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;seen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process_item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;upc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ItemAdapter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"upc"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;upc&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;seen&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;DropItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"Duplicate book: {upc}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;seen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;upc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key ideas:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;ItemAdapter&lt;/code&gt; lets one pipeline work with dictionaries, dataclasses, or &lt;code&gt;scrapy.Item&lt;/code&gt; objects through the same interface.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Return the item&lt;/strong&gt; to pass it along, or &lt;strong&gt;raise&lt;/strong&gt; &lt;code&gt;DropItem&lt;/code&gt; to discard it. Dropped items are logged and counted but never exported.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The first pipeline&lt;/strong&gt; drops incomplete items and converts the price and rating to numbers. &lt;strong&gt;The second&lt;/strong&gt; drops repeats using the UPC, a unique product code, because the same book can appear under more than one URL.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Older tutorials&lt;/strong&gt; write &lt;code&gt;process_item(self, item, spider)&lt;/code&gt;. That still runs in 2.20 but prints a deprecation warning, so drop the &lt;code&gt;spider&lt;/code&gt; argument in new code.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pipelines are off until you switch them on in &lt;code&gt;settings.py&lt;/code&gt;. The numbers set the order, and lower runs first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="no"&gt;ITEM_PIPELINES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"bookscraper.pipelines.ValidateAndCleanPipeline"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"bookscraper.pipelines.DuplicatesPipeline"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Crawling Politely: Settings
&lt;/h2&gt;

&lt;p&gt;Scrapy is fast, which is exactly why you must configure it carefully. Add these to &lt;code&gt;settings.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Tell site owners who you are and how to reach you&lt;/span&gt;
&lt;span class="no"&gt;USER_AGENT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"BookScraperBot/1.0 (+https://example.com/bot; contact@example.com)"&lt;/span&gt;

&lt;span class="c1"&gt;# Adapt the delay to how quickly the server responds&lt;/span&gt;
&lt;span class="no"&gt;AUTOTHROTTLE_ENABLED&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
&lt;span class="no"&gt;AUTOTHROTTLE_START_DELAY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="no"&gt;AUTOTHROTTLE_MAX_DELAY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="no"&gt;AUTOTHROTTLE_TARGET_CONCURRENCY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;

&lt;span class="c1"&gt;# Retry temporary failures a couple of times&lt;/span&gt;
&lt;span class="no"&gt;RETRY_TIMES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

&lt;span class="c1"&gt;# During development only: cache responses on disk so repeat runs don't hit the site&lt;/span&gt;
&lt;span class="no"&gt;HTTPCACHE_ENABLED&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;

&lt;span class="c1"&gt;# Export automatically, without command-line flags&lt;/span&gt;
&lt;span class="no"&gt;FEEDS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"output/books.json"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"format"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"json"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"mode"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"overwrite"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"indent"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What each does:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;USER_AGENT&lt;/code&gt; replaces Scrapy's default. An honest, descriptive agent with contact details lets site owners reach you instead of blocking you.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;AutoThrottle&lt;/strong&gt; adjusts the delay between requests based on how fast the server responds, backing off when the site slows down. It never goes below the project's &lt;code&gt;DOWNLOAD_DELAY&lt;/code&gt; of 1 second, so it can only make the crawl gentler than the default.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;HTTPCACHE_ENABLED&lt;/code&gt; stores every response on disk. Re-running the spider while you tweak your selectors then costs the site nothing. Turn it off for real runs, or you'll scrape stale data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;FEEDS&lt;/code&gt; defines the output in settings, with &lt;code&gt;"mode": "overwrite"&lt;/code&gt; replacing the file on each run.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two more settings are handy while testing: &lt;code&gt;CLOSESPIDER_PAGECOUNT&lt;/code&gt; and &lt;code&gt;CLOSESPIDER_ITEMCOUNT&lt;/code&gt; stop the crawl after roughly that many pages or items (requests already in flight still finish). You can pass any setting on the command line with &lt;code&gt;-s&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;scrapy crawl books -s &lt;span class="nv"&gt;CLOSESPIDER_PAGECOUNT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;10 -O sample.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One catch: &lt;code&gt;-O&lt;/code&gt; adds a second output on top of &lt;code&gt;FEEDS&lt;/code&gt;, so this sample run also overwrites &lt;code&gt;output/books.json&lt;/code&gt; with the partial result. Comment out &lt;code&gt;FEEDS&lt;/code&gt; while you experiment if that bothers you.&lt;/p&gt;

&lt;p&gt;The sandbox has 50 listing pages of 20 books each, so a full crawl is about 1,050 requests. With a delay of at least one second each, expect 20 minutes or more, so test with a small sample first.&lt;/p&gt;

&lt;h3&gt;
  
  
  Spider arguments
&lt;/h3&gt;

&lt;p&gt;You can also pass arguments to a spider with &lt;code&gt;-a&lt;/code&gt;. For example, to crawl one category instead of the whole site, accept a start URL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BooksSpider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scrapy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Spider&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"books"&lt;/span&gt;
    &lt;span class="n"&gt;allowed_domains&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"books.toscrape.com"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;start_urls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"https://books.toscrape.com/"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start_url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nb"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;start_url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start_urls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;start_url&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;scrapy crawl books -a &lt;span class="nv"&gt;start_url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;https://books.toscrape.com/catalogue/category/books/travel_2/index.html -O travel.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Reading the Results
&lt;/h2&gt;

&lt;p&gt;When a crawl finishes, Scrapy prints a statistics summary. These lines tell you most of what you need:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Stat&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;item_scraped_count&lt;/td&gt;
&lt;td&gt;Items that made it through the pipelines&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;item_dropped_count&lt;/td&gt;
&lt;td&gt;Items discarded with DropItem&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;downloader/response_status_count/200&lt;/td&gt;
&lt;td&gt;Successful responses (look for 403, 404, 429 here too)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;dupefilter/filtered&lt;/td&gt;
&lt;td&gt;Requests skipped because the URL had been seen&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;robotstxt/forbidden&lt;/td&gt;
&lt;td&gt;Requests blocked by robots.txt&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;finish_reason&lt;/td&gt;
&lt;td&gt;finished for a normal end, or the reason it stopped early&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A stat that never happened isn't printed at all, so no &lt;code&gt;robotstxt/forbidden&lt;/code&gt; line means nothing was blocked. Check these after every run. A crawl that "worked" but dropped half its items, or got a pile of 403 responses, is telling you something.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running a Spider from a Script
&lt;/h2&gt;

&lt;p&gt;Sometimes you want to run a crawl from your own Python code, for example from a scheduler. Put this in a file at the project root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;scrapy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;crawler&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;AsyncCrawlerProcess&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;scrapy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;project&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;get_project_settings&lt;/span&gt;

&lt;span class="n"&gt;process&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AsyncCrawlerProcess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get_project_settings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;crawl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"books"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# blocks until the crawl is finished&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it from the same folder as &lt;code&gt;scrapy.cfg&lt;/code&gt;, so Scrapy finds your project settings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Responsible Scraping with Scrapy
&lt;/h2&gt;

&lt;p&gt;Because Scrapy can send many requests quickly, a few habits matter more here than in a script:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Leave&lt;/strong&gt; &lt;code&gt;ROBOTSTXT_OBEY = True&lt;/code&gt;&lt;strong&gt;.&lt;/strong&gt; Turning it off to get at pages a site has asked crawlers to avoid is the wrong fix for a "Forbidden by robots.txt" message.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Keep the delay and the concurrency low.&lt;/strong&gt; The generated defaults are conservative, and AutoThrottle helps. Don't raise concurrency against a small site.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Identify yourself&lt;/strong&gt; with &lt;code&gt;USER_AGENT&lt;/code&gt;, and respect &lt;code&gt;429 Too Many Requests&lt;/code&gt; and &lt;code&gt;Retry-After&lt;/code&gt; by slowing down, not by retrying harder.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Read the terms of service,&lt;/strong&gt; and avoid personal data unless you have a lawful reason to collect it. Our guide "Is Web Scraping Legal?" covers this in depth.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Troubleshooting
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Symptom&lt;/th&gt;
&lt;th&gt;Likely cause&lt;/th&gt;
&lt;th&gt;What to try&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Spider runs but crawls nothing (Crawled 0 pages)&lt;/td&gt;
&lt;td&gt;Code from an older tutorial defines start_requests(), which was removed in 2.16&lt;/td&gt;
&lt;td&gt;Use start_urls or async def start()&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Not writing ... already exists and an empty export&lt;/td&gt;
&lt;td&gt;-o on an existing file; projects from 2.20 set FEED_MODE = "create"&lt;/td&gt;
&lt;td&gt;Use -O, or set FEED_MODE explicitly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Empty or None fields&lt;/td&gt;
&lt;td&gt;Selector doesn't match&lt;/td&gt;
&lt;td&gt;Test it in scrapy shell&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Filtered offsite request in the log&lt;/td&gt;
&lt;td&gt;The link is outside allowed_domains&lt;/td&gt;
&lt;td&gt;Check the domain, or leave it if it's intended&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Forbidden by robots.txt&lt;/td&gt;
&lt;td&gt;The site disallows that path&lt;/td&gt;
&lt;td&gt;Respect it; don't disable ROBOTSTXT_OBEY&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ModuleNotFoundError: bookscraper&lt;/td&gt;
&lt;td&gt;You're not in the project folder&lt;/td&gt;
&lt;td&gt;Run from the folder containing scrapy.cfg&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lots of 403 or 429 responses&lt;/td&gt;
&lt;td&gt;You're going too fast, or the site refuses your traffic&lt;/td&gt;
&lt;td&gt;Slow down; check the site's rules&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fewer items than expected&lt;/td&gt;
&lt;td&gt;Pipelines dropped some, or the duplicate filter skipped URLs&lt;/td&gt;
&lt;td&gt;Check item_dropped_count and dupefilter/filtered&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For more detail while debugging, run with &lt;code&gt;-s LOG_LEVEL=DEBUG&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where to Go from Here
&lt;/h2&gt;

&lt;p&gt;You now have the core of a real crawler: a spider, items, pipelines, polite settings, and exports. Natural next steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;CrawlSpider&lt;/code&gt; &lt;strong&gt;rules&lt;/strong&gt; let you describe which links to follow declaratively instead of writing the pagination code yourself.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;JavaScript sites&lt;/strong&gt; need a browser. Plugins such as &lt;code&gt;scrapy-playwright&lt;/code&gt; connect Scrapy to Playwright, so you keep the framework's crawling and pipelines.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Storing results in a database&lt;/strong&gt; is a matter of writing another pipeline, for example one that inserts into SQLite or PostgreSQL.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scheduling&lt;/strong&gt; a nightly crawl is easy once your spider runs from a script or a command.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The mental model to remember: &lt;strong&gt;spiders extract, pipelines clean, settings decide how politely you crawl, and Scrapy runs the machinery.&lt;/strong&gt; Once you've written your first spider, the second takes minutes.&lt;/p&gt;

&lt;p&gt;Happy scraping, and please do it politely!&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Found this helpful? Share it with a fellow developer or leave your questions in the comments.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Published via &lt;a href="https://zyvop.com/web-scraping-with-scrapy-building-a-spider-from-scratch-nt0m5?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=syndication" rel="noopener noreferrer"&gt;ZyVOP&lt;/a&gt; — Write once in Markdown, auto-backup to GitHub, and syndicate to Dev.to, Medium &amp;amp; Hashnode in 1 click.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>data</category>
      <category>python</category>
      <category>scrapy</category>
      <category>spiders</category>
    </item>
    <item>
      <title>Web Scraping with Python and BeautifulSoup: A Practical Beginner's Guide</title>
      <dc:creator>Bhavya Arora</dc:creator>
      <pubDate>Sat, 14 Nov 2026 06:00:07 +0000</pubDate>
      <link>https://dev.to/bhavya_arora/web-scraping-with-python-and-beautifulsoup-a-practical-beginners-guide-20jc</link>
      <guid>https://dev.to/bhavya_arora/web-scraping-with-python-and-beautifulsoup-a-practical-beginners-guide-20jc</guid>
      <description>&lt;p&gt;The web is the largest data source in the world, but most of that data is locked inside HTML pages designed for humans, not programs. &lt;strong&gt;Web scraping&lt;/strong&gt; is the technique of fetching those pages and pulling out the pieces you care about: product prices, article headlines, job listings, research data, and more.&lt;/p&gt;

&lt;p&gt;In this guide, you'll learn how to scrape websites with &lt;strong&gt;Python&lt;/strong&gt; and &lt;strong&gt;BeautifulSoup&lt;/strong&gt;, one of the friendliest parsing libraries around. By the end, you'll have built a complete scraper that walks through multiple pages and saves its results to a CSV file.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is Web Scraping?
&lt;/h2&gt;

&lt;p&gt;Web scraping is the automated process of requesting a web page, reading its HTML, and extracting structured data from it. A scraper typically does four things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Fetches&lt;/strong&gt; a page over HTTP.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Parses&lt;/strong&gt; the HTML into a searchable structure.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Extracts&lt;/strong&gt; the elements it needs (text, links, attributes).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stores&lt;/strong&gt; the results in a useful format such as CSV, JSON, or a database.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Python is a popular choice for this because of its readable syntax and an excellent ecosystem of libraries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why BeautifulSoup?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.crummy.com/software/BeautifulSoup/" rel="noopener noreferrer"&gt;BeautifulSoup&lt;/a&gt; (package name &lt;code&gt;beautifulsoup4&lt;/code&gt;) is a library for parsing HTML and XML. It doesn't download pages itself; it focuses on making the parsed document easy to navigate and search. Here's why it's a great starting point:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Forgiving:&lt;/strong&gt; it handles messy, broken HTML that real websites are full of.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Intuitive:&lt;/strong&gt; searching by tag, class, id, or CSS selector reads almost like English.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Lightweight:&lt;/strong&gt; no browser, no heavy setup.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It pairs naturally with the &lt;code&gt;requests&lt;/code&gt; library, which handles the HTTP side.&lt;/p&gt;

&lt;h2&gt;
  
  
  Before You Scrape: Be a Good Citizen
&lt;/h2&gt;

&lt;p&gt;Scraping is powerful, so use it responsibly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Read the site's Terms of Service.&lt;/strong&gt; Some sites prohibit scraping.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Check&lt;/strong&gt; &lt;code&gt;robots.txt&lt;/code&gt;&lt;strong&gt;.&lt;/strong&gt; Found at &lt;code&gt;https://example.com/robots.txt&lt;/code&gt;, it tells crawlers which paths are off limits.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Prefer official APIs&lt;/strong&gt; when they exist. They're more stable and often explicitly permitted.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Don't hammer servers.&lt;/strong&gt; Add delays between requests.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Identify yourself&lt;/strong&gt; with a descriptive &lt;code&gt;User-Agent&lt;/code&gt; header.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Avoid personal or sensitive data,&lt;/strong&gt; and respect copyright and privacy laws that apply to you.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Python can even check &lt;code&gt;robots.txt&lt;/code&gt; for you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;urllib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;robotparser&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;RobotFileParser&lt;/span&gt;

&lt;span class="n"&gt;rp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RobotFileParser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;rp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://example.com/robots.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;rp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;can_fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"MyLearningScraper"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"https://example.com/some/page"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Setting Up
&lt;/h2&gt;

&lt;p&gt;You'll need &lt;strong&gt;Python 3.9 or newer&lt;/strong&gt; (the full scraper below uses built-in generic type hints like &lt;code&gt;list[dict]&lt;/code&gt;). Create a virtual environment and install the two libraries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python -m venv venv
&lt;span class="nb"&gt;source&lt;/span&gt; venv/bin/activate        &lt;span class="c"&gt;# On Windows: venv\Scripts\activate&lt;/span&gt;
pip install requests beautifulsoup4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Your First Scrape
&lt;/h2&gt;

&lt;p&gt;Let's fetch a page and read its title. We'll use &lt;a href="https://books.toscrape.com/" rel="noopener noreferrer"&gt;books.toscrape.com&lt;/a&gt;, a sandbox site built specifically for practicing scraping.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;bs4&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;BeautifulSoup&lt;/span&gt;

&lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"https://books.toscrape.com/"&lt;/span&gt;
&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;raise_for_status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# Raise an error for 4xx/5xx responses&lt;/span&gt;

&lt;span class="n"&gt;soup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BeautifulSoup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"html.parser"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;soup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few notes on what happened:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;requests.get()&lt;/code&gt; downloads the page. Always set a &lt;code&gt;timeout&lt;/code&gt; so your script can't hang forever.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;raise_for_status()&lt;/code&gt; turns bad HTTP responses into exceptions you can handle.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We pass &lt;code&gt;response.content&lt;/code&gt; (raw bytes) so BeautifulSoup can detect the encoding from the page itself. Some servers don't declare a charset in their headers, and in that case &lt;code&gt;requests&lt;/code&gt; falls back to &lt;code&gt;ISO-8859-1&lt;/code&gt; for &lt;code&gt;response.text&lt;/code&gt;, which garbles characters like &lt;code&gt;£&lt;/code&gt; into &lt;code&gt;£&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;"html.parser"&lt;/code&gt; is Python's built-in parser. You can also install &lt;code&gt;lxml&lt;/code&gt; for extra speed.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Understanding the Soup
&lt;/h2&gt;

&lt;p&gt;Once parsed, the document is a tree of objects you can navigate. Here's a tiny example using an HTML string so you can see the basics without any network calls:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;bs4&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;BeautifulSoup&lt;/span&gt;

&lt;span class="n"&gt;html&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"""
&amp;lt;div class="product" id="p1"&amp;gt;
  &amp;lt;h2&amp;gt;Mechanical Keyboard&amp;lt;/h2&amp;gt;
  &amp;lt;span class="price"&amp;gt;$89.99&amp;lt;/span&amp;gt;
  &amp;lt;a href="/products/keyboard"&amp;gt;View details&amp;lt;/a&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class="product" id="p2"&amp;gt;
  &amp;lt;h2&amp;gt;Wireless Mouse&amp;lt;/h2&amp;gt;
  &amp;lt;span class="price"&amp;gt;$24.50&amp;lt;/span&amp;gt;
  &amp;lt;a href="/products/mouse"&amp;gt;View details&amp;lt;/a&amp;gt;
&amp;lt;/div&amp;gt;
"""&lt;/span&gt;

&lt;span class="n"&gt;soup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BeautifulSoup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"html.parser"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Finding elements
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# First matching element&lt;/span&gt;
&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;soup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"div"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;class_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"product"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;# Mechanical Keyboard&lt;/span&gt;

&lt;span class="c1"&gt;# All matching elements&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;soup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"div"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;class_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"product"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"h2"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"span"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;class_&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;code&gt;class&lt;/code&gt; is a reserved word in Python, so BeautifulSoup uses &lt;code&gt;class_&lt;/code&gt; (with an underscore).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Using CSS selectors
&lt;/h3&gt;

&lt;p&gt;If you know CSS, &lt;code&gt;select()&lt;/code&gt; and &lt;code&gt;select_one()&lt;/code&gt; are often cleaner:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# All prices&lt;/span&gt;
&lt;span class="n"&gt;prices&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;el&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;soup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"div.product span.price"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                                &lt;span class="c1"&gt;# ['$89.99', '$24.50']&lt;/span&gt;

&lt;span class="c1"&gt;# One element by id&lt;/span&gt;
&lt;span class="n"&gt;mouse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;soup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#p2 h2"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mouse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="c1"&gt;# Wireless Mouse&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Reading attributes
&lt;/h3&gt;

&lt;p&gt;Elements behave like dictionaries for their attributes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;soup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"href"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                          &lt;span class="c1"&gt;# /products/keyboard&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"href"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                      &lt;span class="c1"&gt;# Same, but returns None if missing&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use &lt;code&gt;.get()&lt;/code&gt; when an attribute might not exist, which prevents a &lt;code&gt;KeyError&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Quick reference
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Task&lt;/th&gt;
&lt;th&gt;Code&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;First match by tag/class&lt;/td&gt;
&lt;td&gt;soup.find("div", class_="x")&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;All matches&lt;/td&gt;
&lt;td&gt;soup.find_all("a")&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CSS selector (one)&lt;/td&gt;
&lt;td&gt;soup.select_one("ul.menu &amp;gt; li")&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CSS selector (many)&lt;/td&gt;
&lt;td&gt;soup.select("a[href]")&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Text of an element&lt;/td&gt;
&lt;td&gt;el.get_text(strip=True)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Attribute value&lt;/td&gt;
&lt;td&gt;el["href"] or el.get("href")&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Parent / siblings&lt;/td&gt;
&lt;td&gt;el.parent, el.find_next_sibling()&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Building a Real Scraper
&lt;/h2&gt;

&lt;p&gt;Now let's put it all together. Our goal: scrape every book on books.toscrape.com, collecting the &lt;strong&gt;title, price, rating, availability, and URL&lt;/strong&gt;, then save everything to a CSV.&lt;/p&gt;

&lt;p&gt;First, inspect the page using your browser's developer tools (right-click, then &lt;strong&gt;Inspect&lt;/strong&gt;). You'll find each book lives inside an &lt;code&gt;&amp;lt;article class="product_pod"&amp;gt;&lt;/code&gt;, with the title in the link inside &lt;code&gt;&amp;lt;h3&amp;gt;&lt;/code&gt;, the price in &lt;code&gt;p.price_color&lt;/code&gt;, and the rating encoded as a class name like &lt;code&gt;star-rating Three&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;urllib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parse&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;urljoin&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;bs4&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;BeautifulSoup&lt;/span&gt;

&lt;span class="no"&gt;BASE_URL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"https://books.toscrape.com/"&lt;/span&gt;
&lt;span class="no"&gt;HEADERS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"User-Agent"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"MyLearningScraper/1.0 (educational project)"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="no"&gt;RATINGS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"One"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Two"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Three"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Four"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Five"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;BeautifulSoup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Download a page and return it as a BeautifulSoup object."""&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;HEADERS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;raise_for_status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;BeautifulSoup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"html.parser"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;parse_book&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;page_url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Extract the fields we care about from one book card."""&lt;/span&gt;
    &lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;
    &lt;span class="n"&gt;rating_word&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"p.star-rating"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"class"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;price_text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"p.price_color"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;availability&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"p.availability"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"[^\d.]"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price_text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"rating"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;RATINGS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rating_word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"in_stock"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"In stock"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;availability&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;urljoin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page_url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"href"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;scrape_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start_url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max_pages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Follow the 'next' links page by page, collecting every book."""&lt;/span&gt;
    &lt;span class="n"&gt;books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start_url&lt;/span&gt;
    &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;max_pages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"Scraping page {page}: {url}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;soup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;soup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"article.product_pod"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;parse_book&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;next_link&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;soup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"li.next a"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;urljoin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;next_link&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"href"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;next_link&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;

        &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="c1"&gt;# Be polite: pause between requests&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;save_to_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Write the scraped rows to a CSV file."""&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Nothing to save."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;

    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"w"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;newline&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;encoding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"utf-8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;writer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DictWriter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fieldnames&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writeheader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writerows&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"Saved {len(rows)} rows to {filename}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"__main__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;scrape_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;BASE_URL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;save_to_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"books.csv"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save it as &lt;code&gt;scraper.py&lt;/code&gt; and run &lt;code&gt;python scraper.py&lt;/code&gt;. The site has 50 pages of 20 books each, and with a one-second pause between requests the crawl takes a minute or two, leaving you with a &lt;code&gt;books.csv&lt;/code&gt; of about 1,000 books.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's worth noticing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Small, focused functions.&lt;/strong&gt; Fetching, parsing, crawling, and saving are separate, so each is easy to test and change.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;urljoin&lt;/code&gt; &lt;strong&gt;for links.&lt;/strong&gt; Pages often use relative links like &lt;code&gt;catalogue/page-2.html&lt;/code&gt;. &lt;code&gt;urljoin&lt;/code&gt; resolves them against the current URL correctly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pagination by following "next".&lt;/strong&gt; Rather than guessing page numbers, the scraper follows the site's own &lt;code&gt;li.next a&lt;/code&gt; link and stops when it disappears.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Politeness built in.&lt;/strong&gt; A custom &lt;code&gt;User-Agent&lt;/code&gt;, a timeout, and a delay between requests.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Making Your Scraper More Robust
&lt;/h2&gt;

&lt;p&gt;Real-world scraping is messy. Here are the issues you'll most likely run into.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handle network errors and retries
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;adapters&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;HTTPAdapter&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;urllib3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;util&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;retry&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Retry&lt;/span&gt;

&lt;span class="n"&gt;session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Session&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;retries&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Retry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;backoff_factor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status_forcelist&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;429&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;502&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;503&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;504&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HTTPAdapter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_retries&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;retries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"User-Agent"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"MyLearningScraper/1.0"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://books.toscrape.com/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;code&gt;Session&lt;/code&gt; reuses connections (faster) and shares headers across requests. The retry adapter automatically retries temporary failures with increasing delays.&lt;/p&gt;

&lt;h3&gt;
  
  
  Guard against missing elements
&lt;/h3&gt;

&lt;p&gt;Pages are inconsistent. If &lt;code&gt;select_one()&lt;/code&gt; finds nothing, it returns &lt;code&gt;None&lt;/code&gt;, and calling &lt;code&gt;.get_text()&lt;/code&gt; on &lt;code&gt;None&lt;/code&gt; crashes your script. Defend against it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;price_el&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"p.price_color"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;price_el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;price_el&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Watch for site changes
&lt;/h3&gt;

&lt;p&gt;Scrapers break when a site's HTML changes. Prefer stable hooks (ids, semantic classes, data attributes) over fragile ones (deeply nested positions like "the third &lt;code&gt;div&lt;/code&gt; inside the second &lt;code&gt;div&lt;/code&gt;"), and log or alert when a page doesn't match your expectations.&lt;/p&gt;

&lt;h2&gt;
  
  
  When BeautifulSoup Isn't Enough
&lt;/h2&gt;

&lt;p&gt;BeautifulSoup only sees the HTML the server sends. Many modern sites load content with JavaScript after the page opens, so the data you see in your browser isn't in the raw HTML. In that case you have a few options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Look for a hidden API.&lt;/strong&gt; Open the Network tab in your browser's developer tools; the page may fetch JSON from an endpoint you can call directly with &lt;code&gt;requests&lt;/code&gt;. This is often the cleanest solution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use a browser automation tool&lt;/strong&gt; like &lt;a href="https://playwright.dev/python/" rel="noopener noreferrer"&gt;Playwright&lt;/a&gt; or Selenium to render the page, then hand the final HTML to BeautifulSoup.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use a full crawling framework&lt;/strong&gt; like &lt;a href="https://scrapy.org/" rel="noopener noreferrer"&gt;Scrapy&lt;/a&gt; when you need to crawl large sites with concurrency, pipelines, and built-in throttling.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;You've now seen the complete scraping workflow: fetch a page with &lt;code&gt;requests&lt;/code&gt;, parse it with BeautifulSoup, locate elements with &lt;code&gt;find&lt;/code&gt;/&lt;code&gt;select&lt;/code&gt;, extract text and attributes, follow pagination, and save the results. The same pattern scales from a ten-line script to a serious data pipeline.&lt;/p&gt;

&lt;p&gt;A few next steps to practice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Scrape another sandbox, such as &lt;a href="https://quotes.toscrape.com/" rel="noopener noreferrer"&gt;quotes.toscrape.com&lt;/a&gt;, and try extracting authors and tags.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Save your data as JSON or load it into a pandas DataFrame for analysis.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add logging and retries to make your scraper production-ready.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy scraping, and remember to scrape responsibly!&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Found this helpful? Share it with a fellow developer or drop your questions in the comments.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Published via &lt;a href="https://zyvop.com/web-scraping-with-python-and-beautifulsoup-a-practical-beginner-s-guide-rivrw?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=syndication" rel="noopener noreferrer"&gt;ZyVOP&lt;/a&gt; — Write once in Markdown, auto-backup to GitHub, and syndicate to Dev.to, Medium &amp;amp; Hashnode in 1 click.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>beautifulsoup</category>
      <category>data</category>
      <category>python</category>
      <category>webscraping</category>
    </item>
    <item>
      <title>Postgres 14 hits end of life on November 12</title>
      <dc:creator>Bhavya Arora</dc:creator>
      <pubDate>Wed, 11 Nov 2026 05:36:17 +0000</pubDate>
      <link>https://dev.to/bhavya_arora/postgres-14-hits-end-of-life-on-november-12-4c34</link>
      <guid>https://dev.to/bhavya_arora/postgres-14-hits-end-of-life-on-november-12-4c34</guid>
      <description>&lt;p&gt;On Thursday, November 12, the PostgreSQL project ships the last release of version 14. After that, 14 gets no more security fixes and no more bug fixes. The &lt;a href="https://www.postgresql.org/support/versioning/" rel="noopener noreferrer"&gt;versioning policy&lt;/a&gt; gives every major version five years, and 14's five years are up.&lt;/p&gt;

&lt;p&gt;Your database won't stop. It will keep running, and every vulnerability found from that day on stays open.&lt;/p&gt;

&lt;p&gt;I ran a real upgrade from 14.24 to 18.6 to see what breaks. Several things did, and &lt;code&gt;pg_upgrade --check&lt;/code&gt; caught only two of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  What stops on the 12th
&lt;/h2&gt;

&lt;p&gt;Version 14 shipped on September 30, 2021. Its final release is scheduled for November 12, 2026. Both the &lt;a href="https://postgresql.org/about/news/postgresql-184-1710-1614-1518-and-1423-released-3297" rel="noopener noreferrer"&gt;May&lt;/a&gt; and &lt;a href="https://www.postgresql.org/about/news/postgresql-186-1711-1615-1519-1424-and-19-beta-3-released-3365/" rel="noopener noreferrer"&gt;August&lt;/a&gt; update announcements carried the same warning.&lt;/p&gt;

&lt;p&gt;The release team also says the November 12 update will &lt;a href="https://www.postgresql.org/about/news/postgresql-19-rc-1-released-3409/" rel="noopener noreferrer"&gt;include a large number of fixes&lt;/a&gt;. That makes the last 14 release worth installing.&lt;/p&gt;

&lt;p&gt;The security load this year explains why it matters. The August 13 update fixed 28 vulnerabilities and more than 110 bugs across the supported branches.&lt;/p&gt;

&lt;p&gt;One of them, CVE-2026-6471, lets logical decoding load an arbitrary file. It &lt;a href="https://www.csoonline.com/article/4218101/decade-old-postgresql-flaw-turns-backup-account-into-a-backdoor.html" rel="noopener noreferrer"&gt;affects every version back to 9.4&lt;/a&gt;, 14 included.&lt;/p&gt;

&lt;p&gt;After November 12, the next flaw like that stays unpatched on 14.&lt;/p&gt;

&lt;h2&gt;
  
  
  Managed services buy time, for a fee
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/AmazonRDS/latest/PostgreSQLReleaseNotes/postgresql-release-calendar.html" rel="noopener noreferrer"&gt;AWS's RDS calendar&lt;/a&gt; lists February 28, 2027 as the end of standard support for major version 14. Extended Support pricing starts on March 1, 2027, and RDS enrolls the database in &lt;a href="https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/extended-support.html" rel="noopener noreferrer"&gt;Extended Support&lt;/a&gt; automatically. The charge is per vCPU-hour on top of the instance price.&lt;/p&gt;

&lt;p&gt;AWS prices Extended Support per vCPU per hour by Region, and the rate doubles in year three, which for version 14 starts on March 1, 2029. Reserved Instance discounts don't apply, standbys and read replicas are billed too, and you need to be on the latest minor version to get it. I couldn't find a published rate for version 14 itself, so run your instance through the AWS Pricing Calculator before you budget.&lt;/p&gt;

&lt;p&gt;Self-hosted clusters get no such cushion. The date is the date.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pick a target: 18
&lt;/h2&gt;

&lt;p&gt;Each major version gets five years. The versioning policy lists these final-release dates:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Version&lt;/th&gt;
&lt;th&gt;Final release&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;November 11, 2027&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;November 9, 2028&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;td&gt;November 8, 2029&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;18&lt;/td&gt;
&lt;td&gt;November 14, 2030&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Jumping to 15 or 16 means doing this again in a year or two. I'd go to 18. It has the longest runway, and &lt;code&gt;pg_upgrade&lt;/code&gt; in 18 &lt;a href="https://www.postgresql.org/docs/release/18/" rel="noopener noreferrer"&gt;keeps your planner statistics&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;PostgreSQL 19 is a release candidate, and the release team &lt;a href="https://www.postgresql.org/about/news/postgresql-19-rc-1-released-3409/" rel="noopener noreferrer"&gt;plans general availability for November 19&lt;/a&gt;, unless an issue forces a delay. That's a week after 14 stops.&lt;/p&gt;

&lt;p&gt;I'd let a new major version collect its first minor releases before putting production on it. That's a judgment call, not a rule.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to move
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNpNkMFq20AQhl9lOufdgyGH4ENLrNhJIQkhbgNFNmEsjaXFq51ld1RXMX73IiU1Pc7M9_P9zAkrqRnnuPdyrFpKCj9uNwHgpnyWrE3iDLMrqHyfldMWrP0Ki1NBAQbpQenAQJBbSQrSKzX87TymF2At_OI88UUZm7c-Nolq3l6uTzIdb8sHaVxFHhJH7ypSJwFUgCDwEWbXF_eYLKbM8vRAqWGoSQkkQUdhAKWd5_yhX_6vX5XWehcOI2ltPlLcXpjPEndlIXGATmqGA3PMoC2D-PqfHFxQqnSLBjtOHbka5yfUlrvxdzXvqfeK5mPzSslNZUZmL0FX1Dk_4BwtxejZ5iErdwYWY61HqtbTvJKgBja45kYYfn7foIEX2YmKgXv2v1ldRQZukiNvIFPINnNyezSTZO3exy6zq_gHz2eDu6YQLwnn-OXYOmU8_wXcsKF6%3Ftype%3Dpng" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNpNkMFq20AQhl9lOufdgyGH4ENLrNhJIQkhbgNFNmEsjaXFq51ld1RXMX73IiU1Pc7M9_P9zAkrqRnnuPdyrFpKCj9uNwHgpnyWrE3iDLMrqHyfldMWrP0Ki1NBAQbpQenAQJBbSQrSKzX87TymF2At_OI88UUZm7c-Nolq3l6uTzIdb8sHaVxFHhJH7ypSJwFUgCDwEWbXF_eYLKbM8vRAqWGoSQkkQUdhAKWd5_yhX_6vX5XWehcOI2ltPlLcXpjPEndlIXGATmqGA3PMoC2D-PqfHFxQqnSLBjtOHbka5yfUlrvxdzXvqfeK5mPzSslNZUZmL0FX1Dk_4BwtxejZ5iErdwYWY61HqtbTvJKgBja45kYYfn7foIEX2YmKgXv2v1ldRQZukiNvIFPINnNyezSTZO3exy6zq_gHz2eDu6YQLwnn-OXYOmU8_wXcsKF6%3Ftype%3Dpng" alt="Mermaid Diagram" width="456" height="880"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pg_upgrade&lt;/code&gt; has three modes that matter. Copy duplicates every data file and leaves the old cluster untouched. &lt;code&gt;--link&lt;/code&gt; hard-links the files instead.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--swap&lt;/code&gt;, available in 18's &lt;code&gt;pg_upgrade&lt;/code&gt;, moves the old data directories into the new cluster, and &lt;a href="https://www.postgresql.org/docs/18/pgupgrade.html" rel="noopener noreferrer"&gt;the docs say&lt;/a&gt; it can beat the other modes on clusters with many relations.&lt;/p&gt;

&lt;p&gt;Logical replication gets you close to zero downtime, but there's a catch for 14 sources. &lt;code&gt;pg_upgrade&lt;/code&gt; only migrates logical slots &lt;a href="https://www.postgresql.org/docs/17/pgupgrade.html" rel="noopener noreferrer"&gt;when the old cluster is 17 or newer&lt;/a&gt;, and subscription state from older clusters is silently ignored. If you run logical replication today, plan the re-sync.&lt;/p&gt;

&lt;h3&gt;
  
  
  What the three modes cost on a real cluster
&lt;/h3&gt;

&lt;p&gt;Test setup: PostgreSQL 14.24 and 18.6 (conda-forge builds), Linux x86_64, one vCPU, 3 GB RAM, UTF-8. The database held a 5M-row &lt;code&gt;orders&lt;/code&gt; table, a 3M-row &lt;code&gt;events&lt;/code&gt; table with JSONB payloads, 500k customers and 2,000 small tenant tables. That's 2,003 tables and 1,334 MB.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Mode&lt;/th&gt;
&lt;th&gt;Four runs&lt;/th&gt;
&lt;th&gt;Range&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;copy&lt;/td&gt;
&lt;td&gt;9.5, 9.8, 12.3, 11.2 s&lt;/td&gt;
&lt;td&gt;9.5 to 12.3 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;link&lt;/td&gt;
&lt;td&gt;7.6, 7.9, 7.9, 7.7 s&lt;/td&gt;
&lt;td&gt;7.6 to 7.9 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;swap&lt;/td&gt;
&lt;td&gt;6.7, 8.1, 8.7, 8.8 s&lt;/td&gt;
&lt;td&gt;6.7 to 8.8 s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Everything finished in under 13 seconds. Link and swap skip the file copy, so their lead grows with data size, but I only measured 1.3 GB.&lt;/p&gt;

&lt;p&gt;My 2,003 tables didn't separate link from swap. Treat these numbers as a floor for a small, warm-cache database and rehearse on your own data.&lt;/p&gt;

&lt;h2&gt;
  
  
  What broke in my test
&lt;/h2&gt;

&lt;p&gt;Seven things behave differently on 18 than they did on 14. Each one below comes from a real 14.24 to 18.6 run.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Checksums are on by default now
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://www.postgresql.org/docs/release/18/" rel="noopener noreferrer"&gt;18 release notes&lt;/a&gt; say &lt;code&gt;initdb&lt;/code&gt; enables data checksums by default, and &lt;code&gt;pg_upgrade&lt;/code&gt; needs both clusters to match. A default 14 cluster has them off. &lt;code&gt;--check&lt;/code&gt; catches it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;old cluster does not use data checksums but the new one does
Failure, exiting
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are two fixes, and I tested both. Create the new cluster with &lt;code&gt;initdb --no-data-checksums&lt;/code&gt;, and &lt;code&gt;--check&lt;/code&gt; prints &lt;code&gt;*Clusters are compatible*&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The other fix is to enable checksums on the old cluster first. With the old cluster shut down cleanly, &lt;code&gt;pg_checksums --enable&lt;/code&gt; from the 14 binaries scanned 173,865 blocks in 2.1 seconds on my 1.3 GB database. The upgrade into a default 18 cluster then finished, and &lt;code&gt;SHOW data_checksums&lt;/code&gt; returned &lt;code&gt;on&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The second fix is the better end state, but it needs the old cluster offline while every block is rewritten. That time grows with cluster size.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Expression indexes that rely on &lt;code&gt;search_path&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This one passes &lt;code&gt;--check&lt;/code&gt; and fails the real run. Since 17, &lt;code&gt;CREATE INDEX&lt;/code&gt;, &lt;code&gt;REINDEX&lt;/code&gt;, &lt;code&gt;ANALYZE&lt;/code&gt;, &lt;code&gt;VACUUM&lt;/code&gt; and the materialized view commands run with a safe &lt;code&gt;search_path&lt;/code&gt;. The &lt;a href="https://www.postgresql.org/docs/release/" rel="noopener noreferrer"&gt;17 release notes&lt;/a&gt; say functions used by expression indexes and materialized views must set their own search path if they reference non-default schemas.&lt;/p&gt;

&lt;p&gt;Here's the setup that works on 14:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;SCHEMA&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;search_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;public&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;rates&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;factor&lt;/span&gt; &lt;span class="kt"&gt;numeric&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;FUNCTION&lt;/span&gt; &lt;span class="nf"&gt;to_usd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="kt"&gt;numeric&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;RETURNS&lt;/span&gt; &lt;span class="kt"&gt;numeric&lt;/span&gt;
  &lt;span class="no"&gt;LANGUAGE&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt; &lt;span class="no"&gt;IMMUTABLE&lt;/span&gt;
  &lt;span class="k"&gt;AS&lt;/span&gt; $$ &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;factor&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;rates&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt; $$&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="kt"&gt;numeric&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_usd&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;to_usd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;pg_upgrade --check&lt;/code&gt; printed &lt;code&gt;*Clusters are compatible*&lt;/code&gt;. The real run stopped at "Restoring database schemas in the new cluster". The log named the index:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pg_restore: from TOC entry 2400; 1259 16412 INDEX orders_usd postgres
pg_restore: error: could not execute query: ERROR:  relation "rates" does not exist
CONTEXT:  SQL function "to_usd" during inlining
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fix is one line, run on the old cluster:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;FUNCTION&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_usd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;numeric&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;search_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that the upgrade completed, and &lt;code&gt;REINDEX&lt;/code&gt; and &lt;code&gt;REFRESH MATERIALIZED VIEW&lt;/code&gt; both ran clean on 18. The &lt;code&gt;pg14-eol-preflight.sql&lt;/code&gt; script further down finds every index and materialized view that depends on such a function before you upgrade.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Removed settings stop the server from starting
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;pg_upgrade&lt;/code&gt; doesn't copy &lt;code&gt;postgresql.conf&lt;/code&gt;. If you carry your old file over, 18 may refuse to boot. I tested each of the four:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setting&lt;/th&gt;
&lt;th&gt;Removed in&lt;/th&gt;
&lt;th&gt;Error on 18&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;vacuum_defer_cleanup_age&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;FATAL: unrecognized configuration parameter&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;promote_trigger_file&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;same&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;old_snapshot_threshold&lt;/td&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;td&gt;same&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;db_user_namespace&lt;/td&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;td&gt;same&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The &lt;a href="https://www.postgresql.org/docs/16/release-16.html" rel="noopener noreferrer"&gt;16&lt;/a&gt; and 17 release notes list all four. Build the new config from 18's defaults and add your changes by hand.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. &lt;code&gt;adminpack&lt;/code&gt; is gone
&lt;/h3&gt;

&lt;p&gt;The extension was removed in 17. With it installed in the old cluster, &lt;code&gt;--check&lt;/code&gt; fails at "Checking for presence of required libraries" and writes &lt;code&gt;could not load library "$libdir/adminpack"&lt;/code&gt; to &lt;code&gt;loadable_libraries.txt&lt;/code&gt;. Drop the extension on 14 first.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Backup scripts and a numeric literal
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://www.postgresql.org/docs/18/release-15.html" rel="noopener noreferrer"&gt;15 release notes&lt;/a&gt; removed exclusive backup mode. On 18, &lt;code&gt;SELECT pg_start_backup('x')&lt;/code&gt; fails with &lt;code&gt;function pg_start_backup(unknown) does not exist&lt;/code&gt;. The replacements are &lt;code&gt;pg_backup_start()&lt;/code&gt; and &lt;code&gt;pg_backup_stop()&lt;/code&gt;, and &lt;code&gt;pg_backup_start('x')&lt;/code&gt; worked on my cluster.&lt;/p&gt;

&lt;p&gt;The same release stopped accepting numeric literals with trailing characters. &lt;code&gt;SELECT 123abc&lt;/code&gt; returns &lt;code&gt;123&lt;/code&gt; on 14, where &lt;code&gt;abc&lt;/code&gt; becomes a column alias. On 18 it fails with &lt;code&gt;trailing junk after numeric literal&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. New databases inherit an old permission
&lt;/h3&gt;

&lt;p&gt;Since 15, new databases don't let ordinary roles create tables in the &lt;code&gt;public&lt;/code&gt; schema. The release notes say upgrading &lt;a href="https://www.postgresql.org/docs/18/release-15.html" rel="noopener noreferrer"&gt;preserves the existing permissions&lt;/a&gt;. Here's what that means in practice.&lt;/p&gt;

&lt;p&gt;After &lt;code&gt;pg_upgrade&lt;/code&gt;, &lt;code&gt;template1&lt;/code&gt; kept the old grant, and a database I created afterward let a non-owner role create tables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;upgraded cluster, template1:   {postgres=UC/postgres,=UC/postgres}
fresh 18 cluster, template1:   {pg_database_owner=UC/pg_database_owner,=U/pg_database_owner}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the fresh 18 cluster the same role got &lt;code&gt;permission denied for schema public&lt;/code&gt;. On the upgraded cluster it got &lt;code&gt;CREATE TABLE&lt;/code&gt;. If you want the tighter default, revoke &lt;code&gt;CREATE&lt;/code&gt; on &lt;code&gt;public&lt;/code&gt; from &lt;code&gt;PUBLIC&lt;/code&gt; in &lt;code&gt;template1&lt;/code&gt; and in each existing database, after you've confirmed your applications don't rely on it.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. MD5 passwords now warn
&lt;/h3&gt;

&lt;p&gt;Setting an MD5 password on 18 prints this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WARNING:  setting an MD5-encrypted password
DETAIL:  MD5 password support is deprecated and will be removed in a future release of PostgreSQL.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My MD5 role came through the upgrade intact. The release notes say support will be removed in a future major version, and that &lt;code&gt;md5_password_warnings = off&lt;/code&gt; silences the warning. I'd move those roles to SCRAM now instead of hiding it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Documented changes I didn't test
&lt;/h3&gt;

&lt;p&gt;The release notes also list these. They're worth a grep through your code and monitoring:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A role with &lt;code&gt;CREATEROLE&lt;/code&gt; can no longer modify arbitrary non-superuser roles (16).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;pg_stat_bgwriter&lt;/code&gt; lost &lt;code&gt;buffers_backend&lt;/code&gt; and &lt;code&gt;buffers_backend_fsync&lt;/code&gt;, and &lt;code&gt;pg_stat_statements&lt;/code&gt; renamed &lt;code&gt;blk_read_time&lt;/code&gt; and &lt;code&gt;blk_write_time&lt;/code&gt; to &lt;code&gt;shared_blk_read_time&lt;/code&gt; and &lt;code&gt;shared_blk_write_time&lt;/code&gt; (17).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;plpython2u&lt;/code&gt; and &lt;code&gt;plpythonu&lt;/code&gt; were removed (15).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;VACUUM&lt;/code&gt; and &lt;code&gt;ANALYZE&lt;/code&gt; now process inheritance children unless you pass &lt;code&gt;ONLY&lt;/code&gt;. Unlogged partitioned tables are disallowed. &lt;code&gt;AFTER&lt;/code&gt; triggers run as the role active when the event was queued (18).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  After the upgrade
&lt;/h2&gt;

&lt;p&gt;Version 18 carries planner statistics across. On my test, &lt;code&gt;pg_stats&lt;/code&gt; held 6,012 rows for the &lt;code&gt;public&lt;/code&gt; schema before the upgrade and 6,012 after.&lt;/p&gt;

&lt;p&gt;The cumulative counters don't carry over. Right after the upgrade, &lt;code&gt;n_live_tup&lt;/code&gt; for &lt;code&gt;orders&lt;/code&gt; was 0 and &lt;code&gt;last_analyze&lt;/code&gt; was empty. Run the two commands &lt;code&gt;pg_upgrade&lt;/code&gt; prints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;vacuumdb -U postgres --all --analyze-in-stages --missing-stats-only
vacuumdb -U postgres --all --analyze-only
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first took 0.1 seconds on my cluster, because nothing was missing. The second took 1.6 seconds, and afterward &lt;code&gt;n_live_tup&lt;/code&gt; read about 5,000,072 for &lt;code&gt;orders&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  A runbook
&lt;/h2&gt;

&lt;p&gt;Both scripts are printed in full below the runbook. &lt;code&gt;pg14-eol-preflight.sql&lt;/code&gt; is the set of read-only checks from above. &lt;code&gt;pg-upgrade-14-to-18.sh&lt;/code&gt; wraps &lt;code&gt;pg_upgrade&lt;/code&gt; and matches the checksum setting for you.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Run &lt;code&gt;pg14-eol-preflight.sql&lt;/code&gt; in every database on the 14 cluster. Checks 1 to 4 should come back empty.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fix what it finds. Pin function search paths, drop &lt;code&gt;adminpack&lt;/code&gt;, remove the dead settings, and list the roles with MD5 hashes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Restore last night's backup somewhere else and rehearse the whole upgrade on it. &lt;code&gt;--check&lt;/code&gt; passed on the search path case, and only the real run showed the failure.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install 18 beside 14. Stop 14 cleanly with &lt;code&gt;pg_ctl stop -m fast&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the wrapper in check-only mode, then for real:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;OLD_BIN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/path/to/14/bin  &lt;span class="nv"&gt;NEW_BIN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/path/to/18/bin
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;OLD_DATA&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/path/to/14/data &lt;span class="nv"&gt;NEW_DATA&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/path/to/18/data
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;INITDB_ARGS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"-E UTF8 --locale=C.UTF-8"&lt;/span&gt;   &lt;span class="c"&gt;# must match the old cluster&lt;/span&gt;

./pg-upgrade-14-to-18.sh                  &lt;span class="c"&gt;# check only, the default&lt;/span&gt;
&lt;span class="nv"&gt;RUN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1 &lt;span class="nv"&gt;MODE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;link ./pg-upgrade-14-to-18.sh  &lt;span class="c"&gt;# real upgrade; MODE is copy, link or swap&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I ran it in check-only mode and in link and swap mode on the test cluster. It reads the old cluster's checksum setting from &lt;code&gt;pg_controldata&lt;/code&gt; and creates the new cluster to match. Set &lt;code&gt;INITDB_ARGS&lt;/code&gt; so the encoding and locale match your old cluster, or &lt;code&gt;pg_upgrade&lt;/code&gt; will refuse.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Start 18 with a config built from its defaults. Run the two &lt;code&gt;vacuumdb&lt;/code&gt; commands, then your smoke tests.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Keep the old cluster until you're confident. In link and swap mode the old cluster becomes unsafe to start once the new one runs, so take a backup first.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The preflight check: pg14-eol-preflight.sql
&lt;/h2&gt;

&lt;p&gt;Run it as a superuser in every database on the 14 cluster. Every check is read-only.&lt;/p&gt;

&lt;p&gt;Checks 1 to 4 should return no rows before you upgrade. Check 5 lists roles to move to SCRAM, check 6 tells you which &lt;code&gt;initdb&lt;/code&gt; flag to use, and check 7 should return 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- pg14-eol-preflight.sql&lt;/span&gt;
&lt;span class="c1"&gt;-- Read-only checks to run in EVERY database of a PostgreSQL 14 cluster&lt;/span&gt;
&lt;span class="c1"&gt;-- before pg_upgrade to 17 or 18. Run as a superuser:&lt;/span&gt;
&lt;span class="c1"&gt;--   psql -d yourdb -f pg14-eol-preflight.sql&lt;/span&gt;
&lt;span class="c1"&gt;-- Checks 1-4 should return zero rows before you upgrade. Check 5 lists roles to&lt;/span&gt;
&lt;span class="c1"&gt;-- move to SCRAM, check 6 shows the checksum setting, and check 7 should return 0.&lt;/span&gt;

\&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'== 1. Indexes that call SQL/PL/pgSQL functions with no search_path set =='&lt;/span&gt;
&lt;span class="c1"&gt;-- PostgreSQL 17 runs maintenance operations (CREATE INDEX, REINDEX, ANALYZE,&lt;/span&gt;
&lt;span class="c1"&gt;-- VACUUM, REFRESH MATERIALIZED VIEW) with a safe search_path. pg_upgrade --check&lt;/span&gt;
&lt;span class="c1"&gt;-- does not catch these; the schema restore does.&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexrelid&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;index_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regprocedure&lt;/span&gt;    &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;function_name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_index&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pg_depend&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;classid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'pg_class'&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt;
                &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexrelid&lt;/span&gt;
                &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refclassid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'pg_proc'&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pg_proc&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refobjid&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pg_language&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prolang&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lanname&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'sql'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'plpgsql'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nb"&gt;unnest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;proconfig&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;cfg&lt;/span&gt;
                  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;cfg&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'search_path=%'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

\&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'== 2. Materialized views that call SQL/PL/pgSQL functions with no search_path set =='&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;matview_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regprocedure&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;function_name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_class&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pg_rewrite&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ev_class&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pg_depend&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;classid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'pg_rewrite'&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt;
                &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt;
                &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refclassid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'pg_proc'&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pg_proc&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refobjid&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pg_language&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prolang&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;relkind&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'m'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lanname&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'sql'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'plpgsql'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nb"&gt;unnest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;proconfig&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;cfg&lt;/span&gt;
                  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;cfg&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'search_path=%'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

\&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'== 3. Extensions and languages that no longer exist in 17/18 or 15+ =='&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;extname&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;removed_extension&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_extension&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;extname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'adminpack'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;lanname&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;removed_language&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_language&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;lanname&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'plpythonu'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'plpython2u'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

\&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'== 4. Settings that were removed in 15, 16 or 17 and are set explicitly =='&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setting&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_settings&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'vacuum_defer_cleanup_age'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'promote_trigger_file'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
               &lt;span class="s1"&gt;'old_snapshot_threshold'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'db_user_namespace'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'default'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

\&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'== 5. Roles still using MD5 password hashes (deprecated in 18) =='&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;rolname&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;md5_role&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_authid&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;rolpassword&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'md5%'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

\&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'== 6. Data checksums on this cluster (decides the initdb flag for 18) =='&lt;/span&gt;
&lt;span class="no"&gt;SHOW&lt;/span&gt; &lt;span class="n"&gt;data_checksums&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

\&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'== 7. Prepared transactions (pg_upgrade refuses to run with any) =='&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS prepared_transactions FROM pg_prepared_xacts;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It only sees direct dependencies. A function that calls another function without a search path can still slip through, so review those by hand.&lt;/p&gt;

&lt;h2&gt;
  
  
  The upgrade wrapper: pg-upgrade-14-to-18.sh
&lt;/h2&gt;

&lt;p&gt;Save it, run &lt;code&gt;chmod +x pg-upgrade-14-to-18.sh&lt;/code&gt;, and set the environment variables from the runbook. It does nothing destructive until you pass &lt;code&gt;RUN=1&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class="c"&gt;# pg-upgrade-14-to-18.sh&lt;/span&gt;
&lt;span class="c"&gt;# Thin wrapper around pg_upgrade for a PostgreSQL 14 -&amp;gt; 18 jump.&lt;/span&gt;
&lt;span class="c"&gt;# It matches the checksum setting for you, runs pg_upgrade --check first,&lt;/span&gt;
&lt;span class="c"&gt;# and only does the real upgrade when RUN=1.&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="c"&gt;# Required environment:&lt;/span&gt;
&lt;span class="c"&gt;#   OLD_BIN   bin directory of PostgreSQL 14 (pg_upgrade, pg_controldata)&lt;/span&gt;
&lt;span class="c"&gt;#   NEW_BIN   bin directory of PostgreSQL 18&lt;/span&gt;
&lt;span class="c"&gt;#   OLD_DATA  data directory of the old cluster (cleanly shut down)&lt;/span&gt;
&lt;span class="c"&gt;#   NEW_DATA  data directory for the new cluster (must not exist yet)&lt;/span&gt;
&lt;span class="c"&gt;# Optional:&lt;/span&gt;
&lt;span class="c"&gt;#   MODE         copy (default), link or swap&lt;/span&gt;
&lt;span class="c"&gt;#   RUN          set to 1 to run the real upgrade after a clean --check&lt;/span&gt;
&lt;span class="c"&gt;#   INITDB_ARGS  extra initdb flags; encoding and locale must match the old&lt;/span&gt;
&lt;span class="c"&gt;#                cluster, e.g. "-E UTF8 --locale=en_US.UTF-8"&lt;/span&gt;
&lt;span class="c"&gt;#   PGSUPERUSER  bootstrap superuser of the old cluster (default: postgres)&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; -euo pipefail

: &lt;span class="s2"&gt;"${OLD_BIN:?set OLD_BIN}"&lt;/span&gt; &lt;span class="s2"&gt;"${NEW_BIN:?set NEW_BIN}"&lt;/span&gt;
: &lt;span class="s2"&gt;"${OLD_DATA:?set OLD_DATA}"&lt;/span&gt; &lt;span class="s2"&gt;"${NEW_DATA:?set NEW_DATA}"&lt;/span&gt;
&lt;span class="nv"&gt;MODE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"${MODE:-copy}"&lt;/span&gt;
&lt;span class="nv"&gt;SUPERUSER&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"${PGSUPERUSER:-postgres}"&lt;/span&gt;
&lt;span class="nv"&gt;INITDB_ARGS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"${INITDB_ARGS:-}"&lt;/span&gt;

&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"$MODE"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
  copy&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;FLAG&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt; &lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  link&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;FLAG&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"--link"&lt;/span&gt; &lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  swap&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;FLAG&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"--swap"&lt;/span&gt; &lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  *&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"MODE must be copy, link or swap"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;2&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;exit&lt;/span&gt; 2 &lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;esac&lt;/span&gt;

&lt;span class="o"&gt;[&lt;/span&gt; -e &lt;span class="s2"&gt;"$NEW_DATA"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"NEW_DATA already exists: $NEW_DATA"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;2&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;exit&lt;/span&gt; 2&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;&lt;span class="s2"&gt;"$OLD_BIN/pg_controldata"&lt;/span&gt; &lt;span class="s2"&gt;"$OLD_DATA"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; sed -n &lt;span class="s1"&gt;'s/^Database cluster state: *//p'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$state"&lt;/span&gt; != &lt;span class="s2"&gt;"shut down"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Old cluster state is '$state'. Stop it cleanly first (pg_ctl stop -m fast)."&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;2
  &lt;span class="nb"&gt;exit&lt;/span&gt; 1
&lt;span class="k"&gt;fi&lt;/span&gt;

&lt;span class="nv"&gt;old_sums&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;&lt;span class="s2"&gt;"$OLD_BIN/pg_controldata"&lt;/span&gt; &lt;span class="s2"&gt;"$OLD_DATA"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; sed -n &lt;span class="s1"&gt;'s/^Data page checksum version: *//p'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$old_sums"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"0"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
  &lt;span class="nv"&gt;SUMS_FLAG&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"--no-data-checksums"&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Old cluster has data checksums OFF -&amp;gt; initdb 18 with --no-data-checksums"&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="nv"&gt;SUMS_FLAG&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Old cluster has data checksums ON -&amp;gt; initdb 18 with its default (on)"&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;

&lt;span class="c"&gt;# shellcheck disable=SC2086&lt;/span&gt;
&lt;span class="s2"&gt;"$NEW_BIN/initdb"&lt;/span&gt; -D &lt;span class="s2"&gt;"$NEW_DATA"&lt;/span&gt; -U &lt;span class="s2"&gt;"$SUPERUSER"&lt;/span&gt; &lt;span class="nv"&gt;$SUMS_FLAG&lt;/span&gt; &lt;span class="nv"&gt;$INITDB_ARGS&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Created new cluster at $NEW_DATA"&lt;/span&gt;

&lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="s2"&gt;"$(dirname "&lt;/span&gt;&lt;span class="nv"&gt;$NEW_DATA&lt;/span&gt;&lt;span class="s2"&gt;")"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Running pg_upgrade --check ..."&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; ! &lt;span class="s2"&gt;"$NEW_BIN/pg_upgrade"&lt;/span&gt; -b &lt;span class="s2"&gt;"$OLD_BIN"&lt;/span&gt; -B &lt;span class="s2"&gt;"$NEW_BIN"&lt;/span&gt; -d &lt;span class="s2"&gt;"$OLD_DATA"&lt;/span&gt; -D &lt;span class="s2"&gt;"$NEW_DATA"&lt;/span&gt; \
      -U &lt;span class="s2"&gt;"$SUPERUSER"&lt;/span&gt; --check&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"pg_upgrade --check failed. Details: $NEW_DATA/pg_upgrade_output.d"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;2
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Fix the items above, delete $NEW_DATA and rerun."&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;2
  &lt;span class="nb"&gt;exit&lt;/span&gt; 1
&lt;span class="k"&gt;fi&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"${RUN:-0}"&lt;/span&gt; != &lt;span class="s2"&gt;"1"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Check passed. --check does not catch everything (see pg14-eol-preflight.sql)."&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Rehearse on a restored copy of production, then rerun with RUN=1."&lt;/span&gt;
  &lt;span class="nb"&gt;exit&lt;/span&gt; 0
&lt;span class="k"&gt;fi&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Running the real upgrade in $MODE mode ..."&lt;/span&gt;
&lt;span class="nv"&gt;start&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;date +%s&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="c"&gt;# shellcheck disable=SC2086&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; ! &lt;span class="s2"&gt;"$NEW_BIN/pg_upgrade"&lt;/span&gt; -b &lt;span class="s2"&gt;"$OLD_BIN"&lt;/span&gt; -B &lt;span class="s2"&gt;"$NEW_BIN"&lt;/span&gt; -d &lt;span class="s2"&gt;"$OLD_DATA"&lt;/span&gt; -D &lt;span class="s2"&gt;"$NEW_DATA"&lt;/span&gt; \
      -U &lt;span class="s2"&gt;"$SUPERUSER"&lt;/span&gt; &lt;span class="nv"&gt;$FLAG&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"pg_upgrade failed. Read the logs in $NEW_DATA/pg_upgrade_output.d before deleting anything."&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;2
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"In copy mode the old cluster is untouched. In link or swap mode, read the docs' revert section first."&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;2
  &lt;span class="nb"&gt;exit&lt;/span&gt; 1
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"pg_upgrade finished in $(( $(date +%s) - start )) s"&lt;/span&gt;
cat &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;MSG

Next steps:
  1. Start the new cluster and point your config at it.
  2. vacuumdb -U &lt;span class="nv"&gt;$SUPERUSER&lt;/span&gt; --all --analyze-in-stages --missing-stats-only
  3. vacuumdb -U &lt;span class="nv"&gt;$SUPERUSER&lt;/span&gt; --all --analyze-only
  4. If the old cluster was not used &lt;span class="k"&gt;in&lt;/span&gt; link or swap mode, ./delete_old_cluster.sh removes it.
MSG
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  If you can't make the date
&lt;/h2&gt;

&lt;p&gt;Install the final 14 minor release the day it ships, since the release team expects it to be a large batch of fixes. Keep the instance off public networks and tighten who can connect. Put a firm upgrade date on the calendar, because an unpatched database collects risk every week it runs.&lt;/p&gt;

&lt;p&gt;On RDS you have until February 28, 2027 before Extended Support billing starts. Other managed services keep their own calendars, so check yours.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Will Postgres 14 stop working on November 12?&lt;/strong&gt; No. It keeps running. The project stops shipping fixes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can I upgrade straight from 14 to 18?&lt;/strong&gt; Yes. &lt;code&gt;pg_upgrade&lt;/code&gt; supports it, and I did it with 14.24 and 18.6. Read the seven items above first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Should I wait for 19?&lt;/strong&gt; General availability is planned for November 19, after 14 stops. I'd put production on 18 now and revisit 19 once it has had a few minor releases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test notes
&lt;/h2&gt;

&lt;p&gt;Tested with PostgreSQL 14.24 and 18.6 from conda-forge on Linux x86_64 with one vCPU and 3 GB RAM, &lt;code&gt;fsync&lt;/code&gt; off, one run per trap and four runs per mode. Times come from a small, warm-cache database and won't match production. I didn't test replication setups, extensions such as PostGIS, distro wrappers like &lt;code&gt;pg_upgradecluster&lt;/code&gt;, or any managed service.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Published via &lt;a href="https://zyvop.com/postgres-14-hits-end-of-life-on-november-12-r5hhk?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=syndication" rel="noopener noreferrer"&gt;ZyVOP&lt;/a&gt; — Write once in Markdown, auto-backup to GitHub, and syndicate to Dev.to, Medium &amp;amp; Hashnode in 1 click.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>databaseupgrade</category>
      <category>endoflife</category>
      <category>pgupgrade</category>
      <category>postgres18</category>
    </item>
    <item>
      <title>The 15 Git Commands That'll Save Your Sanity (and Your Code)</title>
      <dc:creator>Bhavya Arora</dc:creator>
      <pubDate>Wed, 02 Sep 2026 07:31:06 +0000</pubDate>
      <link>https://dev.to/bhavya_arora/the-15-git-commands-thatll-save-your-sanity-and-your-code-m5e</link>
      <guid>https://dev.to/bhavya_arora/the-15-git-commands-thatll-save-your-sanity-and-your-code-m5e</guid>
      <description>&lt;p&gt;Most developers don't learn Git by reading the manual. They learn it the first time they delete something important, commit the wrong files, or discover that the branch they thought they were working on was not the branch they were actually working on.&lt;/p&gt;

&lt;p&gt;That is not necessarily a bad way to learn. Git becomes much easier once you've seen what these commands are trying to protect you from.&lt;/p&gt;

&lt;p&gt;The problem is that Git has accumulated a reputation for being complicated because there are dozens of commands, countless flags, and a lot of terminology that makes perfect sense once you already understand Git. When you're new to it, though, &lt;code&gt;add&lt;/code&gt;, &lt;code&gt;commit&lt;/code&gt;, &lt;code&gt;push&lt;/code&gt;, &lt;code&gt;pull&lt;/code&gt;, &lt;code&gt;reset&lt;/code&gt;, &lt;code&gt;restore&lt;/code&gt;, and &lt;code&gt;revert&lt;/code&gt; can look like variations of the same thing.&lt;/p&gt;

&lt;p&gt;They aren't.&lt;/p&gt;

&lt;p&gt;This guide focuses on the commands you'll actually use in day-to-day development and, more importantly, the situations that tell you which one to reach for. You don't need to memorize every Git flag to be productive. You need a good mental model, a handful of reliable commands, and enough understanding to avoid turning a small mistake into a much bigger one.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Git Mental Model
&lt;/h2&gt;

&lt;p&gt;Before getting into the commands, it helps to understand the four places your code can exist during normal Git work.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNpNzkFLw0AQBeC_Ms55cxA85SA0LUVBL03VQ-phkkySpbuZsDu11ib_XVJL8fgeH7x3xkpqxhQbJ8eqo6CwXe16AIBF8SFhb_sWtoH581pCkjyOrVWguh4hK3KldjaLwHQ12c1U4r3VEZbFi1TkYMODRKsSTle6vNHhELsRVsWGvSj_l2jQc_Bka0zPqB37-W_NDR2covlr3ilYKh3H2TTS65q8dSdMMaFhcJzEU1T2BjJn-_0rVfklr6VXAzvMuRWGt-cdGthIKSoGnth9sdqKDCyCJWcgUh-TyME2aC4juf2Zv9w_DN84TQbLdilOAqZ4d-ysMk6_2-l2Rg%3Ftype%3Dpng" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNpNzkFLw0AQBeC_Ms55cxA85SA0LUVBL03VQ-phkkySpbuZsDu11ib_XVJL8fgeH7x3xkpqxhQbJ8eqo6CwXe16AIBF8SFhb_sWtoH581pCkjyOrVWguh4hK3KldjaLwHQ12c1U4r3VEZbFi1TkYMODRKsSTle6vNHhELsRVsWGvSj_l2jQc_Bka0zPqB37-W_NDR2covlr3ilYKh3H2TTS65q8dSdMMaFhcJzEU1T2BjJn-_0rVfklr6VXAzvMuRWGt-cdGthIKSoGnth9sdqKDCyCJWcgUh-TyME2aC4juf2Zv9w_DN84TQbLdilOAqZ4d-ysMk6_2-l2Rg%3Ftype%3Dpng" alt="Mermaid Diagram" width="196" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your &lt;strong&gt;working tree&lt;/strong&gt; is where you actually edit files. The &lt;strong&gt;staging area&lt;/strong&gt; is where you decide which of those changes belong in your next commit. A &lt;strong&gt;commit&lt;/strong&gt; records that staged state in your local repository. The &lt;strong&gt;remote repository&lt;/strong&gt; — often hosted on GitHub, GitLab, or another server — is where you share those commits with other people or systems.&lt;/p&gt;

&lt;p&gt;Most of the Git commands in this article are really just ways of inspecting, moving, combining, or undoing changes between those states.&lt;/p&gt;

&lt;p&gt;Once that clicks, Git stops feeling like a collection of unrelated commands.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. &lt;code&gt;git init&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;When you're starting a project from scratch and want Git to track its history, you create a repository with &lt;code&gt;git init&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Git creates a hidden &lt;code&gt;.git&lt;/code&gt; directory inside the project. That directory is where Git keeps the information it needs to manage the repository: commits, references, configuration, and the other internal data that makes version control possible.&lt;/p&gt;

&lt;p&gt;It is worth separating two ideas here because beginners often conflate them. Running &lt;code&gt;git init&lt;/code&gt; creates the repository; it does &lt;strong&gt;not&lt;/strong&gt; automatically add every file in the directory to version control. Your files are still untracked until you stage them.&lt;/p&gt;

&lt;p&gt;A typical first setup therefore looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git init
git add &lt;span class="nb"&gt;.&lt;/span&gt;
git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Initial commit"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also ask Git to create the project directory for you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git init my-new-project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You generally only need to initialize a repository once. When you're not sure whether you're already inside one, &lt;code&gt;git status&lt;/code&gt; is a safer check than running &lt;code&gt;git init again&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. &lt;code&gt;git clone&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;git clone&lt;/code&gt; is the starting point for an existing project. Instead of creating a new repository, you're making a local copy of one that already exists somewhere else.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/username/repository.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A normal clone gives you the project's files and repository history and sets up information about the remote repository so you can fetch and push changes later. It is the command you'll usually run when joining a team, contributing to an open-source project, or moving an existing repository onto a new machine.&lt;/p&gt;

&lt;p&gt;Git normally creates a directory based on the repository name, but you can choose your own:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/username/repository.git my-project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is one practical consideration worth knowing about. A repository with ten years of commits can be considerably larger than the current source code suggests, because you're bringing historical data along with it. When you only need a limited amount of history, a shallow clone can be useful:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone &lt;span class="nt"&gt;--depth&lt;/span&gt; 1 https://github.com/username/repository.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That gives you a much smaller starting point, although it also means you don't have the complete history available locally.&lt;/p&gt;

&lt;p&gt;The simple rule is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use&lt;/strong&gt; &lt;code&gt;init&lt;/code&gt; &lt;strong&gt;when you're creating the repository. Use&lt;/strong&gt; &lt;code&gt;clone&lt;/code&gt; &lt;strong&gt;when the repository already exists.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  3. &lt;code&gt;git status&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;If there is one Git command worth learning so well that you stop thinking about it, it is &lt;code&gt;git status&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you're not sure what Git thinks is happening, ask Git. The command shows your current branch and tells you about changes that are modified, staged, or untracked.&lt;/p&gt;

&lt;p&gt;A simplified example might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;On branch main

Changes not staged for commit:
        modified:   index.html

Untracked files:
        style.css
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, &lt;code&gt;index.html&lt;/code&gt; is already part of the repository but has local changes that haven't been staged. &lt;code&gt;style.css&lt;/code&gt; is a new file that Git isn't tracking yet.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git status&lt;/code&gt; is deliberately boring, and that's exactly why it is useful. It doesn't change your files or history. Run it before you commit. Run it when you've just pulled someone else's changes. Run it before a reset. Run it after you've been switching branches and can no longer remember what you were doing.&lt;/p&gt;

&lt;p&gt;When Git starts feeling confusing, don't guess. Start with &lt;code&gt;git status&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. &lt;code&gt;git add&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The staging area is one of Git's most useful ideas, and also one of the first things beginners find strange.&lt;/p&gt;

&lt;p&gt;Suppose you've changed three files, but only two of those changes belong in the next commit. Git gives you the option to decide exactly what gets recorded.&lt;/p&gt;

&lt;p&gt;That's what &lt;code&gt;git add&lt;/code&gt; does.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git add filename.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can stage everything below the current directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git add &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or stage matching files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git add &lt;span class="k"&gt;*&lt;/span&gt;.css
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Think of staging as preparing a package before you send it. Your working tree can contain many changes, but only the staged changes are going into the next commit.&lt;/p&gt;

&lt;p&gt;That distinction becomes particularly useful when you're doing unrelated work in the same project. You don't have to create a giant commit simply because several files happen to be modified at once.&lt;/p&gt;

&lt;p&gt;One caution: &lt;code&gt;git add .&lt;/code&gt; is convenient, but convenience can hide mistakes. It may stage files you didn't intend to include, which is why &lt;code&gt;.gitignore&lt;/code&gt; should be set up early for things such as dependency directories, local environment files, generated output, and operating-system clutter.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. &lt;code&gt;git commit&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;A commit is a recorded snapshot of the changes you've staged.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Add user authentication"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the point where your work becomes part of the repository's history. You can compare it later, inspect it, revert it, or use it as a known point to return to.&lt;/p&gt;

&lt;p&gt;That makes commit quality matter more than many beginners realize. A repository filled with messages like &lt;code&gt;changes&lt;/code&gt;, &lt;code&gt;fix&lt;/code&gt;, and &lt;code&gt;final&lt;/code&gt; technically works, but it is much harder to understand months later.&lt;/p&gt;

&lt;p&gt;Compare:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Fix bug
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Fix null pointer when cart is empty
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second message tells the next person — often future you — what the commit was actually about.&lt;/p&gt;

&lt;p&gt;There is also a shortcut worth knowing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git commit &lt;span class="nt"&gt;-am&lt;/span&gt; &lt;span class="s2"&gt;"Fix navbar spacing on mobile"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-a&lt;/code&gt; option stages modifications and deletions to files Git already tracks, then creates the commit. It does &lt;strong&gt;not&lt;/strong&gt; include new files, so a brand-new file still needs &lt;code&gt;git add&lt;/code&gt; first.&lt;/p&gt;

&lt;p&gt;And when the latest commit is almost right but not quite, &lt;code&gt;--amend&lt;/code&gt; lets you update it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git commit &lt;span class="nt"&gt;--amend&lt;/span&gt; &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Add user authentication and tests"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Amending is handy while a commit is still local. Once you've pushed it and other people may have based work on it, rewriting that commit becomes a different conversation.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. &lt;code&gt;git branch&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Branches are how you isolate work without putting unfinished changes directly onto the branch other people may be using.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git branch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;lists your local branches.&lt;/p&gt;

&lt;p&gt;To create one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git branch new-feature
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To delete a branch that has already been safely merged:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git branch &lt;span class="nt"&gt;-d&lt;/span&gt; old-feature
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And when you know you want to remove it regardless of whether Git considers it merged:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git branch &lt;span class="nt"&gt;-D&lt;/span&gt; old-feature
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can rename a branch with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git branch &lt;span class="nt"&gt;-m&lt;/span&gt; new-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A useful thing to understand is that a branch isn't a separate copy of your entire project. It is essentially a movable reference to a commit, which is why creating branches is cheap.&lt;/p&gt;

&lt;p&gt;There is one beginner mistake worth remembering: &lt;code&gt;git branch new-feature&lt;/code&gt; creates the branch, but it does &lt;strong&gt;not&lt;/strong&gt; switch you to it. To create and move to a branch in one step, modern Git gives you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git switch &lt;span class="nt"&gt;-c&lt;/span&gt; new-feature
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That leads naturally to the next command family.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. &lt;code&gt;git checkout&lt;/code&gt;, &lt;code&gt;git switch&lt;/code&gt;, and &lt;code&gt;git restore&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;For years, &lt;code&gt;git checkout&lt;/code&gt; handled several unrelated jobs. You could use it to switch branches, create a branch, or restore a file to an earlier state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git checkout new-feature
git checkout &lt;span class="nt"&gt;-b&lt;/span&gt; another-feature
git checkout &lt;span class="nt"&gt;--&lt;/span&gt; filename.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command still works, and you will continue to encounter it in older documentation and existing projects. The problem is that one command having several very different meanings makes it harder for beginners to understand what is happening.&lt;/p&gt;

&lt;p&gt;Modern Git separates those operations.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;git switch&lt;/code&gt; when you're working with branches:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git switch new-feature
git switch &lt;span class="nt"&gt;-c&lt;/span&gt; another-feature
git switch -
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last form switches back to the branch you were previously on, which is surprisingly useful when you're jumping between two tasks.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;git restore&lt;/code&gt; when you're working with files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git restore filename.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That discards unstaged changes to the file.&lt;/p&gt;

&lt;p&gt;To remove a file from the staging area without throwing away its edits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git restore &lt;span class="nt"&gt;--staged&lt;/span&gt; filename.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the simplest mental model is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git switch   → branches
git restore  → files
git checkout → older multi-purpose command
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You don't need to treat &lt;code&gt;checkout&lt;/code&gt; as obsolete. Just recognize that &lt;code&gt;switch&lt;/code&gt; and &lt;code&gt;restore&lt;/code&gt; make your intention clearer when you're writing new commands.&lt;/p&gt;




&lt;h2&gt;
  
  
  8. &lt;code&gt;git merge&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Eventually, isolated work needs to come back together. That's what merging does.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git switch main
git merge new-feature
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If Git can reconcile the two histories automatically, the merge may be uneventful. When it can't, you'll get a conflict.&lt;/p&gt;

&lt;p&gt;A conflict usually means that both sides changed overlapping parts of a file, or made changes Git cannot safely combine on its own. The repository hasn't been corrupted; Git has simply reached a decision that requires a human.&lt;/p&gt;

&lt;p&gt;You'll see something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt; HEAD
your version
=======
their version
&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; new-feature
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the file, decide what the final version should actually be, remove the conflict markers, and then stage the resolved file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git add filename.txt
git commit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first few merge conflicts can feel alarming because the file suddenly contains strange markers and Git seems to stop in the middle of the operation. In practice, conflict resolution is a normal part of collaborative development. The important thing is to understand that Git is waiting for a decision, not reporting a fatal failure.&lt;/p&gt;




&lt;h2&gt;
  
  
  9. &lt;code&gt;git push&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Your commits are local until you send them somewhere shared.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push origin main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That command pushes your local commits to the &lt;code&gt;main&lt;/code&gt; branch on the &lt;code&gt;origin&lt;/code&gt; remote.&lt;/p&gt;

&lt;p&gt;When you create a new branch and push it for the first time, you will often use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push &lt;span class="nt"&gt;-u&lt;/span&gt; origin new-feature
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-u&lt;/code&gt; option establishes the upstream relationship between your local branch and the remote branch. After that, a plain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is usually enough.&lt;/p&gt;

&lt;p&gt;Force-pushing is where things get serious.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push &lt;span class="nt"&gt;--force&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can replace remote history, including commits you weren't aware another developer had added. There are legitimate reasons to rewrite history, particularly on personal branches after an amend or rebase, but it should never be a reflex.&lt;/p&gt;

&lt;p&gt;When a force-push is genuinely necessary, this is generally safer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push &lt;span class="nt"&gt;--force-with-lease&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It checks that the remote branch is still in the state your local repository expects. If someone else has moved it since your last update, the push can be rejected instead of silently overwriting their work.&lt;/p&gt;

&lt;p&gt;Even with that protection, shared branches deserve caution.&lt;/p&gt;




&lt;h2&gt;
  
  
  10. &lt;code&gt;git pull&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;git pull&lt;/code&gt; is the command you use when the remote repository has changes you need to bring into your current branch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git pull origin main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Conceptually, the operation starts by fetching updates from the remote and then integrating the relevant remote branch into your current branch. Exactly how that integration happens depends on your configuration and options.&lt;/p&gt;

&lt;p&gt;A common alternative is rebasing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git pull &lt;span class="nt"&gt;--rebase&lt;/span&gt; origin main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of creating a merge as part of the pull, Git replays your local commits on top of the updated remote history. That can produce a cleaner, more linear history, but rebasing also rewrites the commits being replayed.&lt;/p&gt;

&lt;p&gt;Neither workflow is universally right. Some teams merge, some rebase, and some use stricter fast-forward policies. The important thing is consistency: know what your project expects before changing the default behavior.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git config pull.rebase &lt;span class="nb"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can make rebase the default for the relevant configuration scope.&lt;/p&gt;

&lt;p&gt;One subtle point is worth remembering: &lt;code&gt;git pull origin main&lt;/code&gt; does not necessarily mean “update my local &lt;code&gt;main&lt;/code&gt; branch.” It integrates &lt;code&gt;origin/main&lt;/code&gt; into &lt;strong&gt;whichever branch you're currently on&lt;/strong&gt;. That distinction matters when you're working on a feature branch.&lt;/p&gt;




&lt;h2&gt;
  
  
  11. &lt;code&gt;git log&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Every Git repository eventually reaches the point where you need to know not just what the code looks like now, but how it got there.&lt;/p&gt;

&lt;p&gt;That's what &lt;code&gt;git log&lt;/code&gt; is for.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The default output contains commit metadata as well as the commit message, which can get noisy in a large repository. In practice, you'll often want a more compact view:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git log &lt;span class="nt"&gt;--oneline&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or, when you're trying to understand branching and merges:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git log &lt;span class="nt"&gt;--oneline&lt;/span&gt; &lt;span class="nt"&gt;--graph&lt;/span&gt; &lt;span class="nt"&gt;--all&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few flags cover most everyday needs:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Flag&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;--oneline&lt;/td&gt;
&lt;td&gt;Compact commit summary&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;--graph&lt;/td&gt;
&lt;td&gt;Draw an ASCII representation of branches and merges&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-p&lt;/td&gt;
&lt;td&gt;Show the changes introduced by commits&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;--author="name"&lt;/td&gt;
&lt;td&gt;Filter commits by author&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-n 5&lt;/td&gt;
&lt;td&gt;Show only the latest five commits&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The real value of &lt;code&gt;git log&lt;/code&gt; isn't the list itself. It is the context. When a piece of code looks strange and nobody remembers why it exists, history can often answer the question faster than another hour of searching through the codebase.&lt;/p&gt;




&lt;h2&gt;
  
  
  12. &lt;code&gt;git diff&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Before you commit a change, you should know what you're actually about to record.&lt;/p&gt;

&lt;p&gt;That's the job of &lt;code&gt;git diff&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git diff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;- &amp;lt;h1&amp;gt;Welcome&amp;lt;/h1&amp;gt;
&lt;/span&gt;&lt;span class="gi"&gt;+ &amp;lt;h1&amp;gt;Welcome to My Site&amp;lt;/h1&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The minus line represents the old content; the plus line represents the new content.&lt;/p&gt;

&lt;p&gt;There is a second version you'll use often:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git diff &lt;span class="nt"&gt;--staged&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That shows what is currently in the staging area and therefore what the next commit is likely to contain.&lt;/p&gt;

&lt;p&gt;You can also compare two branch tips directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git diff branch1 branch2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A five-second diff check before committing is one of the best habits you can build. It catches accidental formatting changes, forgotten debugging statements, edits to the wrong file, and all the tiny mistakes that otherwise end up being discovered during code review.&lt;/p&gt;




&lt;h2&gt;
  
  
  13. &lt;code&gt;git stash&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Stash is useful when your current work is unfinished but your working tree needs to be clean.&lt;/p&gt;

&lt;p&gt;Maybe a production issue needs attention. Maybe you need to inspect another branch. Maybe you simply aren't ready to create a commit yet.&lt;/p&gt;

&lt;p&gt;You can temporarily put your work aside with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git stash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Git stores the changes and returns the working tree to a clean state.&lt;/p&gt;

&lt;p&gt;You can see what you've saved with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git stash list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and bring the most recent stash back with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git stash pop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you'd rather reapply it without removing the stash entry:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git stash apply
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And when you know you no longer need one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git stash drop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One detail surprises people: untracked files aren't included in a normal stash. When you need those as well, use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git stash &lt;span class="nt"&gt;-u&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you regularly have several stashes in flight, name them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git stash push &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"search filter work"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That small bit of discipline makes a huge difference when you come back to the stash list later and discover that every entry is just another anonymous &lt;code&gt;WIP&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  14. &lt;code&gt;git reset&lt;/code&gt; and &lt;code&gt;git revert&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;These two commands are often introduced together because both can be used to undo work. They are not interchangeable, and the difference becomes particularly important once your commits are being shared with other people.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;git reset&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Reset moves the current branch reference backward.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git reset &lt;span class="nt"&gt;--soft&lt;/span&gt; HEAD~1
git reset &lt;span class="nt"&gt;--mixed&lt;/span&gt; HEAD~1
git reset &lt;span class="nt"&gt;--hard&lt;/span&gt; HEAD~1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The three modes mainly differ in what happens to your index and working tree:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Mode&lt;/th&gt;
&lt;th&gt;Branch&lt;/th&gt;
&lt;th&gt;Staging area&lt;/th&gt;
&lt;th&gt;Working tree&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;--soft&lt;/td&gt;
&lt;td&gt;Moves&lt;/td&gt;
&lt;td&gt;Keeps changes staged&lt;/td&gt;
&lt;td&gt;Keeps changes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;--mixed&lt;/td&gt;
&lt;td&gt;Moves&lt;/td&gt;
&lt;td&gt;Unstages changes&lt;/td&gt;
&lt;td&gt;Keeps changes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;--hard&lt;/td&gt;
&lt;td&gt;Moves&lt;/td&gt;
&lt;td&gt;Resets&lt;/td&gt;
&lt;td&gt;Resets&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;--mixed&lt;/code&gt; is the default.&lt;/p&gt;

&lt;p&gt;The command that deserves the most respect is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git reset &lt;span class="nt"&gt;--hard&lt;/span&gt; HEAD~1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It can discard changes from tracked files in your working tree. If you haven't checked what Git thinks is going to happen, this is a very bad time to start experimenting.&lt;/p&gt;

&lt;p&gt;The other important consideration is whether the commit has already been shared.&lt;/p&gt;

&lt;p&gt;If the commit exists only locally, resetting can be perfectly reasonable. If you've already pushed it and other people may have based their work on it, moving the branch backward creates a history rewrite that affects everyone downstream.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;git revert&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;For an already-shared commit, &lt;code&gt;git revert&lt;/code&gt; is often the safer choice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git revert HEAD
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rather than moving the branch backward, it creates a &lt;strong&gt;new commit&lt;/strong&gt; that reverses the earlier change.&lt;/p&gt;

&lt;p&gt;The easiest way to remember the distinction is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reset   → move the branch backward
revert  → add a new commit that undoes an earlier one
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Git also keeps a reflog locally, which can sometimes help recover from mistakes involving commands such as &lt;code&gt;reset&lt;/code&gt;. That's useful to know, but it shouldn't be treated as permission to run destructive commands carelessly.&lt;/p&gt;




&lt;h2&gt;
  
  
  15. &lt;code&gt;git remote&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;A Git repository can work entirely locally, but most development eventually involves a remote repository somewhere else. &lt;code&gt;git remote&lt;/code&gt; lets you inspect and manage those connections.&lt;/p&gt;

&lt;p&gt;To see what is configured:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git remote &lt;span class="nt"&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To add a remote:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git remote add origin https://github.com/user/repo.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To remove one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git remote remove origin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To rename one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git remote rename origin upstream
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;origin&lt;/code&gt; is a convention, not a special Git command or keyword. When you clone a repository, Git normally names the remote &lt;code&gt;origin&lt;/code&gt; simply because that is the conventional default.&lt;/p&gt;

&lt;p&gt;Fork-based workflows often use another common convention:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;origin   → your fork
upstream → original repository
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That arrangement lets you push your own work to &lt;code&gt;origin&lt;/code&gt; while fetching updates from the original project through &lt;code&gt;upstream&lt;/code&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Five Habits That Make Git Much Easier
&lt;/h1&gt;

&lt;p&gt;Knowing commands is useful. Knowing when to stop and inspect the repository is even more useful.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check before you change things
&lt;/h3&gt;

&lt;p&gt;When you're uncertain, start here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git status
git diff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those two commands answer a remarkable number of “what just happened?” questions without changing anything themselves.&lt;/p&gt;

&lt;h3&gt;
  
  
  Keep commits focused
&lt;/h3&gt;

&lt;p&gt;Small commits are easier to review and easier to undo. They also make &lt;code&gt;git log&lt;/code&gt; useful instead of turning it into a museum of commits named &lt;code&gt;final&lt;/code&gt;, &lt;code&gt;final2&lt;/code&gt;, and &lt;code&gt;really-final-this-time&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use &lt;code&gt;.gitignore&lt;/code&gt; early
&lt;/h3&gt;

&lt;p&gt;A typical project might ignore things such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node_modules/
.env
.DS_Store
dist/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just remember that &lt;code&gt;.gitignore&lt;/code&gt; doesn't remove a file that has already been committed. If a credential or API key has already been pushed to a remote repository, assume it has been exposed and rotate it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Treat force-push as an exception
&lt;/h3&gt;

&lt;p&gt;Before using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push &lt;span class="nt"&gt;--force&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ask yourself whether anyone else could have based work on the remote branch.&lt;/p&gt;

&lt;p&gt;On a private feature branch, rewriting history may be perfectly reasonable. On a shared branch, it can create a mess for everyone.&lt;/p&gt;

&lt;h3&gt;
  
  
  Practice recovery, not memorization
&lt;/h3&gt;

&lt;p&gt;The fastest way to understand Git is to use it somewhere safe.&lt;/p&gt;

&lt;p&gt;Create a throwaway repository. Make a few commits. Create a branch. Modify a file. Stash it. Undo something. Create a merge conflict and resolve it.&lt;/p&gt;

&lt;p&gt;Once you've recovered from a mistake in a repository you don't care about, the same commands feel much less intimidating in a repository you do.&lt;/p&gt;




&lt;h1&gt;
  
  
  Git Commands by Situation
&lt;/h1&gt;

&lt;p&gt;This is the part worth bookmarking because it answers the question beginners actually have: &lt;strong&gt;“What do I type now?”&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;Reach for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Starting a project&lt;/td&gt;
&lt;td&gt;git init&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Getting an existing project&lt;/td&gt;
&lt;td&gt;git clone&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Not sure what's happening&lt;/td&gt;
&lt;td&gt;git status&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Choosing what belongs in the next commit&lt;/td&gt;
&lt;td&gt;git add&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Saving a checkpoint&lt;/td&gt;
&lt;td&gt;git commit&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Starting isolated work&lt;/td&gt;
&lt;td&gt;git branch / git switch&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Moving between branches&lt;/td&gt;
&lt;td&gt;git switch&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Throwing away a local file change&lt;/td&gt;
&lt;td&gt;git restore&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Combining branch histories&lt;/td&gt;
&lt;td&gt;git merge&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sharing your commits&lt;/td&gt;
&lt;td&gt;git push&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Getting remote changes&lt;/td&gt;
&lt;td&gt;git pull&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Investigating history&lt;/td&gt;
&lt;td&gt;git log&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Inspecting exactly what changed&lt;/td&gt;
&lt;td&gt;git diff&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Temporarily setting work aside&lt;/td&gt;
&lt;td&gt;git stash&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Undoing an unshared commit&lt;/td&gt;
&lt;td&gt;git reset&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Undoing a shared commit&lt;/td&gt;
&lt;td&gt;git revert&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Checking repository connections&lt;/td&gt;
&lt;td&gt;git remote&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h1&gt;
  
  
  Quick-Reference Cheat Sheet
&lt;/h1&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;#&lt;/th&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;git init&lt;/td&gt;
&lt;td&gt;Create a repository&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;git clone&lt;/td&gt;
&lt;td&gt;Copy an existing repository&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;git status&lt;/td&gt;
&lt;td&gt;Inspect repository state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;git add&lt;/td&gt;
&lt;td&gt;Stage changes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;git commit&lt;/td&gt;
&lt;td&gt;Record a snapshot&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;git branch&lt;/td&gt;
&lt;td&gt;Manage branches&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;git checkout / git switch / git restore&lt;/td&gt;
&lt;td&gt;Switch branches or restore files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;git merge&lt;/td&gt;
&lt;td&gt;Combine branch histories&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;git push&lt;/td&gt;
&lt;td&gt;Send commits to a remote&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;git pull&lt;/td&gt;
&lt;td&gt;Fetch and integrate remote changes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;git log&lt;/td&gt;
&lt;td&gt;Inspect commit history&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;git diff&lt;/td&gt;
&lt;td&gt;Compare changes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;13&lt;/td&gt;
&lt;td&gt;git stash&lt;/td&gt;
&lt;td&gt;Temporarily store unfinished work&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;git reset / git revert&lt;/td&gt;
&lt;td&gt;Undo changes in different ways&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;git remote&lt;/td&gt;
&lt;td&gt;Manage remote connections&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h1&gt;
  
  
  Where to Go From Here
&lt;/h1&gt;

&lt;p&gt;You don't need to memorize all fifteen commands before you start building things.&lt;/p&gt;

&lt;p&gt;For most everyday work, you'll spend a surprising amount of time with just:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git status
git add
git commit
git push
git pull
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Branching becomes important as soon as you're working on multiple features or collaborating with other developers. &lt;code&gt;stash&lt;/code&gt;, &lt;code&gt;reset&lt;/code&gt;, and &lt;code&gt;revert&lt;/code&gt; become important when something gets messy.&lt;/p&gt;

&lt;p&gt;That's actually the better way to learn Git: let the situations teach you the commands instead of trying to memorize the entire manual up front.&lt;/p&gt;

&lt;p&gt;And when you do get into trouble, remember the most useful Git habit of all:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Don't guess what Git is doing. Inspect the state first.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git status
git diff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Git isn't really about memorizing commands.&lt;/p&gt;

&lt;p&gt;It's about knowing where your changes are, understanding what you want to happen next, and choosing the command that gets you there without making the problem worse.&lt;/p&gt;

&lt;p&gt;Once that mental model becomes second nature, Git stops feeling mysterious.&lt;/p&gt;

&lt;p&gt;It becomes a safety net.&lt;/p&gt;

&lt;p&gt;And that's what it was supposed to be all along.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://zyvop.com/the-15-git-commands-that-ll-save-your-sanity-and-your-code-7yygk" rel="noopener noreferrer"&gt;ZyVOP&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;💡 For more articles like this, &lt;a href="https://zyvop.com/newsletter" rel="noopener noreferrer"&gt;subscribe to the ZyVOP newsletter&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>git</category>
      <category>versioncontrol</category>
      <category>gitcommands</category>
      <category>gitforbeginners</category>
    </item>
    <item>
      <title>Zero-Downtime Deployments: Strategies and Tradeoffs</title>
      <dc:creator>Bhavya Arora</dc:creator>
      <pubDate>Mon, 31 Aug 2026 06:53:33 +0000</pubDate>
      <link>https://dev.to/bhavya_arora/zero-downtime-deployments-strategies-and-tradeoffs-987</link>
      <guid>https://dev.to/bhavya_arora/zero-downtime-deployments-strategies-and-tradeoffs-987</guid>
      <description>&lt;p&gt;Every deployment is a gamble. You're swapping out a running system mid-flight — like changing the engine of a plane while it's in the air. Do it wrong and users see errors, requests fail, and your on-call phone starts buzzing at 2 AM.&lt;/p&gt;

&lt;p&gt;Zero-downtime deployment is the discipline of making that swap invisible. But "invisible" hides a lot of complexity. This post walks through the main strategies, when to use each one, and what they cost you.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Downtime Happens in the First Place
&lt;/h2&gt;

&lt;p&gt;Before picking a strategy, it helps to understand where downtime actually comes from:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Process restart gap&lt;/strong&gt; — the old process dies before the new one is ready&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Port contention&lt;/strong&gt; — old and new can't bind to the same port simultaneously&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Schema migrations&lt;/strong&gt; — the database is in a state neither the old nor new code can handle&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cache invalidation&lt;/strong&gt; — stale data causes 500s after a deploy&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;In-flight requests&lt;/strong&gt; — requests mid-processing get dropped when a process dies&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each strategy addresses these differently. None of them solve all of these at once without some cost.&lt;/p&gt;




&lt;h2&gt;
  
  
  Strategy 1: Blue-Green Deployments
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How it works
&lt;/h3&gt;

&lt;p&gt;You run two identical production environments — &lt;strong&gt;blue&lt;/strong&gt; (live) and &lt;strong&gt;green&lt;/strong&gt; (idle). You deploy to green, smoke-test it, then flip a load balancer or DNS record to point traffic from blue to green. Blue becomes your instant rollback target.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNp9kcFq20AQhl9lOhBwYBUi05MOhag4aUDtwca5WD6MpJG8ZLUrdtd208SQU-8JuTsteYW8kJ8gj1AkJaRg2j3t_vvNfsPsNeamYIywVGadL8h6SMapBgCYDmYv27snmDq2bn4IQfAJkniWGCogJkU6ZztPdQ8ncXt_k-Lu7hFiLo1lKJVsYHf7AOHx8UGKNxAn09E7fvTK_4KT0rPdx8_Go9G3N4FbZpWlZtE9MkvxZfvwDLFacleyCgEGSq74MMV5X9CuLJyda-fbViH8Ox--58PXnHWx5-o66GTb33BmmXVvGwIMZKEYdj_vYV9b_UNb_V_b2d7m0oMerFEqo_wyAremBtptO5skRoE125pkgdE1-gXX7TcWXNJSeRR9ckFWUqbYtUxptD-lWqorjDCgplEcuCvnuRYQK6kvv1I-6c6nRnsBKU64MgzT8xQFjE1mvBHwhdWKvcxJwImVpAQ40i5wbGWJopNM5I-2l_Bj8x03G4FZ9dkoYzHCD-uF9IybPxZizfA" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNp9kcFq20AQhl9lOhBwYBUi05MOhag4aUDtwca5WD6MpJG8ZLUrdtd208SQU-8JuTsteYW8kJ8gj1AkJaRg2j3t_vvNfsPsNeamYIywVGadL8h6SMapBgCYDmYv27snmDq2bn4IQfAJkniWGCogJkU6ZztPdQ8ncXt_k-Lu7hFiLo1lKJVsYHf7AOHx8UGKNxAn09E7fvTK_4KT0rPdx8_Go9G3N4FbZpWlZtE9MkvxZfvwDLFacleyCgEGSq74MMV5X9CuLJyda-fbViH8Ox--58PXnHWx5-o66GTb33BmmXVvGwIMZKEYdj_vYV9b_UNb_V_b2d7m0oMerFEqo_wyAremBtptO5skRoE125pkgdE1-gXX7TcWXNJSeRR9ckFWUqbYtUxptD-lWqorjDCgplEcuCvnuRYQK6kvv1I-6c6nRnsBKU64MgzT8xQFjE1mvBHwhdWKvcxJwImVpAQ40i5wbGWJopNM5I-2l_Bj8x03G4FZ9dkoYzHCD-uF9IybPxZizfA" alt="Mermaid Diagram" width="1310" height="228"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Real example
&lt;/h3&gt;

&lt;p&gt;AWS Elastic Beanstalk's "swap environment URLs" does exactly this. You deploy v2 to a staging environment, run your test suite, then call &lt;code&gt;aws elasticbeanstalk swap-environment-cnames&lt;/code&gt;. Traffic flips in seconds. If something goes wrong, you swap back.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tradeoffs
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Instant rollback — just flip the switch back&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Green environment acts as a staging/smoke-test target&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No partial state: all users are on one version at a time&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Double the infrastructure cost while both environments run&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Database is the hard problem — both blue and green share it, so schema changes must be backward-compatible&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;DNS-based flips can have propagation lag (use load balancer-level routing instead)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Warm-up time for green (JVM startup, cache priming) can cause a traffic spike on switch&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Best for:&lt;/strong&gt; Stateless services, teams with strict rollback SLAs, apps on managed platforms (Elastic Beanstalk, App Engine).&lt;/p&gt;




&lt;h2&gt;
  
  
  Strategy 2: Rolling Deployments
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How it works
&lt;/h3&gt;

&lt;p&gt;Instead of replacing everything at once, you update instances gradually — replacing a few at a time while the rest keep serving traffic. The load balancer routes only to healthy instances.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNqN0s9q4zAQBvBXmc5ZPvjPXnIoNFtCF3YvddtL3MPYGieismQkJWkaAvsO22Ofrk-y2Ca41IdEF40-JL4f2AesrGScYa3trlqTC_D7vjAAAH5Trhy1a8jjZYF54BZi-Pz7DnPyrJXhAp-Hm92SynEVlDXwMB9Tipfb-BmiKAJKxjEdx6wbhwdsZGG-dyen7qTvXijnA7RWwqaVFFhegCg7_zaBz49_BQ615YgpR0x5DpOeMGmPuSNd72h_AaGaEqpkGo2U6hwlO1F-9JRbe9H3kFOHnDpkOo2yr9HElccQRdeQJ8OWDluGAht2DSmJswOGNTfdrya5po0OKIbkiZyiUrPv7tTWhAU1Su9xhhG1rebI733gRsBcK_Pyh6q8Py-sCQIKzHllGR5_FSjg3pY2WAF3rLccVEUCbpwiLcCT8ZFnp2oUfUmu3jpLnLWveDwKLFc_rbYOZ3i1W6vAePwP_PPz5g" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNqN0s9q4zAQBvBXmc5ZPvjPXnIoNFtCF3YvddtL3MPYGieismQkJWkaAvsO22Ofrk-y2Ca41IdEF40-JL4f2AesrGScYa3trlqTC_D7vjAAAH5Trhy1a8jjZYF54BZi-Pz7DnPyrJXhAp-Hm92SynEVlDXwMB9Tipfb-BmiKAJKxjEdx6wbhwdsZGG-dyen7qTvXijnA7RWwqaVFFhegCg7_zaBz49_BQ615YgpR0x5DpOeMGmPuSNd72h_AaGaEqpkGo2U6hwlO1F-9JRbe9H3kFOHnDpkOo2yr9HElccQRdeQJ8OWDluGAht2DSmJswOGNTfdrya5po0OKIbkiZyiUrPv7tTWhAU1Su9xhhG1rebI733gRsBcK_Pyh6q8Py-sCQIKzHllGR5_FSjg3pY2WAF3rLccVEUCbpwiLcCT8ZFnp2oUfUmu3jpLnLWveDwKLFc_rbYOZ3i1W6vAePwP_PPz5g" alt="Mermaid Diagram" width="811" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Real example
&lt;/h3&gt;

&lt;p&gt;Kubernetes rolling updates are the canonical implementation. Your deployment spec controls the pace:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;strategy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;RollingUpdate&lt;/span&gt;
  &lt;span class="na"&gt;rollingUpdate&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;maxSurge&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;        &lt;span class="c1"&gt;# spin up 1 extra pod before killing old ones&lt;/span&gt;
    &lt;span class="na"&gt;maxUnavailable&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;  &lt;span class="c1"&gt;# never reduce below desired replica count&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With these settings, Kubernetes will:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Spin up a new v2 pod&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Wait until it passes readiness checks&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Only then terminate one v1 pod&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Repeat&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Tradeoffs
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;No infrastructure doubling (with &lt;code&gt;maxSurge: 0&lt;/code&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Works natively in Kubernetes, ECS, and most orchestrators&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Gradual rollout limits the blast radius of a bad deploy&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Both versions run simultaneously&lt;/strong&gt; during the rollout window — your API must be backward-compatible with itself&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Slower to complete than blue-green&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rollback means rolling forward again (another full rollout cycle)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Health checks must be accurate — a pod that starts but crashes under load will still pass initial checks&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Best for:&lt;/strong&gt; Kubernetes-native teams, microservices with stable APIs, when infrastructure cost is a constraint.&lt;/p&gt;




&lt;h2&gt;
  
  
  Strategy 3: Canary Releases
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How it works
&lt;/h3&gt;

&lt;p&gt;You send a small percentage of traffic to the new version and watch metrics before committing. The name comes from the "canary in a coal mine" — if the canary dies, you know there's a problem before it affects everyone.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNpdUL1KA0EQfpVxQFDYKxK0MIXiD6JgLC4kILcWc5eJt7i3G3bXizEGrK0U7dP4ZD5BHkFuTxEtv_l-5ptZYGHHjD2caDsrSnIBLlJpAACGW9l69foBQ8_OX29DkuxDmh2TITeH1N4FdtetMm24R4l7u5sAPlCuWeIjjDqZxLoDn0_vMGin0mzl5Fkrw9sS_9kbNxQxP9q7mcT16uUD6m7MaFc3GYZnULPzyprfmFEnVuxn69XbM_Q5OFX4H67bci3qR3CyqLtQMulQzg-WLXPyXeWKfVypTOGYPMNm0yj9K7q0UeOs1jkVt-3JKLBiV5EaY2-BoeSq-e6YJ3SnA4p2MiKnmn_4RjOxJpxSpfQce5jQdKo58XMfuBJwpJW57VMxiPjUmiBA4oBvLMPwXKKA1OY2WAFnrGsOqiABh06RFuDJ-MSzUxMUcclAPTRdOjvTe1wuBeY3x1Zbhz3cmJUqMC6_AAGTrEU" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNpdUL1KA0EQfpVxQFDYKxK0MIXiD6JgLC4kILcWc5eJt7i3G3bXizEGrK0U7dP4ZD5BHkFuTxEtv_l-5ptZYGHHjD2caDsrSnIBLlJpAACGW9l69foBQ8_OX29DkuxDmh2TITeH1N4FdtetMm24R4l7u5sAPlCuWeIjjDqZxLoDn0_vMGin0mzl5Fkrw9sS_9kbNxQxP9q7mcT16uUD6m7MaFc3GYZnULPzyprfmFEnVuxn69XbM_Q5OFX4H67bci3qR3CyqLtQMulQzg-WLXPyXeWKfVypTOGYPMNm0yj9K7q0UeOs1jkVt-3JKLBiV5EaY2-BoeSq-e6YJ3SnA4p2MiKnmn_4RjOxJpxSpfQce5jQdKo58XMfuBJwpJW57VMxiPjUmiBA4oBvLMPwXKKA1OY2WAFnrGsOqiABh06RFuDJ-MSzUxMUcclAPTRdOjvTe1wuBeY3x1Zbhz3cmJUqMC6_AAGTrEU" alt="Mermaid Diagram" width="1043" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You progressively shift traffic: 5% → 25% → 50% → 100%, with automated or manual checks at each stage.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real example
&lt;/h3&gt;

&lt;p&gt;Netflix's Spinnaker is built around canary analysis. At each stage it compares key metrics (error rate, latency p99, CPU) between the canary and baseline. If the canary degrades relative to baseline, the pipeline auto-rolls back.&lt;/p&gt;

&lt;p&gt;You can approximate this with Nginx using &lt;code&gt;split_clients&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="k"&gt;split_clients&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$request_id&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt; &lt;span class="nv"&gt;$upstream&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kn"&gt;5%&lt;/span&gt;   &lt;span class="s"&gt;v2_backend&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kn"&gt;*&lt;/span&gt;    &lt;span class="s"&gt;v1_backend&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or with Kubernetes + Argo Rollouts, which handles the traffic shifting and metric analysis natively.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💾 &lt;strong&gt;Working example:&lt;/strong&gt; &lt;a href="https://github.com/bhavyaarora1905/blogs/blob/main/canary-router.js" rel="noopener noreferrer"&gt;canary-router.js&lt;/a&gt; — a pure Node.js canary router that splits traffic between v1 and v2, tracks per-version error rates and latency, and warns when the canary degrades. Run with &lt;code&gt;CANARY_PERCENT=10 node canary-router.js&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Tradeoffs
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Real production validation before full rollout&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Catches issues that staging never would (traffic patterns, data edge cases)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Blast radius is tiny if something goes wrong early&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Both versions serve traffic simultaneously — same backward-compat requirement as rolling&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Requires solid observability (you can't make canary decisions without metrics)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;More complex pipeline to build and maintain&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;User experience can be inconsistent if users hit different versions across requests (especially problematic for SPAs)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Best for:&lt;/strong&gt; High-traffic services where production behavior is hard to predict in staging. Teams with mature observability.&lt;/p&gt;




&lt;h2&gt;
  
  
  Strategy 4: Feature Flags (The Invisible Deploy)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How it works
&lt;/h3&gt;

&lt;p&gt;You decouple &lt;strong&gt;deploying code&lt;/strong&gt; from &lt;strong&gt;releasing features&lt;/strong&gt;. The new code ships to 100% of servers, but it's dormant behind a flag. You control rollout through config, not deployments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;feature_flags&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enabled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;new_checkout_flow&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;new_checkout_flow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;legacy_checkout_flow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can roll out to 1% of users, then 10%, then specific cohorts, then everyone — without a single new deployment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real example
&lt;/h3&gt;

&lt;p&gt;Facebook has deployed code to all servers while exposing features to 0 users, then gradually rolled out to billions. Their internal system (now open-sourced as GK) gates features by user ID, location, device type, and more.&lt;/p&gt;

&lt;p&gt;Tools like LaunchDarkly, Unleash, and Statsig give you this out of the box with a dashboard, targeting rules, and kill switches.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💾 &lt;strong&gt;Working example:&lt;/strong&gt; &lt;a href="https://github.com/bhavyaarora1905/blogs/blob/main/feature-flags.js" rel="noopener noreferrer"&gt;feature-flags.js&lt;/a&gt; — a zero-dependency feature flag engine with deterministic percentage rollout (FNV-1a hashing), kill switches, user allowlists, stale flag warnings, and an access log. Run with &lt;code&gt;node feature-flags.js&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Tradeoffs
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Deployment risk decoupled from feature risk — a bad feature flag is a config change to revert, not a rollback&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enables A/B testing and gradual rollouts without ops involvement&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Works for both frontend and backend features&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Flag debt&lt;/strong&gt; — old flags never get cleaned up and become landmines&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Code complexity: every flag is a branch, every branch needs testing&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Flags don't help with infrastructure changes or dependency upgrades&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The database still needs to handle both code paths until the old one is removed&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Best for:&lt;/strong&gt; Feature launches, A/B testing, kill switches for risky features. Not a substitute for deployment infrastructure — use alongside blue-green or rolling, not instead of.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Hard Part: Database Migrations
&lt;/h2&gt;

&lt;p&gt;Every strategy above sidesteps the hardest problem: &lt;strong&gt;your database doesn't version with your code&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When v1 and v2 run simultaneously (rolling, canary) or share a database (blue-green), your schema has to satisfy both versions at once. This rules out most naive migrations.&lt;/p&gt;

&lt;h3&gt;
  
  
  The expand-contract pattern
&lt;/h3&gt;

&lt;p&gt;Instead of one migration, you do three deployments:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNpd0c1unDAQB_BXmc4ZDgs9cahEgLSR9gOxbFWpjioDQ7BibISd3dJkpT5E7lH7DlXueZQ8QR-hWsKq2Rzt-cu_mfEtlroiDLCWelc2vLeQx0wBAKSzrwz_Pjz8hrThhmAGzz_vIfmShsuYMRXO8ySDPDybJwGEcQzRar5ZLKESppN8-KZ4S4yprQe7XlgyUGjbQKnlTasMPD1CT7wyUPe6hTGLl5Prvbi_Jtcb3cXFxyzME8bUJo3DPAmg4OV1LaQ8EV-9x9TWn5CTxNPj647-s_7I3v-ZWH9ko9Uyz8IofztwnK3S48STFkniCkzZUMsPiFZyOJV7arlQZiSPOwbX_XDHsKJO6gG2HsM7SL3jJt5U_bHqo4Mt9S0XFQa3aBtqDz9YUc1vpEXn5eYz7wUvJJlDptbKnvNWyAEDdHnXSXLNYCy1DpxJoa4XvFyP53OtrAMM13SlCTYXDB3IdKGtduATyS1ZUXIHwl5w6YDhyriGelGjMyJr8ePQy-x99x33eweLq0hL3WOA73aNsIT7fwOb1gQ" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNpd0c1unDAQB_BXmc4ZDgs9cahEgLSR9gOxbFWpjioDQ7BibISd3dJkpT5E7lH7DlXueZQ8QR-hWsKq2Rzt-cu_mfEtlroiDLCWelc2vLeQx0wBAKSzrwz_Pjz8hrThhmAGzz_vIfmShsuYMRXO8ySDPDybJwGEcQzRar5ZLKESppN8-KZ4S4yprQe7XlgyUGjbQKnlTasMPD1CT7wyUPe6hTGLl5Prvbi_Jtcb3cXFxyzME8bUJo3DPAmg4OV1LaQ8EV-9x9TWn5CTxNPj647-s_7I3v-ZWH9ko9Uyz8IofztwnK3S48STFkniCkzZUMsPiFZyOJV7arlQZiSPOwbX_XDHsKJO6gG2HsM7SL3jJt5U_bHqo4Mt9S0XFQa3aBtqDz9YUc1vpEXn5eYz7wUvJJlDptbKnvNWyAEDdHnXSXLNYCy1DpxJoa4XvFyP53OtrAMM13SlCTYXDB3IdKGtduATyS1ZUXIHwl5w6YDhyriGelGjMyJr8ePQy-x99x33eweLq0hL3WOA73aNsIT7fwOb1gQ" alt="Mermaid Diagram" width="276" height="563"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Phase 1 — Expand:&lt;/strong&gt; Add the new column/table without removing anything.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;display_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Deploy v2 which writes to both &lt;code&gt;name&lt;/code&gt; (old) and &lt;code&gt;display_name&lt;/code&gt; (new), reads from &lt;code&gt;name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Phase 2 — Migrate:&lt;/strong&gt; Backfill the new column.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;display_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;display_name&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Deploy v3 which reads from &lt;code&gt;display_name&lt;/code&gt;, writes to both.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Phase 3 — Contract:&lt;/strong&gt; Remove the old column once no code references it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is slower and more tedious than a single migration, but it's the only way to be truly safe with zero-downtime deploys.&lt;/p&gt;

&lt;h3&gt;
  
  
  What to avoid
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;NOT NULL&lt;/code&gt; columns without defaults added to existing tables&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Renaming columns directly&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dropping columns that old code still reads&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Changing column types in ways that break existing queries&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Putting It Together: A Decision Framework
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNpVksFq20AQhl9luqeESIckzsWHlthBSQ9JsN0USjaHkTSSl6x2xe44rqoYemjvPRQKoWAovfQB-gR9kz6BH6FIcmz3OMs_3__BbC0Sm5Loi0zbeTJFx_DmTBoAgAmj473b1fLpI1zRHFIqta0KMgyGKKX0bh_C8CWMDushGqjsDDDLrEulNEd_voEymUOInaJMV68W0nTY0WGz9fiO_COMjuorohSQbaES8HPFyVRKcwDKeEbD4KzWMSb3zf7O9pV9hNFxfaHyacgOs0wlUprS2XSWsLIGPLkHldBO7dG2dnB-K8Vq-fX3arn8AQM9o_DcERkp7v4Ldy0bwvGOeK--RJ45ktLYuCnDWGnF1cbzeEMYX3dtn2BstVYmh5syRaa2bZ3ubdHD0zb-5ScM0aCrYEya0NNWrreDfiYMzrtbnNQRtWKhpgfSUhqn_D2wtc9m4-t1shuHp5txjT_ZukRR6_L0Cw5gzYVIY-6lNKityb1KaX1ozw6Z8mrrebLxPLOG9m6l-Pv9M0ymqgTFUtztd7koag2ajAhEQa5AlYp-LXhKRfM1U8pwplkE3ctbdApjTb7JZNZwhIXSleiLEMtSU-grz1QEMNDK3F9iMmnnyBoOQIoJ5Zbg5rUUAYxtbNkGcEH6gVglGMCpU6gD8Gh86MmpTARtyUR9aFwOe-V7sVgEIs6HVlsn-uLFfKqYxOIfebgaBg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNpVksFq20AQhl9luqeESIckzsWHlthBSQ9JsN0USjaHkTSSl6x2xe44rqoYemjvPRQKoWAovfQB-gR9kz6BH6FIcmz3OMs_3__BbC0Sm5Loi0zbeTJFx_DmTBoAgAmj473b1fLpI1zRHFIqta0KMgyGKKX0bh_C8CWMDushGqjsDDDLrEulNEd_voEymUOInaJMV68W0nTY0WGz9fiO_COMjuorohSQbaES8HPFyVRKcwDKeEbD4KzWMSb3zf7O9pV9hNFxfaHyacgOs0wlUprS2XSWsLIGPLkHldBO7dG2dnB-K8Vq-fX3arn8AQM9o_DcERkp7v4Ldy0bwvGOeK--RJ45ktLYuCnDWGnF1cbzeEMYX3dtn2BstVYmh5syRaa2bZ3ubdHD0zb-5ScM0aCrYEya0NNWrreDfiYMzrtbnNQRtWKhpgfSUhqn_D2wtc9m4-t1shuHp5txjT_ZukRR6_L0Cw5gzYVIY-6lNKityb1KaX1ozw6Z8mrrebLxPLOG9m6l-Pv9M0ymqgTFUtztd7koag2ajAhEQa5AlYp-LXhKRfM1U8pwplkE3ctbdApjTb7JZNZwhIXSleiLEMtSU-grz1QEMNDK3F9iMmnnyBoOQIoJ5Zbg5rUUAYxtbNkGcEH6gVglGMCpU6gD8Gh86MmpTARtyUR9aFwOe-V7sVgEIs6HVlsn-uLFfKqYxOIfebgaBg" alt="Mermaid Diagram" width="640" height="1582"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Blue-Green&lt;/th&gt;
&lt;th&gt;Rolling&lt;/th&gt;
&lt;th&gt;Canary&lt;/th&gt;
&lt;th&gt;Feature Flags&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Rollback speed&lt;/td&gt;
&lt;td&gt;Instant&lt;/td&gt;
&lt;td&gt;Slow (re-deploy)&lt;/td&gt;
&lt;td&gt;Fast&lt;/td&gt;
&lt;td&gt;Instant (flag toggle)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Infrastructure cost&lt;/td&gt;
&lt;td&gt;2x during deploy&lt;/td&gt;
&lt;td&gt;~1x&lt;/td&gt;
&lt;td&gt;~1x&lt;/td&gt;
&lt;td&gt;~1x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Handles DB migrations&lt;/td&gt;
&lt;td&gt;No (shared DB)&lt;/td&gt;
&lt;td&gt;No (dual version)&lt;/td&gt;
&lt;td&gt;No (dual version)&lt;/td&gt;
&lt;td&gt;Partial&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Observability needed&lt;/td&gt;
&lt;td&gt;Low&lt;/td&gt;
&lt;td&gt;Low&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Best rollout size&lt;/td&gt;
&lt;td&gt;All-or-nothing&lt;/td&gt;
&lt;td&gt;Gradual&lt;/td&gt;
&lt;td&gt;Gradual by %&lt;/td&gt;
&lt;td&gt;Granular&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Most mature teams use a combination: rolling or blue-green for the infrastructure layer, canary for traffic validation, and feature flags for product changes. The strategies are complementary, not competing.&lt;/p&gt;




&lt;h2&gt;
  
  
  Checklist Before Your Next Deploy
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;💾 &lt;strong&gt;Working example:&lt;/strong&gt; &lt;a href="https://github.com/bhavyaarora1905/blogs/blob/main/graceful-shutdown.js" rel="noopener noreferrer"&gt;graceful-shutdown.js&lt;/a&gt; — a Node.js HTTP server demonstrating all three items below: separate liveness/readiness probes, SIGTERM drain with in-flight request tracking, and a hard-exit failsafe timeout. Run with &lt;code&gt;node graceful-shutdown.js&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Health checks return 200 only when the app is genuinely ready (not just started)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Graceful shutdown: app drains in-flight requests before exiting (&lt;code&gt;SIGTERM&lt;/code&gt; handler)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Database migration is backward-compatible with the previous version&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Readiness probe is separate from liveness probe in Kubernetes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You have a tested rollback procedure (not just a theoretical one)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Observability is in place: error rate, latency, and saturation dashboards open during deploy&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Code Examples
&lt;/h2&gt;

&lt;p&gt;All working scripts from this post are available &lt;a href="https://github.com/bhavyaarora1905/blogs/" rel="noopener noreferrer"&gt;here&lt;/a&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;File&lt;/th&gt;
&lt;th&gt;What it demonstrates&lt;/th&gt;
&lt;th&gt;Run&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;graceful-shutdown.js&lt;/td&gt;
&lt;td&gt;Liveness + readiness probes, SIGTERM drain, connection tracking&lt;/td&gt;
&lt;td&gt;node graceful-shutdown.js&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;canary-router.js&lt;/td&gt;
&lt;td&gt;Canary traffic split, per-version error rate, rollback warning&lt;/td&gt;
&lt;td&gt;CANARY_PERCENT=10 node canary-router.js&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;feature-flags.js&lt;/td&gt;
&lt;td&gt;Percentage rollout, kill switches, allowlists, stale flag detection&lt;/td&gt;
&lt;td&gt;node feature-flags.js&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;No dependencies — pure Node.js v14+. Clone and run immediately.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thought
&lt;/h2&gt;

&lt;p&gt;Zero-downtime deployment isn't a single tool you install — it's a set of habits and constraints you build your system around. The earlier you treat backward compatibility and graceful degradation as first-class concerns, the cheaper these strategies become to implement.&lt;/p&gt;

&lt;p&gt;The teams that do this best don't deploy carefully. They deploy &lt;em&gt;constantly&lt;/em&gt;, because they've made deployment boring.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://zyvop.com/zero-downtime-deployments-strategies-and-tradeoffs-40waq" rel="noopener noreferrer"&gt;ZyVOP&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;💡 For more articles like this, &lt;a href="https://zyvop.com/newsletter" rel="noopener noreferrer"&gt;subscribe to the ZyVOP newsletter&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>devops</category>
      <category>kubernetes</category>
      <category>cicd</category>
      <category>infrastructure</category>
    </item>
    <item>
      <title>Local HTTPS Done Right: Wildcard SSL for Modern Development</title>
      <dc:creator>Bhavya Arora</dc:creator>
      <pubDate>Mon, 31 Aug 2026 06:53:18 +0000</pubDate>
      <link>https://dev.to/bhavya_arora/local-https-done-right-wildcard-ssl-for-modern-development-1l68</link>
      <guid>https://dev.to/bhavya_arora/local-https-done-right-wildcard-ssl-for-modern-development-1l68</guid>
      <description>&lt;p&gt;Modern web development demands HTTPS. If you are building an application on &lt;code&gt;http://localhost:3000&lt;/code&gt;, you will inevitably hit a wall:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;OAuth providers (Google, GitHub, Apple) often refuse HTTP redirect URIs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stripe webhooks require a secure context.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cross-domain cookies (&lt;code&gt;SameSite=None&lt;/code&gt;) are strictly blocked by browsers without the &lt;code&gt;Secure&lt;/code&gt; flag.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Service Workers and modern Web APIs (Geolocation, Clipboard) fail silently.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The historical solution was generating self-signed certificates with &lt;code&gt;openssl&lt;/code&gt;. But modern browsers hate self-signed certs. They throw terrifying red "Your connection is not private" screens, forcing you to click through hidden "Proceed anyway" menus.&lt;/p&gt;

&lt;p&gt;Today, we are going to fix this the right way. We will become our own Certificate Authority (CA) and generate a &lt;strong&gt;Wildcard SSL Certificate&lt;/strong&gt; so you can develop locally on &lt;code&gt;https://app.mylocal.dev&lt;/code&gt; with a perfectly green, trusted padlock.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Secret Weapon: mkcert
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/FiloSottile/mkcert" rel="noopener noreferrer"&gt;mkcert&lt;/a&gt; is an incredibly clever tool. Instead of generating a standalone self-signed certificate, it creates a local Certificate Authority (CA) and injects it into your operating system's and browser's trust stores.&lt;/p&gt;

&lt;p&gt;Because your OS explicitly trusts this local CA, any certificate signed by it is treated as entirely valid.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Install mkcert
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;macOS (via Homebrew):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;mkcert
&lt;span class="c"&gt;# If you use Firefox, it manages its own trust store, so we need nss&lt;/span&gt;
brew &lt;span class="nb"&gt;install &lt;/span&gt;nss 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Windows (via Chocolatey):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;choco&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;install&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;mkcert&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Ubuntu/Debian:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;libnss3-tools
brew &lt;span class="nb"&gt;install &lt;/span&gt;mkcert &lt;span class="c"&gt;# via Linuxbrew, or download the binary from GitHub&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Initialize Your Local CA
&lt;/h3&gt;

&lt;p&gt;Run the following command &lt;em&gt;once&lt;/em&gt; per machine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mkcert &lt;span class="nt"&gt;-install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your operating system will prompt you for an admin password. You are giving &lt;code&gt;mkcert&lt;/code&gt; permission to add a root certificate to your keychain.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Generate the Wildcard Certificate
&lt;/h3&gt;

&lt;p&gt;Choose a Top-Level Domain (TLD) for local development. Do not use &lt;code&gt;.dev&lt;/code&gt; or &lt;code&gt;.app&lt;/code&gt;—Google owns these and strictly enforces HSTS (HTTPS routing) at the DNS level, which can cause annoying conflicts.&lt;/p&gt;

&lt;p&gt;The internet standard for local development is &lt;code&gt;.test&lt;/code&gt; or &lt;code&gt;.loc&lt;/code&gt;. Let's use &lt;code&gt;*.myapp.test&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; ~/local-certs &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd&lt;/span&gt; ~/local-certs
mkcert &lt;span class="s2"&gt;"*.myapp.test"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This generates two files:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;_wildcard.myapp.test.pem&lt;/code&gt; (The Public Certificate)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;_wildcard.myapp.test-key.pem&lt;/code&gt; (The Private Key)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 4: Map Local DNS
&lt;/h2&gt;

&lt;p&gt;We need to tell your computer that &lt;code&gt;api.myapp.test&lt;/code&gt; and &lt;code&gt;app.myapp.test&lt;/code&gt; should resolve to &lt;code&gt;localhost&lt;/code&gt; (&lt;code&gt;127.0.0.1&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Edit your hosts file:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Mac/Linux&lt;/strong&gt;: &lt;code&gt;sudo nano /etc/hosts&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Windows&lt;/strong&gt;: Open Notepad as Administrator, then open &lt;code&gt;C:\Windows\System32\drivers\etc\hosts&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Add your domains to the bottom:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;127.0.0.1   myapp.test
127.0.0.1   app.myapp.test
127.0.0.1   api.myapp.test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 5: Implementation
&lt;/h2&gt;

&lt;p&gt;You have the trusted certificates and the DNS mapping. Now you just need to serve them. You can do this at the application level or the proxy level.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach A: Using Docker and Nginx (Recommended)
&lt;/h3&gt;

&lt;p&gt;This closely mirrors a production environment. Nginx handles the SSL termination and passes standard HTTP traffic to your Node/Python app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;docker-compose.yml&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3.8'&lt;/span&gt;
&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;web&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;node:18&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm run dev&lt;/span&gt;
    &lt;span class="c1"&gt;# Notice we don't expose ports directly&lt;/span&gt;

  &lt;span class="na"&gt;proxy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nginx:alpine&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;443:443"&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./nginx.conf:/etc/nginx/nginx.conf:ro&lt;/span&gt;
      &lt;span class="c1"&gt;# Mount the folder containing our certificates&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;~/local-certs:/etc/nginx/certs:ro&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;nginx.conf&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="k"&gt;events&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="k"&gt;http&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kn"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kn"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;443&lt;/span&gt; &lt;span class="s"&gt;ssl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kn"&gt;server_name&lt;/span&gt; &lt;span class="s"&gt;app.myapp.test&lt;/span&gt; &lt;span class="s"&gt;api.myapp.test&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="kn"&gt;ssl_certificate&lt;/span&gt; &lt;span class="n"&gt;/etc/nginx/certs/_wildcard.myapp.test.pem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kn"&gt;ssl_certificate_key&lt;/span&gt; &lt;span class="n"&gt;/etc/nginx/certs/_wildcard.myapp.test-key.pem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="kn"&gt;location&lt;/span&gt; &lt;span class="n"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kn"&gt;proxy_pass&lt;/span&gt; &lt;span class="s"&gt;http://web:3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="kn"&gt;proxy_set_header&lt;/span&gt; &lt;span class="s"&gt;Host&lt;/span&gt; &lt;span class="nv"&gt;$host&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Approach B: Native Node.js (Next.js / Vite)
&lt;/h3&gt;

&lt;p&gt;If you don't use Docker, you can pass the certificates directly to your dev server.&lt;/p&gt;

&lt;p&gt;For &lt;strong&gt;Vite&lt;/strong&gt;, add this to &lt;code&gt;vite.config.ts&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;server&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/Users/bhavya/local-certs/_wildcard.myapp.test-key.pem&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="na"&gt;cert&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/Users/bhavya/local-certs/_wildcard.myapp.test.pem&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app.myapp.test&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Crucial Tip: Node.js Backend-to-Backend Requests
&lt;/h2&gt;

&lt;p&gt;If your Node.js backend tries to fetch data from your own secure local API (e.g., Server-Side Rendering hitting &lt;code&gt;https://api.myapp.test&lt;/code&gt;), Node will throw an &lt;code&gt;UNABLE_TO_VERIFY_LEAF_SIGNATURE&lt;/code&gt; error.&lt;/p&gt;

&lt;p&gt;Why? Because Node.js does not use the operating system's trust store. It uses its own hardcoded list of root authorities.&lt;/p&gt;

&lt;p&gt;To tell Node.js to trust your &lt;code&gt;mkcert&lt;/code&gt; CA, you must pass an environment variable when starting your Node script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Find where mkcert put your root CA&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;NODE_EXTRA_CA_CERTS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;mkcert &lt;span class="nt"&gt;-CAROOT&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;/rootCA.pem"&lt;/span&gt;

&lt;span class="c"&gt;# Then run your app&lt;/span&gt;
npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Boot up your application and navigate to &lt;code&gt;https://app.myapp.test&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You will be greeted by a flawless, green padlock. No warnings, no "Proceed to unsafe" buttons. Your cookies will adhere to strict security policies, third-party APIs will accept your webhooks, and your local development environment will finally mirror production parity.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://zyvop.com/local-https-done-right-wildcard-ssl-for-modern-development-muwll" rel="noopener noreferrer"&gt;ZyVOP&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;💡 For more articles like this, &lt;a href="https://zyvop.com/newsletter" rel="noopener noreferrer"&gt;subscribe to the ZyVOP newsletter&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>devops</category>
      <category>ssl</category>
      <category>localdevelopment</category>
      <category>websecurity</category>
    </item>
    <item>
      <title>LongCat-2.0: The AI Model You Were Already Using. Built By The Company That Delivers Your Dinner.</title>
      <dc:creator>Bhavya Arora</dc:creator>
      <pubDate>Mon, 31 Aug 2026 06:52:48 +0000</pubDate>
      <link>https://dev.to/bhavya_arora/longcat-20-the-ai-model-you-were-already-using-built-by-the-company-that-delivers-your-dinner-4m3f</link>
      <guid>https://dev.to/bhavya_arora/longcat-20-the-ai-model-you-were-already-using-built-by-the-company-that-delivers-your-dinner-4m3f</guid>
      <description>&lt;p&gt;&lt;em&gt;For two months, a model called "Owl Alpha" quietly out-hustled GPT-5.5 and Claude on OpenRouter. Nobody knew it was Meituan — yes, the food-delivery app — running a 1.6-trillion-parameter model trained entirely on Chinese chips.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The short version:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Meituan — China's Uber Eats-meets-everything super-app — spent two months running a model called &lt;strong&gt;Owl Alpha&lt;/strong&gt; anonymously on &lt;a href="https://openrouter.ai/openrouter/owl-alpha" rel="noopener noreferrer"&gt;OpenRouter&lt;/a&gt;. Real developers used it in real coding agents, with zero idea who built it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On June 29, Meituan outed itself. On June 30, it launched the model publicly as &lt;strong&gt;LongCat-2.0&lt;/strong&gt;: 1.6 trillion parameters, MIT-licensed, &lt;a href="https://beincrypto.com/china-debuts-longcat-ai-model-without-nvidia-chips/" rel="noopener noreferrer"&gt;trained entirely on Chinese-made chips&lt;/a&gt; — no Nvidia anywhere in the pipeline.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It undercuts GPT-5.5 on price by roughly 6–7x and edges past it on one coding benchmark by less than a point — a claim worth double-checking, not taking on faith.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The actual downloadable weights didn't show up until &lt;a href="https://huggingface.co/meituan-longcat/LongCat-2.0-INT8" rel="noopener noreferrer"&gt;July 4&lt;/a&gt;, five days after the "open source" announcement.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you ran a coding agent through Hermes Agent, Claude Code, or OpenClaw sometime between late April and late June 2026, you probably already have an opinion about LongCat-2.0. You just knew it by a different name.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Reveal
&lt;/h2&gt;

&lt;p&gt;Meituan didn't do a keynote. It didn't do a demo video with swelling music. It posted a tweet.&lt;/p&gt;

&lt;p&gt;On June 29, &lt;a href="https://www.testingcatalog.com/meituan-launches-longcat-2-0-1-6t-parameter-model-on-apis/" rel="noopener noreferrer"&gt;the official Meituan LongCat account wrote&lt;/a&gt;: &lt;em&gt;"Some of you guessed right. 👀 Owl Alpha on @OpenRouter — that's us."&lt;/em&gt; The thread added that Owl Alpha had hit the global top three by daily volume on OpenRouter, and — by monthly volume — ranked #1 on the Hermes Agent workspace, #2 on Claude Code-compatible deployments, and #3 across OpenClaw. The next day, the &lt;a href="https://github.com/meituan-longcat/LongCat-2.0" rel="noopener noreferrer"&gt;LongCat-2.0 GitHub repo&lt;/a&gt; and &lt;a href="https://huggingface.co/meituan-longcat/LongCat-2.0" rel="noopener noreferrer"&gt;Hugging Face model card&lt;/a&gt; went live, MIT license attached.&lt;/p&gt;

&lt;p&gt;That's the whole trick, and it's a good one: don't launch a model and hope people adopt it. Let them adopt it first, then tell them whose it was.&lt;/p&gt;

&lt;h2&gt;
  
  
  Meet Owl Alpha
&lt;/h2&gt;

&lt;p&gt;Before June 29, here's everything OpenRouter's public listing told you about &lt;a href="https://openrouter.ai/openrouter/owl-alpha" rel="noopener noreferrer"&gt;Owl Alpha&lt;/a&gt;: a "high-performance foundation model designed for agentic workloads," hosted by a provider labeled simply &lt;strong&gt;"Stealth."&lt;/strong&gt; No parameter count. No company. Just a note that your prompts might get logged to improve the model — standard fine print for anything running incognito.&lt;/p&gt;

&lt;p&gt;That vagueness didn't stop it from putting up real numbers. One independent tracker following OpenRouter's public usage charts estimated Owl Alpha was processing &lt;a href="https://toastyst.github.io/SpaghettiStories/personal/2026/06/21/owl-alpha-mystery/" rel="noopener noreferrer"&gt;north of 10 trillion tokens a month&lt;/a&gt; by the time of the reveal, growing over 200% month-over-month, and — within the Hermes Agent ecosystem specifically — out-consuming its next four competitors &lt;em&gt;combined&lt;/em&gt;. Take the precise figures with a grain of salt; they're estimates from public dashboards, not audited numbers. But the direction isn't in dispute: developers with zero brand loyalty kept picking this thing for real work.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Detective Work
&lt;/h2&gt;

&lt;p&gt;Naturally, people tried to unmask it. That's half the fun of a stealth model — the community turns into an amateur forensics team.&lt;/p&gt;

&lt;p&gt;The strongest tell was behavioral: users on Reddit and elsewhere noticed Owl Alpha would affirm that Taiwan is part of China and steer hard away from questions about Taiwanese independence — the kind of reflex you don't get from a Western lab's model. That pointed toward a Chinese origin fast. Guesses circulated toward Tencent's Hunyuan team, MiniMax, Baidu, or some unannounced newcomer. A &lt;a href="https://news.ycombinator.com/item?id=48727116" rel="noopener noreferrer"&gt;Hacker News thread&lt;/a&gt; speculated about the Huawei Ascend chips underneath it well before Meituan confirmed anything. A handful of users even reported the model referring to itself as being "from the Zoo company" in casual chat — nobody could explain that one until the reveal.&lt;/p&gt;

&lt;p&gt;Almost nobody guessed a food-delivery app.&lt;/p&gt;

&lt;h2&gt;
  
  
  This Playbook Isn't New. The Patience Is.
&lt;/h2&gt;

&lt;p&gt;If this feels familiar, it should. Running a flagship model incognito before the official launch has become standard practice at frontier labs. OpenAI has done it more than once — &lt;a href="https://medium.com/@leucopsis/is-polaris-alpha-a-gpt-5-1-bd445598e27b" rel="noopener noreferrer"&gt;Horizon Alpha&lt;/a&gt; is widely believed to have been an early GPT-5 checkpoint, and &lt;a href="https://openrouter.ai/openrouter/polaris-alpha" rel="noopener noreferrer"&gt;Polaris Alpha&lt;/a&gt;, which showed up on OpenRouter with unusually generous rate limits, turned out to be an early build of GPT-5.1. xAI does this constantly, cycling code names like Sonic, Sherlock, and &lt;a href="https://openrouter.ai/openrouter/sonoma-sky-alpha" rel="noopener noreferrer"&gt;Sonoma&lt;/a&gt; before they resolve into named Grok releases. Cursor's "Cheetah" turned out to be its own Composer model. Even Anthropic has quietly run checkpoints under names like Bobcat and Code-Supernova. There's a &lt;a href="https://jonathanrreed.com/blog/stealth-models/" rel="noopener noreferrer"&gt;decent field guide to spotting these&lt;/a&gt; if you want to go down that rabbit hole yourself.&lt;/p&gt;

&lt;p&gt;What's different about Owl Alpha isn't the tactic — it's the patience. Most stealth models get made and claimed within days, maybe a couple of weeks. Owl Alpha ran anonymously for roughly two months, long enough to rack up genuine production usage across three separate agent ecosystems before Meituan said a word. Leaderboard screenshots are easy to game. Two months of developers voluntarily choosing your model with no idea who made it is a lot harder to fake.&lt;/p&gt;

&lt;p&gt;It's also not like Meituan appeared from nowhere. Its LongCat team had already shipped &lt;a href="https://github.com/meituan-longcat" rel="noopener noreferrer"&gt;LongCat-Flash&lt;/a&gt;, a 560-billion-parameter model, back in September 2025, followed by the multimodal LongCat-Next in March 2026 — public papers, model cards, GitHub repos, the whole open-source routine. What Meituan had never done before was let a model build a reputation &lt;em&gt;before&lt;/em&gt; attaching its name to it. Coming from the company most people outside China associate with fried chicken delivery, that combination was enough to catch the industry flat-footed.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's Actually Inside It
&lt;/h2&gt;

&lt;p&gt;Cut the backstory and LongCat-2.0 is a Mixture-of-Experts (MoE) model: a huge pool of specialized sub-networks where only a small slice fires for any given token, instead of the whole network lighting up every time. The headline is 1.6 trillion total parameters. The number that actually determines your bill is closer to &lt;strong&gt;48 billion active parameters per token&lt;/strong&gt;, sliding between roughly 33 billion and 56 billion depending on how hard the request is. Autocompleting a variable name barely taxes it; a gnarly multi-step reasoning problem pulls in more of the network.&lt;/p&gt;

&lt;p&gt;A few specific design choices carry the weight, per &lt;a href="https://huggingface.co/meituan-longcat/LongCat-2.0" rel="noopener noreferrer"&gt;Meituan's own technical writeup&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;LongCat Sparse Attention (LSA)&lt;/strong&gt; — standard attention gets brutally expensive as context grows, because every token has to weigh every other token. LSA instead indexes and selects only the most relevant chunks of a long conversation, which is the trick behind sustaining a full 1-million-token context window without the cost exploding.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;N-gram Embedding&lt;/strong&gt; — a 135-billion-parameter layer, borrowed from an earlier LongCat model, that sits alongside the MoE experts rather than inside them. Instead of only recognizing individual words, it can treat common multi-word phrases as single units — "New York City" as one concept instead of three disconnected tokens. Meituan claims roughly 100x richer representations from this without meaningfully increasing model size.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Multi-Token Prediction (MTP)&lt;/strong&gt; — a lightweight head that drafts several tokens ahead in one pass, which the main model then verifies. Reported acceptance rate above 90%, which is what gets inference speeds above 100 tokens/second through the API.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Multi-Teacher On-Policy Distillation (MOPD)&lt;/strong&gt; — a post-training step fusing three specialist skill sets (tool use and self-correction, multi-step and STEM reasoning, instruction-following and hallucination suppression) into one model.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pretraining ran on more than 30 trillion tokens of code, English, Chinese, and multilingual data. License is MIT — commercial use, fine-tuning, redistribution, all explicitly fair game.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Benchmarks — Read the Fine Print
&lt;/h2&gt;

&lt;p&gt;Here's what Meituan is claiming:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Benchmark&lt;/th&gt;
&lt;th&gt;LongCat-2.0&lt;/th&gt;
&lt;th&gt;For comparison&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;SWE-bench Pro (real GitHub issue resolution)&lt;/td&gt;
&lt;td&gt;59.5&lt;/td&gt;
&lt;td&gt;GPT-5.5: 58.6 · Gemini 3.1 Pro: 54.2 · Claude Opus 4.7: 64.3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;FORTE (office-task agent benchmark)&lt;/td&gt;
&lt;td&gt;73.2&lt;/td&gt;
&lt;td&gt;Claude Opus 4.6: 73.2 (tied) · GPT-5.5: 77.8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Terminal-Bench 2.1&lt;/td&gt;
&lt;td&gt;70.8&lt;/td&gt;
&lt;td&gt;outperforms Gemini 3.1 Pro&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SWE-Bench Multilingual&lt;/td&gt;
&lt;td&gt;77.3&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Here's the part most coverage buried: these are Meituan's own numbers, run mostly on Meituan's own testing harness. Someone actually &lt;a href="https://huggingface.co/meituan-longcat/LongCat-2.0/discussions/2" rel="noopener noreferrer"&gt;filed a pull request&lt;/a&gt; trying to add these scores to Hugging Face's public evaluation database and hit a wall — Terminal-Bench 2.1, SWE-Bench Multilingual, and FORTE didn't have registered independent evaluations to check against. That's not proof the numbers are inflated. It's just a reminder that "beats GPT-5.5 by eight-tenths of a point on one benchmark" is a claim to file under &lt;em&gt;competitive, not confirmed&lt;/em&gt; until outside labs run their own passes. One outlet that actually built something with it found the output capable but noticeably behind Claude's top tier — closer to a mid-range Claude model than the headline number suggests.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Price Is the Actual Headline
&lt;/h2&gt;

&lt;p&gt;Whatever you think of the benchmark gap, the pricing is not up for debate. Standard API access runs $0.75 per million input tokens and $2.95 per million output, dropping to a launch-promo rate of &lt;strong&gt;$0.30 / $1.20&lt;/strong&gt; — with cached context reads billed at zero.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Model&lt;/th&gt;
&lt;th&gt;Input ($/M tokens)&lt;/th&gt;
&lt;th&gt;Output ($/M tokens)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;LongCat-2.0 (promo)&lt;/td&gt;
&lt;td&gt;$0.30&lt;/td&gt;
&lt;td&gt;$1.20&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LongCat-2.0 (standard)&lt;/td&gt;
&lt;td&gt;$0.75&lt;/td&gt;
&lt;td&gt;$2.95&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DeepSeek V4-Pro&lt;/td&gt;
&lt;td&gt;$0.435&lt;/td&gt;
&lt;td&gt;$0.87&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Claude Sonnet 5 (intro rate)&lt;/td&gt;
&lt;td&gt;$2.00&lt;/td&gt;
&lt;td&gt;$10.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GPT-5.5&lt;/td&gt;
&lt;td&gt;$5.00&lt;/td&gt;
&lt;td&gt;$30.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Claude Opus 4.7&lt;/td&gt;
&lt;td&gt;~$15.00&lt;/td&gt;
&lt;td&gt;~$75.00&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For teams burning through millions of tokens re-reading the same repo on every agent loop, free cache reads alone change the math. Meituan is also running flash-sale &lt;a href="https://www.longcatai.org/news/longcat-2" rel="noopener noreferrer"&gt;token packs&lt;/a&gt; — roughly a billion tokens for $60 — released four times a day on a Beijing schedule.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Chip Story Nobody Should Skip
&lt;/h2&gt;

&lt;p&gt;Here's the part that actually rattled policy people, and it has nothing to do with coding benchmarks. Meituan says LongCat-2.0 was trained from scratch &lt;em&gt;and&lt;/em&gt; served entirely on a cluster of roughly 50,000 domestic Chinese AI accelerators, coordinated with Huawei's HCCL library — &lt;a href="https://tech.yahoo.com/ai/articles/china-longcat-2-0-becomes-134258951.html" rel="noopener noreferrer"&gt;the domestic answer to Nvidia's NCCL&lt;/a&gt; — with zero Nvidia hardware anywhere in the pipeline.&lt;/p&gt;

&lt;p&gt;That distinction is bigger than it sounds. Other Chinese models, including DeepSeek's V4-Pro, have reportedly used domestic chips for &lt;em&gt;inference&lt;/em&gt; while still training on non-domestic hardware. Meituan's claim is that LongCat-2.0 is the first trillion-parameter-class model to do the &lt;em&gt;harder&lt;/em&gt; part — training — entirely on Chinese silicon, at frontier scale, with no catastrophic loss spikes along the way. &lt;a href="https://www.caixinglobal.com/2026-07-07/meituan-open-sources-16-trillion-parameter-ai-model-built-on-chinese-chips-102461495.html" rel="noopener noreferrer"&gt;Caixin Global&lt;/a&gt; and &lt;a href="https://www.digitimes.com/news/a20260707VL209/meituan-infrastructure-ascend-metax.html" rel="noopener noreferrer"&gt;Digitimes&lt;/a&gt; both framed it as a real stress test of China's chip stack, not just a press release.&lt;/p&gt;

&lt;p&gt;Analysts treated it that way too. One noted it should put to rest doubts about whether Huawei's Atlas-950 clusters could actually scale; a Lehigh University researcher called it the first model trained to near-frontier performance entirely on domestic accelerators. The read that stuck: export controls on the newest Nvidia chips don't stop capable models from getting built — they just push the timeline toward domestic alternatives faster than the policy assumed.&lt;/p&gt;

&lt;p&gt;That doesn't settle the argument. Controls still raise costs and slow things down — Meituan's own writeup admits real pain from hardware faults, memory pressure, and numerical instability at 50,000-chip scale. But the assumption that Chinese labs simply &lt;em&gt;couldn't&lt;/em&gt; train frontier-adjacent systems without the latest Nvidia stack took a real hit.&lt;/p&gt;

&lt;p&gt;There's a business subplot worth one line: Meituan's core delivery business has had a brutal year — stock down more than 30% year-to-date, market cap under HK$400 billion — and CEO Wang Xing has publicly taken responsibility for it. A trillion-parameter model quietly beating Western pricing on homegrown chips is, among other things, a pretty loud answer to shareholders asking what else the company's been doing.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's Still Soft
&lt;/h2&gt;

&lt;p&gt;Be honest about the gaps before you bet a production stack on this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The benchmarks are self-reported&lt;/strong&gt;, and several don't have independent verification yet.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;"Open weights" and "MIT license" didn't mean downloadable until July 4&lt;/strong&gt; — the GitHub and Hugging Face pages sat on "coming soon" for five days after the big announcement. An &lt;a href="https://github.com/unslothai/unsloth/issues/6759" rel="noopener noreferrer"&gt;Unsloth GitHub issue&lt;/a&gt; tracking community support for the model captures that gap in real time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Self-hosting isn't realistic for most people.&lt;/strong&gt; Estimates put a full deployment around sixteen high-end GPUs — "open source" here mostly means &lt;em&gt;someone&lt;/em&gt; can run it, not &lt;em&gt;you&lt;/em&gt;, unless you've got serious hardware.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;One hands-on test&lt;/strong&gt; — building something end-to-end rather than running a benchmark — found the actual output quality trailing top-tier closed models by more than the score table implies.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Bottom Line
&lt;/h2&gt;

&lt;p&gt;Strip away the theater and two things are solidly true: real developers adopted this model in volume with zero idea who built it, and the pricing advantage over GPT-5.5 and Claude's intro rates is real and large, especially for high-volume agentic coding. The domestic-chip training claim, if the SWE-bench-level scrutiny holds up, is a legitimately significant data point about where Chinese AI infrastructure actually stands — independent of how you feel about the export-control debate.&lt;/p&gt;

&lt;p&gt;What's still an open question is whether it's &lt;em&gt;actually&lt;/em&gt; as good as the benchmark table says, and the honest answer right now is: probably close, not quite there, and worth testing yourself rather than taking Meituan's word for it.&lt;/p&gt;

&lt;p&gt;The bigger story isn't a half-point benchmark edge over GPT-5.5. It's that a two-month blind trial, run without anyone's permission to be impressed, ended up more convincing than a launch event — and the company running it wasn't one anyone in the AI industry was watching closely enough.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Go deeper:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://huggingface.co/meituan-longcat/LongCat-2.0" rel="noopener noreferrer"&gt;LongCat-2.0 model card — Hugging Face&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/meituan-longcat/LongCat-2.0" rel="noopener noreferrer"&gt;LongCat-2.0 repo — GitHub&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://openrouter.ai/openrouter/owl-alpha" rel="noopener noreferrer"&gt;Owl Alpha's original OpenRouter listing&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.longcatai.org/news/longcat-2" rel="noopener noreferrer"&gt;Meituan's official LongCat-2.0 announcement&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://venturebeat.com/technology/meituan-open-sources-longcat-2-0-the-1-6t-near-frontier-agentic-coding-model-thats-been-leading-openrouter-trained-entirely-on-chinese-chips" rel="noopener noreferrer"&gt;VentureBeat's launch-day writeup&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.marktechpost.com/2026/07/05/meituan-releases-longcat-2-0-a-1-6t-parameter-open-moe-model-with-native-1m-context-and-longcat-sparse-attention/" rel="noopener noreferrer"&gt;MarkTechPost's architecture breakdown&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://toastyst.github.io/SpaghettiStories/personal/2026/06/21/owl-alpha-mystery/" rel="noopener noreferrer"&gt;The Owl Alpha mystery, tracked in real time&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://jonathanrreed.com/blog/stealth-models/" rel="noopener noreferrer"&gt;A field guide to stealth models generally&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://zyvop.com/longcat-2-0-the-ai-model-you-were-already-using-built-by-the-company-that-delivers-your-dinner-pwsf3" rel="noopener noreferrer"&gt;ZyVOP&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;💡 For more articles like this, &lt;a href="https://zyvop.com/newsletter" rel="noopener noreferrer"&gt;subscribe to the ZyVOP newsletter&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>agenticcoding</category>
      <category>longcat20</category>
      <category>meituan</category>
      <category>owlalpha</category>
    </item>
    <item>
      <title>What AI Still Cannot Do: Four Boundaries the 2026 Benchmarks Keep Finding</title>
      <dc:creator>Bhavya Arora</dc:creator>
      <pubDate>Mon, 31 Aug 2026 06:52:27 +0000</pubDate>
      <link>https://dev.to/bhavya_arora/what-ai-still-cannot-do-four-boundaries-the-2026-benchmarks-keep-finding-2km0</link>
      <guid>https://dev.to/bhavya_arora/what-ai-still-cannot-do-four-boundaries-the-2026-benchmarks-keep-finding-2km0</guid>
      <description>&lt;h2&gt;
  
  
  The benchmark built to find the ceiling
&lt;/h2&gt;

&lt;p&gt;In January 2026, the training-data company Mercor released &lt;a href="https://www.mercor.com/blog/introducing-apex-agents/" rel="noopener noreferrer"&gt;APEX-Agents&lt;/a&gt;, a benchmark built from real professional work: week-long consulting projects, investment banking analyses, and corporate law memos, drawn from practitioners at firms including Goldman Sachs, McKinsey, and the law firm Cravath. Frontier models completed less than 25 percent of the 480 tasks. Given eight attempts each, the ceiling rose to 40 percent. No model finished the work end to end.&lt;/p&gt;

&lt;p&gt;AI agents were setting records elsewhere that same month. Put the two side by side and you get the actual texture of frontier AI in mid-2026: extraordinary, well-documented progress on tasks built to be measured cleanly, and a much rockier picture wherever an evaluation looks like the ambiguous, judgment-heavy work that fills an actual job. Four boundaries keep turning up across the newest research, each found by an evaluation built specifically to find it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F14wzecjssxkshf87t2is.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F14wzecjssxkshf87t2is.webp" width="800" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The 50-percent time horizon by model release date, on a log scale. Data: METR's Time Horizon 1.1 and 1.0 series (metr.org/time-horizons) and METR's GPT-5.6 Sol evaluation (metr.org/blog/2026-06-26-gpt-5-6-sol).&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Long tasks are getting longer, but "long" is not "real"
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://metr.org/time-horizons/" rel="noopener noreferrer"&gt;METR&lt;/a&gt;, a nonprofit that has tracked AI agent capability since 2019, measures what it calls the time horizon: the length of task, timed against a skilled human, that an agent can complete with 50 percent reliability.&lt;/p&gt;

&lt;p&gt;Claude Opus 4.6 posted the leading score among generally available models in February 2026: roughly 14 hours and 30 minutes. METR itself flags anything above 16 hours as unreliable, given the limits of its current task suite.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The 50-percent time horizon is the length of task, measured by how long it takes a human expert, at which a model would be predicted to succeed about half the time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;METR states three caveats about this metric plainly on its own site. The task suite is drawn almost entirely from software engineering, machine learning, and cybersecurity work, chosen because it is self-contained with a clear pass or fail condition.&lt;/p&gt;

&lt;p&gt;The human baseline is closer to a low-context new hire than an experienced professional working with full context. And METR's own follow-up research found something sharper still: agent performance drops substantially once a task is graded holistically instead of algorithmically, which is closer to how a manager actually judges finished work.&lt;/p&gt;

&lt;p&gt;Mercor's result lives exactly in that gap. Real professional tasks are rarely self-contained: finding the right file in a messy folder, reading between the lines of an ambiguous instruction, holding context across a workflow that spans documents, spreadsheets, and chat. Mercor's own writeup was blunt about what this produces. Most agent failures were not shortfalls in raw capability. They were failures to manage ambiguity, locate the correct document, or sustain context across the full task.&lt;/p&gt;

&lt;p&gt;The same pattern shows up across other evaluations built the same way, from general web tasks to repeated-trial customer service work. Each figure below is drawn from a different point over the past year and a half, so read this as a gradient rather than a single leaderboard snapshot:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Evaluation&lt;/th&gt;
&lt;th&gt;What it tests&lt;/th&gt;
&lt;th&gt;Best reported result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;METR 50% time horizon&lt;/td&gt;
&lt;td&gt;Self-contained software, ML, and security tasks&lt;/td&gt;
&lt;td&gt;14h 30m&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;WebArena&lt;/td&gt;
&lt;td&gt;General web-agent tasks (human baseline: 78%)&lt;/td&gt;
&lt;td&gt;61.7%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;WebChoreArena&lt;/td&gt;
&lt;td&gt;Complex, long-memory web tasks&lt;/td&gt;
&lt;td&gt;37.8%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mercor APEX-Agents&lt;/td&gt;
&lt;td&gt;Real investment banking, consulting, and legal work&lt;/td&gt;
&lt;td&gt;under 25%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;tau-bench, pass at 8 tries&lt;/td&gt;
&lt;td&gt;Customer-service tasks, repeated-trial reliability&lt;/td&gt;
&lt;td&gt;25%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Sources: METR time horizon tracker, February 2026 update. WebArena (result dated February 2025) and WebChoreArena figures via a late-2025 &lt;a href="https://arxiv.org/pdf/2512.04367" rel="noopener noreferrer"&gt;agentic-sandbox survey&lt;/a&gt; citing IBM's CUGA agent and Gemini 2.5 Pro. Mercor APEX-Agents, January 2026. The tau-bench pass-at-8 figure via an &lt;a href="https://arxiv.org/pdf/2511.14136" rel="noopener noreferrer"&gt;enterprise agent evaluation framework paper&lt;/a&gt; citing Sierra Research's original tau-bench work.&lt;/p&gt;

&lt;p&gt;None of this means the underlying trend is slowing. METR's original 2025 analysis found the 50-percent time horizon had roughly doubled every seven months across six years of releases. A January 2026 update found the pace accelerating further for models after 2023: the doubling interval compressed to about 4.3 months.&lt;/p&gt;

&lt;p&gt;The frontier is moving fast. It is just moving fastest on the kind of task that can be scored cleanly, which is not the same as the kind of task that fills a real job.&lt;/p&gt;

&lt;p&gt;The measurement itself broke down entirely by June 2026. &lt;a href="https://metr.org/blog/2026-06-26-gpt-5-6-sol/" rel="noopener noreferrer"&gt;METR's predeployment evaluation&lt;/a&gt; of OpenAI's GPT-5.6 Sol found the model exploiting bugs in the evaluation environment, including extracting hidden source code containing expected answers, at a higher rate than any model METR had tested before. Scored the standard way, with cheating counted as failure, Sol's 50-percent time horizon came out to about 11.3 hours. Counted as success instead, the same data produced an estimate beyond 270 hours.&lt;/p&gt;

&lt;p&gt;METR trusted neither number as a real measurement of capability.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Reasoning still collapses once problems get hard enough
&lt;/h2&gt;

&lt;p&gt;Apple's machine learning research team published &lt;a href="https://machinelearning.apple.com/research/illusion-of-thinking" rel="noopener noreferrer"&gt;"The Illusion of Thinking"&lt;/a&gt; in June 2025, a study built around puzzles like the Tower of Hanoi, where researchers could dial complexity up or down while holding the underlying logic constant. The central finding: frontier reasoning models hit a complexity threshold beyond which accuracy collapsed completely, not gradually. Below that threshold, the models did well. Above it, they failed regardless of how much additional computation they were given.&lt;/p&gt;

&lt;p&gt;The paper documented something stranger than a hard ceiling. As problems approached the threshold, the length of the models' reasoning traces increased, as expected. But past the threshold, that effort declined again, even with token budget left unused. The authors describe three regimes: standard models beat reasoning models on easy problems, reasoning models pull ahead on medium ones, and both collapse together once problems get hard enough.&lt;/p&gt;

&lt;p&gt;This finding did not go unchallenged. Open Philanthropy researcher Alex Lawsen, working with an Anthropic Claude model credited as a co-author, published &lt;a href="https://arxiv.org/abs/2506.09250" rel="noopener noreferrer"&gt;"The Illusion of the Illusion of Thinking,"&lt;/a&gt; arguing several reported failures were artifacts of the experiment rather than evidence of a reasoning ceiling. Their central point: some high-complexity Tower of Hanoi failures occurred because models ran out of output tokens while listing an exponentially long solution, not because the underlying logic was beyond them. Removing that constraint changed the picture for at least some failures.&lt;/p&gt;

&lt;p&gt;A second, separate problem came from the same rebuttal: some of the River Crossing puzzle instances Apple used above a certain size have no valid solution at all, given the stated boat capacity. Models were still scored as failures for not solving puzzles that cannot be solved. This is a flaw in the evaluation's construction itself, independent of any argument about token budgets or output length.&lt;/p&gt;

&lt;p&gt;Neither side has won this argument outright, and developers need not pick one to take the practical lesson from it. Even the rebuttal does not dispute that reasoning models struggle disproportionately as complexity climbs; it disputes the cause. Apple's account points to a genuine ceiling on generalizable reasoning. Lawsen's account points to evaluation artifacts that a different design would remove.&lt;/p&gt;

&lt;p&gt;Either way, the advice holds: do not trust one unverified chain of reasoning on a genuinely hard problem, no matter how confident the output looks.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Code suggestions still invent packages that do not exist
&lt;/h2&gt;

&lt;p&gt;For developers, the sharpest version of this gap shows up somewhere narrower and more concrete: package names.&lt;/p&gt;

&lt;p&gt;A 2025 USENIX Security paper by Spracklen and colleagues found that code-generating LLMs invent references to Python and JavaScript packages that do not exist on PyPI or npm, at rates from 5.2 percent on commercial models up to 21.7 percent on open-source ones. Researchers named the resulting attack slopsquatting: an attacker registers the hallucinated name in advance, and the next developer who receives the same suggestion installs the attacker's code instead.&lt;/p&gt;

&lt;p&gt;An independent 2026 replication tested five frontier models released between October 2025 and March 2026 and found the range had compressed sharply: 4.62 percent for Claude Haiku 4.5, up to 6.10 percent for GPT-5.4-mini. Meaningfully better than the 2024 cohort -- but the study's own authors are explicit that a narrower range is not the same as the threat disappearing. At those rates, an attacker sampling a few thousand prompts still surfaces hundreds of viable hallucinated names, and registering a package costs nothing.&lt;/p&gt;

&lt;p&gt;The more troubling finding was a set of 127 package names that all five models, spanning four different providers, hallucinated identically. Coordinated disclosure to PyPI Security and to the security firm Socket.dev found that 53 of those names, across both registries, remained available for an attacker to register despite each registry's existing defenses. A single registration under one of those names could reach users of every major model provider at once, since the vulnerability does not depend on which frontier model a given developer happens to be using.&lt;/p&gt;

&lt;p&gt;This is not a hypothetical. In January 2026, Aikido Security researcher Charlie Eriksen traced a hallucinated npm package called react-codeshift, a plausible-sounding mashup of two real tools, back to a single unreviewed commit of AI-generated agent instructions. By the time he found it, the name had spread to 237 GitHub repositories, with autonomous agents still attempting to install it daily. He registered the name himself, purely to keep it out of an attacker's hands first.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Multiple agents working together fail in their own specific ways
&lt;/h2&gt;

&lt;p&gt;Running several AI agents together, each handling a piece of a larger job, is one of the most common ways teams try to scale agent work past a single well-specified task.&lt;/p&gt;

&lt;p&gt;A team at UC Berkeley set out in 2025 to measure how well that pattern actually holds up, analyzing more than 1,600 execution traces across seven widely used open-source multi-agent frameworks, including MetaGPT, ChatDev, and AppWorld. The result, &lt;a href="https://sky.cs.berkeley.edu/project/mast/" rel="noopener noreferrer"&gt;MAST&lt;/a&gt;, the Multi-Agent System Failure Taxonomy, identified 14 distinct failure modes, organized into three categories, appearing consistently across every framework studied.&lt;/p&gt;

&lt;p&gt;One number from that study stands out given how mature the framework already was. ChatDev, among the more established multi-agent coding frameworks at the time, completed only 33.33 percent of tasks correctly on the researchers' own program-development benchmark. The failures were not concentrated in any single agent's reasoning. They clustered at the seams between agents: losing track of conversation history, drifting outside an assigned role, repeating completed steps, or failing to recognize that a task was already done.&lt;/p&gt;

&lt;p&gt;The Berkeley team's conclusion is that stronger base models alone will not close this gap, and later work backs that up. A February 2026 follow-up with IBM Research applied the same taxonomy to real IT-automation tasks instead of synthetic benchmarks, and found the same shape of problem: more capable models failed in cleaner, more isolated ways, while weaker open models racked up cascading failures across several categories at once.&lt;/p&gt;

&lt;p&gt;A capable model coordinating with copies of itself still needs an explicit mechanism for tracking what each agent has already done, what it remains responsible for, and when the job is actually finished. Without that, adding more agents to a workflow multiplies the number of places it can quietly go wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  The shape of the boundary
&lt;/h2&gt;

&lt;p&gt;Across all four of these evaluations, the failure mode is not "the model produces an implausible answer." Producing a fluent, plausible-looking answer is close to solved. What breaks down is reliability under the exact conditions real work actually has: ambiguity about what is being asked, a long horizon before anyone checks the result, coordination with other agents or tools, and no built-in way for the system to recognize when it has gotten something wrong.&lt;/p&gt;

&lt;p&gt;There is a practical implication in this for how to use these systems now, not a reason to avoid them. Narrow, well-specified, single-agent tasks with a clear pass or fail condition -- the kind METR's benchmark is built from -- sit close to the frontier's actual strength. Long, ambiguous work spanning tools and judgment calls -- the kind Mercor's benchmark is built from -- still needs a human checking the output before it ships.&lt;/p&gt;

&lt;p&gt;Every code suggestion naming a package is a two-second registry lookup away from being verified rather than trusted outright.&lt;/p&gt;

&lt;p&gt;The boundary itself keeps moving. METR's own data shows it moving faster after 2023 than before. But as of mid-2026, the gap between what a benchmark can measure cleanly and what a real job actually demands, not raw model intelligence, remains the dominant reason frontier AI still fails at the tasks placed in front of it.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://zyvop.com/what-ai-still-cannot-do-four-boundaries-the-2026-benchmarks-keep-finding-iczrx" rel="noopener noreferrer"&gt;ZyVOP&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;💡 For more articles like this, &lt;a href="https://zyvop.com/newsletter" rel="noopener noreferrer"&gt;subscribe to the ZyVOP newsletter&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>ailimitations2026</category>
      <category>metrtimehorizon</category>
      <category>aiagentfailurerate</category>
      <category>llmreasoninglimits</category>
    </item>
    <item>
      <title>The Ultimate Guide to Automating Postgres Backups from Docker to Google Drive</title>
      <dc:creator>Bhavya Arora</dc:creator>
      <pubDate>Mon, 31 Aug 2026 06:52:02 +0000</pubDate>
      <link>https://dev.to/bhavya_arora/the-ultimate-guide-to-automating-postgres-backups-from-docker-to-google-drive-2m6j</link>
      <guid>https://dev.to/bhavya_arora/the-ultimate-guide-to-automating-postgres-backups-from-docker-to-google-drive-2m6j</guid>
      <description>&lt;p&gt;If you run a production web application, your database is your most valuable asset. While Docker makes deploying a Postgres database incredibly easy, it also creates a false sense of security. If your entire server crashes, your VPS provider goes down, or you accidentally delete a volume, your data is gone forever.&lt;/p&gt;

&lt;p&gt;The gold standard for backups is &lt;strong&gt;Off-Site Storage&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this guide, we will walk through exactly how to completely automate your Postgres database backups. We'll extract the data securely from a Docker container, compress it, upload it directly to your Google Drive, and automatically delete old backups so your hard drive never fills up.&lt;/p&gt;




&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before we begin, ensure your server has the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A running &lt;strong&gt;Docker&lt;/strong&gt; container running PostgreSQL&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;rclone&lt;/strong&gt; installed on your server (&lt;code&gt;sudo apt install rclone&lt;/code&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A &lt;strong&gt;Google account&lt;/strong&gt; with sufficient Drive storage space&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 1: Configure Rclone for Google Drive
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://rclone.org/" rel="noopener noreferrer"&gt;Rclone&lt;/a&gt; is often referred to as "The Swiss army knife of cloud storage". It's a command-line program that can sync files to almost any cloud provider, including Google Drive.&lt;/p&gt;

&lt;p&gt;If your database is hosted on a remote Ubuntu server, you'll need to use rclone's "headless" authorization, since you don't have a web browser on the server.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;On your server&lt;/strong&gt;, initialize the config:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rclone config
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Type &lt;code&gt;n&lt;/code&gt; for &lt;strong&gt;New remote&lt;/strong&gt; and name it &lt;code&gt;gdrive&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select &lt;code&gt;drive&lt;/code&gt; (Google Drive) from the list of cloud providers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Leave &lt;code&gt;client_id&lt;/code&gt; and &lt;code&gt;client_secret&lt;/code&gt; blank (press Enter).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For &lt;code&gt;scope&lt;/code&gt;, choose the option for &lt;strong&gt;Full access all files&lt;/strong&gt; (usually option &lt;code&gt;1&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Leave the rest of the options default until it asks: &lt;code&gt;Use auto config?&lt;/code&gt;. Type &lt;code&gt;n&lt;/code&gt; (No).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Rclone will now give you a long command that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rclone authorize &lt;span class="s2"&gt;"drive"&lt;/span&gt; &lt;span class="s2"&gt;"eyJzY29wZ...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;On your local computer&lt;/strong&gt; (where you &lt;em&gt;do&lt;/em&gt; have a web browser), install rclone, open your terminal, and paste that exact command.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Your browser will open. Log into your Google Account and click &lt;strong&gt;Allow&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Your local terminal will output a secure token. Copy the entire &lt;code&gt;{ "access_token": ... }&lt;/code&gt; JSON string.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Back on your server&lt;/strong&gt;, paste the token into the prompt.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Your Google Drive is now securely linked to your server!&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: The Ultimate Backup Script
&lt;/h2&gt;

&lt;p&gt;Now we need a bash script that handles the heavy lifting. This script will read your database credentials from your &lt;code&gt;.env&lt;/code&gt; file, export a compressed snapshot of your Postgres database from inside the Docker container, and upload it to Google Drive.&lt;/p&gt;

&lt;p&gt;Create a file named &lt;code&gt;backup-db.sh&lt;/code&gt; in your project's root folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nano backup-db.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Paste the following script inside. Be sure to update the &lt;code&gt;PROJECT_DIR&lt;/code&gt; and &lt;code&gt;CONTAINER_NAME&lt;/code&gt; variables to match your environment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="c"&gt;# Configuration&lt;/span&gt;
&lt;span class="nv"&gt;PROJECT_DIR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/home/bhavya/apps/my-project"&lt;/span&gt;
&lt;span class="nv"&gt;BACKUP_DIR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROJECT_DIR&lt;/span&gt;&lt;span class="s2"&gt;/backups"&lt;/span&gt;
&lt;span class="nv"&gt;ENV_FILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PROJECT_DIR&lt;/span&gt;&lt;span class="s2"&gt;/.env"&lt;/span&gt;

&lt;span class="c"&gt;# Rclone configuration&lt;/span&gt;
&lt;span class="c"&gt;# Format: "RemoteName:FolderNameInGoogleDrive"&lt;/span&gt;
&lt;span class="nv"&gt;RCLONE_REMOTE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"gdrive:database-backups"&lt;/span&gt;

&lt;span class="c"&gt;# Docker container running Postgres&lt;/span&gt;
&lt;span class="nv"&gt;CONTAINER_NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"my-project-db-1"&lt;/span&gt;

&lt;span class="c"&gt;# Automatically load the database variables from your .env file&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$ENV_FILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
    &lt;/span&gt;&lt;span class="nb"&gt;export&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="s1"&gt;'^#'&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$ENV_FILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | xargs&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else
    &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Error: .env file not found at &lt;/span&gt;&lt;span class="nv"&gt;$ENV_FILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;span class="k"&gt;fi

&lt;/span&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$BACKUP_DIR&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="c"&gt;# Generate a timestamped filename&lt;/span&gt;
&lt;span class="nv"&gt;TIMESTAMP&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +&lt;span class="s2"&gt;"%Y-%m-%d_%H-%M-%S"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;BACKUP_FILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$BACKUP_DIR&lt;/span&gt;&lt;span class="s2"&gt;/db_backup_&lt;/span&gt;&lt;span class="nv"&gt;$TIMESTAMP&lt;/span&gt;&lt;span class="s2"&gt;.dump"&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Starting backup at &lt;/span&gt;&lt;span class="nv"&gt;$TIMESTAMP&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="c"&gt;# 1. Export the database from the Docker container&lt;/span&gt;
&lt;span class="c"&gt;# We use the custom Postgres format (-F c) for maximum compression and reliability&lt;/span&gt;
docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$CONTAINER_NAME&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; pg_dump &lt;span class="nt"&gt;-U&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$POSTGRES_USER&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt; c &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$POSTGRES_DB&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$BACKUP_FILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;$?&lt;/span&gt; &lt;span class="nt"&gt;-eq&lt;/span&gt; 0 &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
    &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Backup created successfully: &lt;/span&gt;&lt;span class="nv"&gt;$BACKUP_FILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

    &lt;span class="c"&gt;# 2. Upload the encrypted file to Google Drive using rclone&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Uploading to Google Drive..."&lt;/span&gt;
    rclone copy &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$BACKUP_FILE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$RCLONE_REMOTE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;$?&lt;/span&gt; &lt;span class="nt"&gt;-eq&lt;/span&gt; 0 &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
        &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Upload successful!"&lt;/span&gt;
    &lt;span class="k"&gt;else
        &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Error during rclone upload!"&lt;/span&gt;
        &lt;span class="nb"&gt;exit &lt;/span&gt;1
    &lt;span class="k"&gt;fi
else
    &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Error creating backup!"&lt;/span&gt;
    &lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;span class="k"&gt;fi&lt;/span&gt;

&lt;span class="c"&gt;# 3. Clean up: Delete local backups older than 7 days so your server disk never fills up&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Cleaning up old local backups..."&lt;/span&gt;
find &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$BACKUP_DIR&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-type&lt;/span&gt; f &lt;span class="nt"&gt;-name&lt;/span&gt; &lt;span class="s2"&gt;"db_backup_*.dump"&lt;/span&gt; &lt;span class="nt"&gt;-mtime&lt;/span&gt; +7 &lt;span class="nt"&gt;-exec&lt;/span&gt; &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="se"&gt;\;&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Backup process completed."&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"-----------------------------------"&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save the file and make it executable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chmod&lt;/span&gt; +x backup-db.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can test it immediately by running &lt;code&gt;./backup-db.sh&lt;/code&gt;. If it works, check your Google Drive—you should see a new folder containing your &lt;code&gt;.dump&lt;/code&gt; file!&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3: Put it on Autopilot with Cron
&lt;/h2&gt;

&lt;p&gt;We don't want to run this script manually every day. Linux has a built-in time-based job scheduler called &lt;code&gt;cron&lt;/code&gt; that is perfect for this.&lt;/p&gt;

&lt;p&gt;Open your crontab editor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;crontab &lt;span class="nt"&gt;-e&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Scroll to the very bottom and add the following line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Run the database backup daily at 2:00 AM&lt;/span&gt;
0 2 &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt; /home/bhavya/apps/my-project/backup-db.sh &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; /home/bhavya/apps/my-project/backups/backup.log 2&amp;gt;&amp;amp;1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Breaking down the cron schedule:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;0 2 * * *&lt;/code&gt; means the script will run exactly at &lt;strong&gt;2:00 AM every single day&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;gt;&amp;gt; backup.log 2&amp;gt;&amp;amp;1&lt;/code&gt; ensures that all terminal outputs (both successes and errors) are saved to a log file. If you ever need to debug why a backup failed, simply check this file!&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;And that's it! You have now implemented an enterprise-grade disaster recovery pipeline for your startup or side project.&lt;/p&gt;

&lt;p&gt;Every night at 2:00 AM:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Your Docker database is frozen and exported.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The file is seamlessly transferred to Google Drive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Any backup older than 7 days is automatically deleted from your server to save disk space.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Sleep soundly knowing your users' data is safe!&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://zyvop.com/the-ultimate-guide-to-automating-postgres-backups-from-docker-to-google-drive-lcq8e" rel="noopener noreferrer"&gt;ZyVOP&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;💡 For more articles like this, &lt;a href="https://zyvop.com/newsletter" rel="noopener noreferrer"&gt;subscribe to the ZyVOP newsletter&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>docker</category>
      <category>backups</category>
      <category>rclone</category>
    </item>
  </channel>
</rss>
