ur
English
Español
中國人
Tiếng Việt
Deutsch
Українська
Português
Français
भारतीय
Türkçe
한국인
Italiano
Gaeilge
Indonesia
Polski Craigslist جیسا اشتہاری پلیٹ فارم ڈیجیٹل دور میں بھی متعلقہ رہتا ہے۔ کلیکٹر اشتہارات سے معلومات نکالنے کے عمل کو خودکار بنانے میں مدد کرتا ہے، خاص طور پر ویب سائٹس سے براہِ راست ڈیٹا حاصل کر کے۔ BeautifulSoup اور Requests جیسی لچکدار اور طاقتور لائبریریز کی بدولت، ڈیٹا جمع کرنا مؤثر طریقے سے کیا جا سکتا ہے۔ یہ ٹیوٹوریل Python کے ساتھ Craigslist اسکریپنگ پر مرکوز ہے، جس میں BeautifulSoup، Requests، اور بوٹ ڈیٹیکشن سے بچنے کے لیے پراکسی روٹیشن کو اجاگر کیا گیا ہے۔
اب ہم قدم بہ قدم یہ سمجھیں گے کہ Craigslist کو کیسے اسکریپ کیا جائے۔
اب ہم مزید تفصیل سے ان مراحل سے گزریں گے جن کے ذریعے Craigslist کو ویب اسکریپ کیا جاتا ہے، جس کا آغاز کسی مخصوص ویب صفحے پر HTTP درخواستیں بھیجنے سے ہوتا ہے، مطلوبہ صفحہ کو سیکشن میں تقسیم کرنا، مطلوبہ ڈیٹا جمع کرنا اور اسے ایک طے شدہ فارمیٹ میں محفوظ کرنا۔
مندرجہ ذیل پیکجز ڈاؤن لوڈ اور انسٹال کرنے ہوں گے:
pip install beautifulsoup4
pip install requests
ویب صفحات سے ڈیٹا حاصل کرنے کے لیے، سب سے پہلے آپ کو ان URLز پر HTTP درخواستیں بھیجنی ہوں گی جنہیں آپ اسکریپ کرنا چاہتے ہیں۔ requests لائبریری کا استعمال کرتے ہوئے، آپ GET درخواستیں بھیج سکتے ہیں تاکہ HTML مواد حاصل کیا جا سکے، جسے آپ بعد میں پروسیس کر کے مطلوبہ معلومات نکال سکتے ہیں۔
import requests
# List of Craigslist URLs to scrape
urls = [
"link",
"link"
]
for url in urls:
# Send a GET request to the URL
response = requests.get(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Extract HTML content from the response
html_content = response.text
else:
# If the request failed, print an error message with the status code
print(f"Failed to retrieve {url}. Status code: {response.status_code}")
BeautifulSoup کے ساتھ، آپ HTML کے ذریعے جا سکتے ہیں اور Craigslist سے وہ حصے نکال سکتے ہیں جن کی آپ کو ضرورت ہے۔ یہ آپ کو ٹیگز تلاش کرنے، متن حاصل کرنے، اور لنکس یا قیمتوں جیسی چیزیں نکالنے کی اجازت دیتا ہے۔ یہ بے ترتیب ویب صفحات سے مفید معلومات حاصل کرنے کا ایک آسان طریقہ ہے۔
from bs4 import BeautifulSoup
# Iterate through each URL in the list
for url in urls:
# Send a GET request to the URL
response = requests.get(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Extract HTML content from the response
html_content = response.text
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(html_content, 'html.parser')
else:
# If the request failed, print an error message with the status code
print(f"Failed to retrieve {url}. Status code: {response.status_code}")
HTML مواد حاصل کرنے کے بعد، اگلا مرحلہ اسے BeautifulSoup لائبریری کا استعمال کرتے ہوئے پارس کرنا ہے۔ فنکشنز کا استعمال کرتے ہوئے ہم Craigslist کا ڈیٹا اسکریپ کرتے ہیں جیسے کہ آئٹمز کی لسٹنگ، عنوانات اور قیمتیں۔ یہ ایسے ہی ہے جیسے آپ کے پاس ایک ایسا ٹول ہو جو آپ کو بے ترتیب کوڈ میں سے مفید حصے تیزی اور مؤثر طریقے سے تلاش کرنے میں مدد دے۔
from bs4 import BeautifulSoup
# Iterate through each URL in the list
for url in urls:
# Send a GET request to the URL
response = requests.get(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Extract HTML content from the response
html_content = response.text
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(html_content, 'html.parser')
# Extracting specific data points
# Find the title of the listing
title = soup.find('span', id='titletextonly').text.strip()
# Find the price of the listing
price = soup.find('span', class_='price').text.strip()
# Find the description of the listing (may contain multiple paragraphs)
description = soup.find('section', id='postingbody').find_all(text=True, recursive=False)
# Print extracted data (for demonstration purposes)
print(f"Title: {title}")
print(f"Price: {price}")
print(f"Description: {description}")
else:
# If the request fails, print an error message with the status code
print(f"Failed to retrieve {url}. Status code: {response.status_code}")
عنوان:
قیمت:
تفصیل:
Craigslist ڈیٹا نکالنے کے بعد، اس بات کو یقینی بنائیں کہ اسے مستقبل میں استعمال یا تجزیے کے لیے اور دیگر ایپلیکیشنز کے ساتھ ہم آہنگی کے لیے CSV فارمیٹ میں محفوظ کیا جائے۔
import csv
# Define the CSV file path and field names
csv_file = 'craigslist_data.csv'
fieldnames = ['Title', 'Price', 'Description']
# Writing data to CSV file
try:
# Open the CSV file in write mode with UTF-8 encoding
with open(csv_file, mode='w', newline='', encoding='utf-8') as file:
# Create a CSV DictWriter object with the specified fieldnames
writer = csv.DictWriter(file, fieldnames=fieldnames)
# Write the header row in the CSV file
writer.writeheader()
# Iterate through each item in the scraped_data list
for item in scraped_data:
# Write each item as a row in the CSV file
writer.writerow(item)
# Print a success message after writing data to the CSV file
print(f"Data saved to {csv_file}")
except IOError:
# Print an error message if an IOError occurs while writing to the CSV file
print(f"Error occurred while writing data to {csv_file}")
اگر آپ Craigslist ویب سائٹ سے خودکار طور پر لسٹنگز اکٹھی کرنا چاہتے ہیں، تو سب سے آسان طریقوں میں سے ایک یہ ہے کہ ایک لائبریری کے ذریعے غیر سرکاری API استعمال کریں۔ یہ آپ کو سائٹ پر کوئری کرنے، نتائج کو زمرہ، شہر، قیمت، کلیدی الفاظ اور مزید کے مطابق فلٹر کرنے کی اجازت دیتا ہے۔
لائبریری کو انسٹال کر کے شروع کریں:
pip install python-craigslist
یہاں نیویارک میں اپارٹمنٹ کرایہ پر تلاش کرنے کی ایک سادہ مثال دی گئی ہے:
from craigslist import CraigslistHousing
cl_h = CraigslistHousing(site='newyork', category='apa', filters={'max_price': 2000})
for result in cl_h.get_results(limit=10):
print(result['name'], result['price'], result['url'])
یہ کوڈ نیویارک سٹی میں اپارٹمنٹس/ہاؤسنگ فار رینٹ سیکشن سے پہلی 10 لسٹنگز حاصل کرتا ہے، جہاں قیمت $2000 سے کم ہو۔
لائبریری نوکریوں، گاڑیوں، فروخت کے لیے اشیاء اور مزید جیسی کیٹیگریز کے ساتھ ساتھ وسیع پیمانے پر فلٹرز کو بھی سپورٹ کرتی ہے۔ یہ باآسانی Python پر مبنی ٹولز بنانے کے لیے ایک بہترین ٹول ہے، جیسے کہ بوٹس، لسٹنگ ٹریکرز، یا مارکیٹ اینالیٹکس۔
ویب اسکریپنگ کرتے وقت، خاص طور پر Craigslist کے ساتھ، کئی اضافی چیلنجز کا سامنا ہو سکتا ہے۔ یہ اسکریپنگ کی کوششوں کو روکنے کے لیے IP بلاکس اور CAPTCHA چیلنجز نافذ کرتا ہے۔ ان مسائل سے بچنے کے لیے آپ پروکسیز کے ساتھ ساتھ یوزر ایجنٹ روٹیشن بھی استعمال کر سکتے ہیں۔
پروکسیز کا استعمال:
پروکسیز اور روٹٹنگ یوزر ایجنٹس کو ایک ساتھ استعمال کرنا اسکریپنگ کو بغیر پکڑے جاری رکھنے کا ایک سمجھدار طریقہ ہے۔
proxies = {
'http': 'http://your_proxy_ip:your_proxy_port',
'https': 'https://your_proxy_ip:your_proxy_port'
}
response = requests.get(url, proxies=proxies)
یوزر-ایجنٹ روٹیشن کا مطلب ہے ہر درخواست کے ساتھ آپ کا اسکریپر جو براؤزر کی شناخت بھیجتا ہے اسے تبدیل کرنا۔ اگر آپ ہمیشہ ایک ہی یوزر-ایجنٹ استعمال کریں تو یہ مشکوک لگتا ہے۔ مختلف یوزر-ایجنٹس کے درمیان سوئچ کرنے سے آپ کا اسکریپر عام صارفین کی طرح نظر آتا ہے، جس سے بلاکس سے بچنے میں مدد ملتی ہے:
import random
user_agents = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
# Add more user agents as needed
]
headers = {
'User-Agent': random.choice(user_agents)
}
response = requests.get(url, headers=headers)
اس ٹیوٹوریل میں بیان کیے گئے تمام ماڈیولز کو یکجا کرنے سے ایک مکمل طور پر فعال Python Craigslist اسکریپر تیار کیا جا سکتا ہے۔ یہ پروگرام کئی URLs سے ڈیٹا نکال سکتا ہے، اسے پارس کر سکتا ہے، اور مطلوبہ ڈیٹا حاصل کرنے کے لیے ان میں نیویگیٹ کر سکتا ہے۔
import requests
import urllib3
from bs4 import BeautifulSoup
import csv
import random
import ssl
ssl._create_default_https_context = ssl._create_stdlib_context
urllib3.disable_warnings()
# List of Craigslist URLs to scrape
urls = [
"link",
"link"
]
# User agents and proxies
user_agents = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
]
proxies = [
{'http': 'http://your_proxy_ip1:your_proxy_port1', 'https': 'https://your_proxy_ip1:your_proxy_port1'},
{'http': 'http://your_proxy_ip2:your_proxy_port2', 'https': 'https://your_proxy_ip2:your_proxy_port2'},
]
# List to store scraped data
scraped_data = []
# Loop through each URL in the list
for url in urls:
# Rotate user agent for each request to avoid detection
headers = {
'User-Agent': random.choice(user_agents)
}
# Use a different proxy for each request to avoid IP blocking
proxy = random.choice(proxies)
try:
# Send GET request to the Craigslist URL with headers and proxy
response = requests.get(url, headers=headers, proxies=proxy, timeout=30, verify=False)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse HTML content of the response
html_content = response.text
soup = BeautifulSoup(html_content, 'html.parser')
# Extract data from the parsed HTML
title = soup.find('span', id='titletextonly').text.strip()
price = soup.find('span', class_='price').text.strip()
description = soup.find('section', id='postingbody').get_text(strip=True, separator='\n') # Extracting description
# Append scraped data as a dictionary to the list
scraped_data.append({'Title': title, 'Price': price, 'Description': description})
print(f"Data scraped for {url}")
else:
# Print error message if request fails
print(f"Failed to retrieve {url}. Status code: {response.status_code}")
except Exception as e:
# Print exception message if an error occurs during scraping
print(f"Exception occurred while scraping {url}: {str(e)}")
# CSV file setup for storing scraped data
csv_file = 'craigslist_data.csv'
fieldnames = ['Title', 'Price', 'Description']
# Writing scraped data to CSV file
try:
with open(csv_file, mode='w', newline='', encoding='utf-8') as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
# Write header row in the CSV file
writer.writeheader()
# Iterate through scraped_data list and write each item to the CSV file
for item in scraped_data:
writer.writerow(item)
# Print success message if data is saved successfully
print(f"Data saved to {csv_file}")
except IOError:
# Print error message if there is an IOError while writing to the CSV file
print(f"Error occurred while writing data to {csv_file}")
اب جب کہ آپ سمجھ چکے ہیں کہ ویب اسکریپنگ کیسے کام کرتی ہے، یہ دیکھنا آسان ہے کہ یہ کتنی مفید ہے — چاہے آپ مارکیٹ کا تجزیہ کر رہے ہوں یا لیڈز تلاش کر رہے ہوں۔ ویب سائٹس قیمتی معلومات سے بھری ہوتی ہیں، اور BeautifulSoup اور Requests جیسے ٹولز اس ڈیٹا کو نکالنا کافی آسان بنا دیتے ہیں۔ اس گائیڈ میں چند اہم نکات پر بھی روشنی ڈالی گئی، جیسے ڈائنامک مواد سے نمٹنا اور زیرِ نظر آئے بغیر رہنے کے لیے روٹیٹنگ پراکسیز کا استعمال۔ اگر صحیح طریقے سے کیا جائے تو Python کے ساتھ اسکریپنگ واقعی کاروباروں اور افراد کو ہر قسم کے شعبوں میں بہتر فیصلے کرنے میں مدد دے سکتی ہے۔
تبصرے: 0