ویب سکریپنگ میں ڈیٹا تجزیہ ، تحقیق ، اور آٹومیشن جیسے کاموں کے لئے ویب سائٹوں سے ڈیٹا نکالنا شامل ہے۔ جبکہ ازگر HTTPS کی درخواستیں بھیجنے اور سکریپنگ انجام دینے کے لئے لائبریریوں کی پیش کش کرتا ہے ، لیکن PYCURL کے ذریعے CURL کا استعمال زیادہ موثر ہوسکتا ہے۔ اس ٹیوٹوریل میں ، ہم یہ ظاہر کریں گے کہ ویب صفحات کو کھرچنے کے لئے ازگر کرل کو کس طرح استعمال کیا جائے۔ ہم مثالیں فراہم کریں گے اور اس کی کارکردگی کا موازنہ دیگر مشہور لائبریریوں جیسے درخواستوں ، HTTPX ، اور AIOHTTP سے کریں گے۔
ازگر انضمام میں غوطہ لگانے سے پہلے ، کرل کی بنیادی باتوں کو سمجھنا ضروری ہے۔ گیٹ اور پوسٹ کی درخواستیں بنانے جیسے کام انجام دینے کے لئے آپ ٹرمینل میں براہ راست CURL کمانڈز استعمال کرسکتے ہیں۔
مثال کے طور پر کرل کے احکامات:
# GET request
curl -X GET "https://httpbin.org/get"
# POST request
curl -X POST "https://httpbin.org/post"
ازگر میں کرل کو استعمال کرنے کے ل we ، ہمیں پائکورل لائبریری کی ضرورت ہے ، جو کرل لائبریری کو ایک ازگر انٹرفیس فراہم کرتا ہے۔
پائکورل انسٹال کرنا:
pip install pycurl
پائکورل ازگر میں HTTP درخواستوں پر تفصیلی کنٹرول پیش کرتا ہے۔ ذیل میں ایک مثال پیش کی گئی ہے جس میں یہ ظاہر کیا گیا ہے کہ کس طرح پیکورل کے ساتھ درخواست کی درخواست کی جائے:
import pycurl
import certifi
from io import BytesIO
# Create a BytesIO object to hold the response data
buffer = BytesIO()
# Initialize a cURL object
c = pycurl.Curl()
# Set the URL for the HTTP GET request
c.setopt(c.URL, 'https://httpbin.org/get')
# Set the buffer to capture the output data
c.setopt(c.WRITEDATA, buffer)
# Set the path to the CA bundle file for SSL/TLS verification
c.setopt(c.CAINFO, certifi.where())
# Perform the HTTP request
c.perform()
# Close the cURL object to free up resources
c.close()
# Retrieve the content of the response from the buffer
body = buffer.getvalue()
# Decode and print the response body
print(body.decode('iso-8859-1'))
پوسٹ کی درخواستوں کے ساتھ ڈیٹا بھیجنا عام ہے۔ پائکورل کے ساتھ ، پوسٹ فیلڈز کا آپشن استعمال کریں۔ یہاں پائکورل کے ساتھ پوسٹ کی درخواست کرنے کی ایک مثال ہے:
import pycurl
import certifi
from io import BytesIO
# Create a BytesIO object to hold the response data
buffer = BytesIO()
# Initialize a cURL object
c = pycurl.Curl()
# Set the URL for the HTTP POST request
c.setopt(c.URL, 'https://httpbin.org/post')
# Set the data to be posted
post_data = 'param1="pycurl"¶m2=article'
c.setopt(c.POSTFIELDS, post_data)
# Set the buffer to capture the output data
c.setopt(c.WRITEDATA, buffer)
# Set the path to the CA bundle file for SSL/TLS verification
c.setopt(c.CAINFO, certifi.where())
# Perform the HTTP request
c.perform()
# Close the cURL object to free up resources
c.close()
# Retrieve the content of the response from the buffer
body = buffer.getvalue()
# Decode and print the response body
print(body.decode('iso-8859-1'))
HTTP درخواستوں کے ساتھ اکثر کسٹم ہیڈر یا توثیق کی ضرورت ہوتی ہے۔ ذیل میں Pycurl کے ساتھ کسٹم ہیڈر ترتیب دینے کی ایک مثال ہے:
import pycurl
import certifi
from io import BytesIO
# Create a BytesIO object to hold the response data
buffer = BytesIO()
# Initialize a cURL object
c = pycurl.Curl()
# Set the URL for the HTTP GET request
c.setopt(c.URL, 'https://httpbin.org/get')
# Set custom HTTP headers
c.setopt(c.HTTPHEADER, ['User-Agent: MyApp', 'Accept: application/json'])
# Set the buffer to capture the output data
c.setopt(c.WRITEDATA, buffer)
# Set the path to the CA bundle file for SSL/TLS verification
c.setopt(c.CAINFO, certifi.where())
# Perform the HTTP request
c.perform()
# Close the cURL object to free up resources
c.close()
# Retrieve the content of the response from the buffer
body = buffer.getvalue()
# Decode and print the response body
print(body.decode('iso-8859-1'))
APIs کے ساتھ کام کرتے وقت XML ردعمل کو پارس کرنا اور ہینڈلنگ کرنا بہت ضروری ہے۔ ذیل میں pycurl کے ساتھ XML ردعمل کو سنبھالنے کی ایک مثال ہے:
# Import necessary libraries
import pycurl # Library for making HTTP requests
import certifi # Library for SSL certificate verification
from io import BytesIO # Library for handling byte streams
import xml.etree.ElementTree as ET # Library for parsing XML
# Create a buffer to hold the response data
buffer = BytesIO()
# Initialize a cURL object
c = pycurl.Curl()
# Set the URL for the HTTP GET request
c.setopt(c.URL, 'https://www.google.com/sitemap.xml')
# Set the buffer to capture the output data
c.setopt(c.WRITEDATA, buffer)
# Set the path to the CA bundle file for SSL/TLS verification
c.setopt(c.CAINFO, certifi.where())
# Perform the HTTP request
c.perform()
# Close the cURL object to free up resources
c.close()
# Retrieve the content of the response from the buffer
body = buffer.getvalue()
# Parse the XML content into an ElementTree object
root = ET.fromstring(body.decode('utf-8'))
# Print the tag and attributes of the root element of the XML tree
print(root.tag, root.attrib)
قابل اعتماد HTTP درخواستیں کرنے کے لئے مضبوط غلطی سے نمٹنا ضروری ہے۔ ذیل میں پِکورل کے ساتھ غلطی سے نمٹنے کی ایک مثال ہے:
import pycurl # Import the pycurl library
import certifi # Import the certifi library
from io import BytesIO # Import BytesIO for handling byte streams
# Initialize a Curl object
c = pycurl.Curl()
buffer = BytesIO()
# Set the URL for the HTTP request
c.setopt(c.URL, 'http://example.com')
c.setopt(c.WRITEDATA, buffer)
c.setopt(c.CAINFO, certifi.where())
try:
# Perform the HTTP request
c.perform()
except pycurl.error as e:
# If an error occurs during the request, catch the pycurl.error exception
errno, errstr = e.args # Retrieve the error number and error message
print(f'Error: {errstr} (errno {errno})') # Print the error message and error number
finally:
# Close the Curl object to free up resources
c.close()
body = buffer.getvalue()
print(body.decode('iso-8859-1')) # Decode and print the response body
درست کوڈ URL کو https://example.com میں ایڈجسٹ کرتا ہے ، پروٹوکول کے مسئلے کو حل کرتا ہے۔ یہ درخواست کی تشکیل ، اسے انجام دینے ، اور ابتدائی ٹکڑوں کی طرح غلطیوں کو سنبھالنے کے عمل کو دہراتا ہے۔ کامیاب پھانسی کے بعد ، ردعمل کا جسم ایک بار پھر ضابطہ کشائی اور پرنٹ کیا جاتا ہے۔ یہ ٹکڑوں نے PYCURL کے ساتھ HTTP درخواستوں میں مناسب URL ترتیب اور مضبوط غلطی سے نمٹنے کی اہمیت کو اجاگر کیا ہے۔
import pycurl # Import the pycurl library
import certifi # Import the certifi library
from io import BytesIO # Import BytesIO for handling byte streams
# Reinitialize the Curl object
c = pycurl.Curl()
buffer = BytesIO()
# Correct the URL to use HTTPS
c.setopt(c.URL, 'https://example.com')
c.setopt(c.WRITEDATA, buffer)
c.setopt(c.CAINFO, certifi.where())
try:
# Perform the corrected HTTP request
c.perform()
except pycurl.error as e:
# If an error occurs during the request, catch the pycurl.error exception
errno, errstr = e.args # Retrieve the error number and error message
print(f'Error: {errstr} (errno {errno})') # Print the error message and error number
finally:
# Close the Curl object to free up resources
c.close()
body = buffer.getvalue()
print(body.decode('iso-8859-1')) # Decode and print the response body
CURL HTTP درخواست کے طرز عمل کو کنٹرول کرنے کے لئے بہت سے جدید اختیارات فراہم کرتا ہے ، جیسے کوکیز اور ٹائم آؤٹ کو سنبھالنا۔ ذیل میں ایک مثال ہے جو پائکورل کے ساتھ جدید اختیارات کا مظاہرہ کرتی ہے۔
import pycurl # Import the pycurl library
import certifi # Import the certifi library for SSL certificate verification
from io import BytesIO # Import BytesIO for handling byte streams
# Create a buffer to hold the response data
buffer = BytesIO()
# Initialize a Curl object
c = pycurl.Curl()
# Set the URL for the HTTP request
c.setopt(c.URL, 'http://httpbin.org/cookies')
# Enable cookies by setting a specific key-value pair
c.setopt(c.COOKIE, 'cookies_key=cookie_value')
# Set a timeout of 30 seconds for the request
c.setopt(c.TIMEOUT, 30)
# Set the buffer to capture the output data
c.setopt(c.WRITEDATA, buffer)
# Set the path to the CA bundle file for SSL/TLS verification
c.setopt(c.CAINFO, certifi.where())
# Perform the HTTP request
c.perform()
# Close the Curl object to free up resources
c.close()
# Retrieve the content of the response from the buffer
body = buffer.getvalue()
# Decode the response body using UTF-8 encoding and print it
print(body.decode('utf-8'))
جب ازگر میں ایچ ٹی ٹی پی کی درخواستوں کے ساتھ کام کرتے ہو تو ، چار مشہور لائبریریاں پائکورل ، درخواستیں ، HTTPX ، اور AIOHTTP ہیں۔ ہر ایک کی اپنی طاقت اور کمزوری ہوتی ہے۔ اپنی ضروریات کے لئے صحیح ٹول کا انتخاب کرنے میں مدد کرنے کے لئے یہاں ایک موازنہ ہے:
خصوصیت | PycURL | Requests | HTTPX | AIOHTTP |
---|---|---|---|---|
استعمال میں آسانی | اعتدال پسند | بہت آسان | آسان | اعتدال پسند |
کارکردگی | اعلی | اعتدال پسند | اعلی | اعلی |
غیر متزلزل حمایت | نہیں | نہیں | ہاں | ہاں |
اسٹریمنگ | ہاں | محدود | ہاں | ہاں |
پروٹوکول سپورٹ | وسیع (بہت سے پروٹوکول کی حمایت کرتا ہے) | HTTP/HTTPS | HTTP/HTTPS, HTTP/2, WebSockets | HTTP/HTTPS, WebSockets |
تقابلی تجزیہ اس بات کی نشاندہی کرتا ہے کہ PYCURL اعلی کارکردگی اور لچک پیش کرتا ہے ، جس سے یہ اعلی درجے کے صارفین کے لئے موزوں ہوتا ہے جنھیں HTTP درخواستوں کا تفصیلی انتظام کی ضرورت ہوتی ہے۔ دوسری طرف ، درخواستیں اور HTTPX آسان ، زیادہ بدیہی منظرناموں کے لئے بہتر موزوں ہیں۔ AIOHTTP غیر متزلزل کاموں کو سنبھالنے میں کھڑا ہے ، جس میں غیر متزلزل درخواستوں کو سنبھالنے کے لئے موثر ٹولز مہیا کرتے ہیں۔
صحیح لائبریری کا انتخاب آپ کے پروجیکٹ کی مخصوص ضروریات اور ضروریات پر منحصر ہے ، جس میں پائیکورل ان لوگوں کے لئے ایک بہترین آپشن ہے جو رفتار اور اعلی درجے کی صلاحیتوں کی ضرورت ہے۔
تبصرے: 0