Turn cURL Commands into Python Code Instantly with curl2py – A Step‑by‑Step Guide

This tutorial shows how to install the filestools library, use its curl2py command to convert cURL requests into ready‑to‑run Python code, and demonstrates the process with a real‑world example, saving time and avoiding manual copying of headers and parameters.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Turn cURL Commands into Python Code Instantly with curl2py – A Step‑by‑Step Guide

Introduction

The curl2py command is part of the filestools library and can directly convert a cURL request into equivalent Python code, eliminating the tedious manual copying of headers, cookies, and parameters.

Installation

Install the library via pip: pip install filestools or use a trusted mirror:

pip install filestools -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com

Traditional Method

Normally you would open the browser’s developer tools, copy the request headers, cookies, and parameters one by one, and paste them into your script—a process that is error‑prone and time‑consuming.

Using curl2py

1. In the browser, right‑click the request and choose Copy → Copy as cURL (bash) .

2. Paste the copied cURL command into the following Python snippet and run it:

from curl2py.curlParseTool import curlCmdGenPyScript
curl_cmd = """xxx"""
output = curlCmdGenPyScript(curl_cmd)
print(output)

Example

Running the above with a real request to https://xxkol.cn/api/klist generates the following script:

#######################################
#      The generated by curl2py.      
#      author:小小明                  
#######################################

import requests
import json

headers = {
    "authority": "xxkol.cn",
    "sec-ch-ua": "\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Microsoft Edge\";v=\"92\"",
    "accept": "application/json, text/plain, */*",
    "authorization": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
    "sec-ch-ua-mobile": "?0",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.73",
    "sec-fetch-site": "same-origin",
    "sec-fetch-mode": "cors",
    "sec-fetch-dest": "empty",
    "referer": "https://xxkol.cn/kol",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"
}
cookies = {
    "Hm_lvt_d4217dc2524e360ff487588dd84ad4ab": "1629232919",
    "xxtoken": "eyJ0eXAiOiJKVhbGciOiJIUzI1NiJ9...",
    "Hm_lpvt_d4217dc2524e360ff488dd84ad4ab": "16292212"
}
params = {
    "pagesize": "20",
    "page": "2",
    "name": "",
    "follower_start": "",
    "follower_end": "",
    "inter_start": "",
    "inter_end": "",
    "xxpoint_start": "",
    "xxpoint_end": "",
    "platform": "",
    "sex": "",
    "attribute": "",
    "category": "",
    "sort_type": ""
}

res = requests.get("https://xxkol.cn/api/klist", params=params, headers=headers, cookies=cookies)
print(res.text)

Result

Executing the script returns JSON data, which can be inspected with an online JSON formatter (e.g., https://www.sojson.com/ ).

Conclusion

The curl2py tool automates the conversion of web requests into Python code, saving effort and reducing errors, making it a handy utility for anyone working on web scraping or API integration.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Code GenerationAutomationHTTP requestscurl2pyfilestools
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.