ক্রলার বিকাশের জন্য সেরা কাঠামোগুলির মধ্যে একটি হল স্ক্র্যাপি। স্ক্র্যাপি হল একটি জনপ্রিয় ওয়েব স্ক্র্যাপিং এবং ক্রলিং ফ্রেমওয়ার্ক যা উচ্চ-স্তরের কার্যকারিতা ব্যবহার করে ওয়েবসাইটগুলিকে স্ক্র্যাপ করা সহজ করে তোলে৷
ইনস্টলেশন
উইন্ডোজে স্ক্র্যাপি ইনস্টল করা সহজ:আমরা পিপ বা কনডা (যদি আপনার অ্যানাকোন্ডা থাকে) ব্যবহার করতে পারি। স্ক্র্যাপি পাইথন 2 এবং 3 উভয় সংস্করণেই চলে৷
৷pip install Scrapy
অথবা
conda install –c conda-forge scrapy
স্ক্র্যাপি সঠিকভাবে ইনস্টল করা থাকলে, একটি স্ক্র্যাপি কমান্ড এখন টার্মিনালে উপলব্ধ হবে −
C:\Users\rajesh>scrapy Scrapy 1.6.0 - no active project Usage: scrapy <command> [options] [args] Available commands: bench Run quick benchmark test fetch Fetch a URL using the Scrapy downloader genspider Generate new spider using pre-defined templates runspider Run a self-contained spider (without creating a project) settings Get settings values shell Interactive scraping console startproject Create new project version Print Scrapy version view Open URL in browser, as seen by Scrapy [ more ] More commands available when run from project directory Use "scrapy <command> -h" to see more info about a command.
একটি প্রকল্প শুরু করা হচ্ছে
এখন যে Scrapy ইনস্টল করা হয়েছে, আমরা startproject চালাতে পারি আমাদের প্রথম স্ক্র্যাপি প্রকল্পের জন্য ডিফল্ট কাঠামো তৈরি করতে কমান্ড।
এটি করার জন্য, টার্মিনাল খুলুন এবং যে ডিরেক্টরিতে আপনি আপনার স্ক্র্যাপি প্রকল্প সংরক্ষণ করতে চান সেখানে নেভিগেট করুন এবং তারপরে স্ক্র্যাপি স্টার্টপ্রজেক্ট <প্রকল্পের নাম> চালান। . নীচে আমি প্রকল্পের নামের জন্য scrapy_example ব্যবহার করছি −
C:\Users\rajesh>scrapy startproject scrapy_example New Scrapy project 'scrapy_example', using template directory 'c:\python\python361\lib\site-packages\scrapy\templates\project', created in: C:\Users\rajesh\scrapy_example You can start your first spider with: cd scrapy_example scrapy genspider example example.com C:\Users\rajesh>cd scrapy_example C:\Users\rajesh\scrapy_example>tree /F Folder PATH listing Volume serial number is 8CD6-8D39 C:. │ scrapy.cfg │ └───scrapy_example │ items.py │ middlewares.py │ pipelines.py │ settings.py │ __init__.py │ ├───spiders │ │ __init__.py │ │ │ └───__pycache__ └───__pycache__
আরেকটি উপায় হল আমরা স্ক্র্যাপি শেল চালাই এবং ওয়েব স্ক্র্যাপিং করি, যেমন নীচে −
In [18]: fetch ("https://www.wsj.com/india") 019-02-04 22:38:53 [scrapy.core.engine] DEBUG: Crawled (200)" rel="nofollow noopener noreferrer" target="_blank">https://www.wsj.com/india> (referer: None)
স্ক্র্যাপি ক্রলার একটি "প্রতিক্রিয়া" অবজেক্ট ফিরিয়ে দেবে যাতে ডাউনলোড করা তথ্য রয়েছে। আমাদের উপরের ক্রলারে কি আছে তা পরীক্ষা করা যাক −
In [19]: view(response) Out[19]: True
এবং আপনার ডিফল্ট ব্রাউজারে, ওয়েব লিঙ্কটি খুলবে এবং আপনি −
এর মত কিছু দেখতে পাবেন
দারুণ, এটি দেখতে কিছুটা আমাদের ওয়েব পৃষ্ঠার মতো, তাই ক্রলার সফলভাবে সম্পূর্ণ ওয়েব পৃষ্ঠা ডাউনলোড করেছে৷
এখন আসুন আমাদের ক্রলারে কি আছে -
In [22]: print(response.text) <!DOCTYPE html> <html data-region = "asia,india" data-protocol = "https" data-reactid = ".2316x0ul96e" data-react-checksum = "851122071"> <head data-reactid = ".2316x0ul96e.0"> <title data-reactid = ".2316x0ul96e.0.0">The Wall Street Journal & Breaking News, Business, Financial and Economic News, World News and Video</title> <meta http-equiv = "X-UA-Compatible" content = "IE = edge" data-reactid = ".2316x0ul96e.0.1"/> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8" data-reactid = ".2316x0ul96e.0.2"/> <meta name = "viewport" content = "initial-scale = 1.0001, minimum-scale = 1.0001, maximum-scale = 1.0001, user-scalable = no" data-reactid = ".2316x0ul96e.0.3"/> <meta name = "description" content = "WSJ online coverage of breaking news and current headlines from the US and around the world. Top stories, photos, videos, detailed analysis and in-depth reporting." data-reactid = ".2316x0ul96e.0.4"/> <meta name = "keywords" content = "News, breaking news, latest news, US news, headlines, world news, business, finances, politics, WSJ, WSJ news, WSJ.com, Wall Street Journal" data-reactid = ".2316x0ul96e.0.5"/> <meta name = "page.site" content = "wsj" data-reactid = ".2316x0ul96e.0.7"/> <meta name = "page.site.product" content = "WSJ" data-reactid = ".2316x0ul96e.0.8"/> <meta name = "stack.name" content = "dj01:vir:prod-sections" data-reactid = ".2316x0ul96e.0.9"/> <meta name = "referrer" content = "always" data-reactid = ".2316x0ul96e.0.a"/> <link rel = "canonical" href = "https://www.wsj.com/india/" data-reactid = ".2316x0ul96e.0.b"/> <meta nameproperty = "og:url" content = "https://www.wsj.com/india/" data-reactid = ".2316x0ul96e.0.c:$0"/> <meta nameproperty = "og:title" content = "The Wall Street Journal & Breaking News, Business, Financial and Economic News, World News and Video" data-reactid = ".2316x0ul96e.0.c:$1"/> <meta nameproperty = "og:description" content = "WSJ online coverage of breaking news and current headlines from the US and around the world. Top stories, photos, videos, detailed analysis and in-depth reporting." data-reactid = ".2316x0ul96e.0.c:$2"/> <meta nameproperty = "og:type" content = "website" data-reactid = ".2316x0ul96e.0.c:$3"/> <meta nameproperty = "og:site_name" content = "The Wall Street Journal" data-reactid = ".2316x0ul96e.0.c:$4"/> <meta nameproperty = "og:image" content = "https://s.wsj.net/img/meta/wsj-social-share.png" data-reactid = ".2316x0ul96e.0.c:$5"/> <meta name = "twitter:site" content = "@wsj" data-reactid = ".2316x0ul96e.0.c:$6"/> <meta name = "twitter:app:name:iphone" content = "The Wall Street Journal" data-reactid = ".2316x0ul96e.0.c:$7"/> <meta name = "twitter:app:name:googleplay" content = "The Wall Street Journal" data-reactid = " "/> …& so much more:
আসুন এই ওয়েবপেজ থেকে কিছু গুরুত্বপূর্ণ তথ্য বের করার চেষ্টা করি -
ওয়েবপৃষ্ঠার শিরোনাম বের করা −
স্ক্র্যাপি ক্লাস, আইডি ইত্যাদির মতো সিএসএস নির্বাচকদের উপর ভিত্তি করে এইচটিএমএল থেকে তথ্য বের করার উপায় সরবরাহ করে। যেকোনো ওয়েবপেজ শিরোনামের শিরোনামের জন্য সিএসএস নির্বাচক খুঁজে পেতে, নীচের মত, কেবল রাইট ক্লিক করুন এবং পরিদর্শনে ক্লিক করুন:
এটি আপনার ব্রাউজার উইন্ডোতে বিকাশকারী সরঞ্জামগুলি খুলবে -
যেমনটি দেখা যায়, CSS ক্লাস "wsj-headline-link" শিরোনাম আছে এমন সমস্ত অ্যাঙ্কর() ট্যাগে প্রয়োগ করা হয়। এই তথ্যের সাথে, আমরা প্রতিক্রিয়া অবজেক্টের বাকি বিষয়বস্তু থেকে সমস্ত শিরোনাম খুঁজে বের করার চেষ্টা করব -
Response.css() হল একটি ফাংশন যা CSS সিলেক্টরের উপর ভিত্তি করে বিষয়বস্তু বের করবে (উপরের অ্যাঙ্কর ট্যাগের মতো)। আসুন আমাদের response.css ফাংশনের আরও কিছু উদাহরণ দেখি।
In [24]: response.css(".wsj-headline-link::text").extract_first() Out[24]: 'China Fears Loom Over Stocks After January Surge'
এবং
In [25]: response.css(".wsj-headline-link").extract_first() Out[25]: '<a class="wsj-headline-link" href = "https://www.wsj.com/articles/china-fears-loom-over-stocks-after-january-surge-11549276200" data-reactid=".2316x0ul96e.1.1.5.1.0.3.3.0.0.0:$0.1.0">China Fears Loom Over Stocks After January Surge</a>'
ওয়েবপেজ থেকে সমস্ত লিঙ্ক পেতে -
links = response.css('a::attr(href)').extract()
আউটপুট
['https://www.google.com/intl/en_us/chrome/browser/desktop/index.html', 'https://support.apple.com/downloads/', 'https://www.mozilla.org/en-US/firefox/new/', 'https://windows.microsoft.com/en-us/internet-explorer/download-ie', 'https://www.barrons.com', 'https://bigcharts.marketwatch.com', 'https://www.wsj.com/public/page/wsj-x-marketing.html', 'https://www.dowjones.com/', 'https://global.factiva.com/factivalogin/login.asp?productname=global', 'https://www.fnlondon.com/', 'https://www.mansionglobal.com/', 'https://www.marketwatch.com', 'https://newsplus.wsj.com', 'https://privatemarkets.dowjones.com', 'https://djlogin.dowjones.com/login.asp?productname=rnc', 'https://www.wsj.com/conferences', 'https://www.wsj.com/pro/centralbanking', 'https://www.wsj.com/video/', 'https://www.wsj.com', 'https://www.bigdecisions.com/', 'https://www.businessspectator.com.au/', 'https://www.checkout51.com/?utm_source=wsj&utm_medium=digitalhousead&utm_campaign=wsjspotlight', 'https://www.harpercollins.com/', 'https://housing.com/', 'https://www.makaan.com/', 'https://nypost.com/', 'https://www.newsamerica.com/', 'https://www.proptiger.com', 'https://www.rea-group.com/', …… ……
wsj(ওয়াল স্ট্রিট জার্নাল) ওয়েবপেজ −
থেকে মন্তব্যের সংখ্যা পেতেIn [38]: response.css(".wsj-comment-count::text").extract() Out[38]: ['71', '59']
উপরে শুধু স্ক্র্যাপির মাধ্যমে ওয়েব-স্ক্র্যাপিং এর ভূমিকা, আমরা স্ক্র্যাপি দিয়ে আরও অনেক কিছু করতে পারি।