Web Scraping with Python

Web Scraping with Python

What is Web Scraping and Why Should We Know?

Web scraping ဆိုတာကတော့ website တစ်ခုရဲ့ HTML content ကို parse လုပ်ပြီး အဲဒီ content ထဲက data တွေကို ထုတ်ယူတဲ့ process ပါ။ Web scraping ကို အများအားဖြင့် automated software သို့မဟုတ် scripts တွေကို အသုံးပြုပြီး လုပ်ဆောင်ကြပါတယ်။ ဒီ scripts တွေသည် web browser မှ လုပ်ဆောင်နိုင်သည့်အတိုင်း Website တစ်ခုကို ဖွင့်တာ အချို့သော အချက်အလက်တွေကို ရှာဖွေကာ အထိရောက်ဆုံးနည်းလမ်းနဲ့ အဲဒီ အချက်အလက်တွေကို စုဆောင်းတာ သို့မဟုတ် သိမ်းဆည်းတာပါ။

Web scraping က အရမ်းအသုံးဝင်ပြီး၊ scenarios အမျိုးမျိုးတွင် အသုံးပြုကြပါတယ်။ Security perspective မှာ web scraping ကို အသုံးပြုခြင်းသည် များသောအားဖြင့် လုံခြုံရေး လေ့လာမှုများ၊ အချက်အလက် စစ်ဆေးမှုများနှင့် အန္တရာယ်ဆိုင်ရာ ပုံစံများကို အချိန်နှင့် တပြေးညီ စောင့်ကြည့်သည့်အတွက် အထောက်အကူဖြစ်စေပါသည်။ အောက်တွင် အဓိက အသုံးပြုချက်အချို့ကို ဖော်ပြပါသည် -

  1. Phishing Website Detection -

    Web scraping ကို အသုံးပြုကာ unverified phishing websites (Phishing websites that are not fake) များကို အလိုအလျောက် ရှာဖွေနိုင်သည်။ ဥပမာအားဖြင့် သာမန် websites များနှင့် ကွက်ကွက်ကွင်းကွင်းဆင်တူ ဖန်တီးထားသော phishing sites များကို လျင်မြန်စွာ တွေ့ရှိနိုင်ရန် scraping နည်းလမ်းကို အသုံးပြုနိုင်သည်။

  2. Vulnerability Scanning - Web scraping ကို အချက်အလက် အားဖြင့် ဝဘ်ဆိုဒ်များ၏ အားနည်းချက်များကို စစ်ဆေးခြင်းတွင် အသုံးပြုနိုင်သည်။ ဥပမာအားဖြင့်, URL များတွင် ထည့်သွင်းထားသော sensitive information များ (e.g., အများသုံး API keys, tokens) အတွက် scanning လုပ်ကာ လုံခြုံရေး အားနည်းချက်များကို ရှာဖွေပြီး အန္တရာယ်အနည်းဆုံးဖြစ်စေရန် အထောက်အကူဖြစ်စေပါသည်။

  3. Vulnerability Scanning
    Web scraping ကို အသုံးပြုပြီး websites များတွင် vulnerabilities များကို စစ်ဆေးနိုင်သည်။ ဥပမာအားဖြင့်, URL များတွင် ထည့်သွင်းထားသော sensitive information တွေဖြစ်သည့် (API keys, tokens) တို့ကို scanning ပြုလုပ်ခြင်းဖြင့် security vulnerabilities များကို ရှာဖွေနိုင်သည်။ ယင်း vulnerabilities များကို စောင့်ကြည့်ကာ အန္တရာယ်အနည်းဆုံးဖြစ်အောင် ကာကွယ်ရန် အထောက်အကူပြုပါသည်။

  4. Monitoring for Data Leaks - အခြား websites များတွင် လုံခြုံရေး အချက်အလက်များ ပေါက်ကြားခြင်းအတွက် စောင့်ကြည့်နိုင်သည်။ မတရားထုတ်ဖော်ခြင်း ခံရသော sensitive data များကို စနစ်တကျ စောင့်ကြည့်ပြီး လျင်မြန်စွာ သတိပေးနိုင်သည်။

  5. Brand Protection - မဟုတ်မခံ websites သို့မဟုတ် မူပိုင်ခွင့်ရှိသော ပုံများကို တရားမဝင် အသုံးပြုမှုများကို ရှာဖွေဖော်ထုတ်နိုင်သည်။ Web scraping နည်းလမ်းဖြင့် လက်ပေါ်ထင်နေသော ဝဘ်ဆိုဒ်များကို စာရင်းပြုစုကာ တရားမဝင်လုပ်ငန်းများကို ကာကွယ်နိုင်သည်။

  6. Threat Intelligence Gathering - Hacker forums သို့မဟုတ် မတရား စကားဝှက်များ မျှဝေရာနေရာများအား scraping စနစ်ဖြင့် စောင့်ကြည့်ကာ ရှိနိုင်သည့် လက်ရှိနှင့် အနာဂတ် လုံခြုံရေး အန္တရာယ်များအတွက် အချက်အလက်စုဆောင်းနိုင်သည်။

Security Perspective တွင်၊ Web scraping သည် လုံခြုံရေး အခြေအနေကို တိုးတက်စေပြီး အန္တရာယ်များကို လျင်မြန်စွာ ပြန်လည်တုံ့ပြန်နိုင်ရန် အထောက်အကူပြုသည်။

ဒါပေမယ့် အခြားသော ကိစ္စရပ်တွေမှာ အသုံးဝင်နိုင်တဲ့ examples တွေကိုပေးရရင် ပိုမိုသိမြင်စေပါလိမ့်မယ်။

Data Science and Machine Learning

Machine learning models အများစုသည် အချက်အလက်များ (data) အလွန်လိုအပ်ကြပြီး အချက်အလက်များကို စုဆောင်းရန်အတွက် web scraping ကို အသုံးပြုနိုင်ပါသည်။ Web scraping သည် အင်တာနက်ပေါ်မှ အချက်အလက်များကို အလိုအလျောက် စုဆောင်းနိုင်စေသောနည်းလမ်းဖြစ်ပြီး အများအားဖြင့် ရရှိရန် ခက်ခဲသော data များကို ရရှိနိုင်စေပါသည်။

For example: လူတစ်ယောက်သည် social media ပေါ်ရှိ comment များ၏ sentiment ကို လေ့လာလိုကြောင်းယူထားပါစို့။ သူသည် လူများ၏ comment များကို အပြုသဘော၊ အနုတ်သဘော၊ သုတေသနဖတ်သဘော စသဖြင့် မျှဝေထားသည့် ပုံစံကို စိစစ်လိုပါက ယင်း comment များကို အလိုအလျောက် စုဆောင်းရန် web scraping ကို အသုံးပြုနိုင်ပါသည်။ ဒီလို ရရှိသော အချက်အလက်များကို ထပ်မံ လေ့လာ၍ sentiment analysis လုပ်ဆောင်နိုင်သော machine learning model တစ်ခု ဖန်တီးနိုင်ပါတယ်။ ဒီ model ကနေပြန်လာတဲ့ ရလဒ်အရ လူ့အဖွဲ့အစည်းများရဲ့ သဘောထား၊ အပြုအမူတို့ကို နားလည်မြင်သာစေမှာ ဖြစ်ပါတယ်။

ထို့ကြောင့် web scraping သည် machine learning အတွက် လိုအပ်သော အချက်အလက်များကို ရှာဖွေနိုင်စေသည့် အရေးကြီးသော စနစ်တစ်ခု ဖြစ်ပါတယ်။

Market Research

Companies တွေနဲ့ Researcher တွေသည် online product prices, product features, consumer reviews စတဲ့ အချက်အလက်တွေကို စုဆောင်းဖို့ web scraping ကို အသုံးပြုနိုင်ပါတယ်။ ဒါကို trends တွေကို ခြေရာခံတာ၊ ပြိုင်ဘက်ကုန်ပစ္စည်းဈေးနှုန်းတွေကို ကြည့်တာ၊ သို့မဟုတ် စျေးကွက်ခွဲခြမ်းစိတ်ဖြာတာတွေအတွက် အသုံးပြုနိုင်ပါတယ်။

Market research အတွက် web scraping ကို အသုံးပြုခြင်းသည် အများအားဖြင့် ပြိုင်ဘက်လေ့လာမှု၊ စျေးကွက်ခွဲခြမ်းစိတ်ဖြာခြင်းနှင့် စျေးကွက်ထဲရှိ customer behavior ကို နားလည်ရန် အထောက်အကူပြုပါသည်။

  1. Product Prices - မျိုးစုံသော websites ပေါ်ရှိ အချိန်နှင့် တပြေးညီ product prices များကို scraping ဖြင့် စုဆောင်းနိုင်ပါသည်။ ဥပမာအားဖြင့် Amazon, eBay, Walmart စတဲ့ သီးသန့် e-commerce sites များမှ ပြိုင်ဘက်ကုန်ပစ္စည်းများ၏ ရောင်းဈေးများကို စိစစ်ကာ လက်ရှိပြိုင်ဘက်ဈေးနှုန်းကို သိရှိနိုင်စေသည်။ ယင်းအချက်အလက်ကို အသုံးပြုပြီး မိမိကုမ္ပဏီ၏ product pricing strategy ကို မိမိခိုင်မာစေသည်။

  2. Product Features and Specifications - စျေးကွက်ထဲရှိ နောက်ဆုံးထွက်ရှိထားသော product များ၏ features နှင့် specifications များကို စုဆောင်းနိုင်သည်။ အထူးသဖြင့် Tech, Consumer Electronics, Fashion စတဲ့ sectors များတွင် Web scraping ကို အသုံးပြုကာ ပြိုင်ဘက်ကုန်ပစ္စည်းများ၏ အရည်အသွေးနှင့် အထူးစွမ်းဆောင်ချက်များကို ဖော်ထုတ်နိုင်သည်။

  3. Consumer Reviews and Ratings - Customers ၏ အသုံးပြုမှုအတွေ့အကြုံများကို ရရှိရန် customer reviews နှင့် ratings များကို စုဆောင်းနိုင်သည်။ Web scraping ဖြင့် ရရှိသော customer reviews များက စျေးကွက်ခွဲခြမ်းစိတ်ဖြာမှုအတွက် လုံလောက်သော insights ရရှိစေသည်။ Customer reviews များကို sentiment analysis လုပ်၍ သုံးစွဲသူများ၏ အကြံပြုချက်များနှင့် သဘောထားကို ခွဲခြမ်းစိတ်ဖြာနိုင်သည်။

  4. Trends and Consumer Behavior Tracking - Web scraping ဖြင့် အချိန်အလိုက် market trends များနှင့် သုံးစွဲသူများ၏ စိတ်ဝင်စားမှုများကို ခြေရာခံနိုင်သည်။ ဥပမာအားဖြင့် season နဲ့လိုက်ပြီး သုံးစွဲသူများ၏ ဝယ်ယူမူပုံစံများကို လေ့လာနိုင်သည်။

  5. Competitive Analysis - ပြိုင်ဘက်ကုမ္ပဏီများ၏ လုပ်ဆောင်ချက်များကို ချိန်ချိန် စောင့်ကြည့်နိုင်သည်။ ဥပမာအားဖြင့် ပြိုင်ဘက်ကုန်ပစ္စည်းပေါ်တွင် မကြာခဏ လျှော့စျေးများ ထည့်သွင်း၍ စျေးကွက် အားလုံး၏ ဈေးကွက်အခြေအနေကို လေ့လာနိုင်သည်။

  6. Market Gap Identification - Web scraping အားဖြင့် မျိုးစုံသော product categories အလိုက် သုံးစွဲသူများ၏ တောင်းဆိုမှုကို စုဆောင်းနိုင်ပြီး၊ စျေးကွက်အတွင်း လက်ရှိတွင် မရှိသေးသော လိုအပ်ချက်များကို ဖော်ထုတ်နိုင်ပါသည်။

အနှစ်ချုပ်အားဖြင့် Market research အတွက် web scraping သည် စျေးကွက်အခြေအနေများ၊ ပြိုင်ဘက်အခြေအနေများနှင့် သုံးစွဲသူ၏ စိတ်ဝင်စားမှုများကို လေ့လာသင်ယူကာ သုံးသပ်ခြင်းအတွက် အရေးကြီးသော နည်းစနစ်တစ်ခုဖြစ်ပါသည်။

Web Content Scraping

လူတစ်ယောက် သို့မဟုတ် ကုမ္ပဏီတစ်ခုက အချို့သော ခေါင်းစဉ်နဲ့ ပတ်သက်တဲ့ အချက်အလက်တွေကို စုဆောင်းဖို့ web scraping ကို အသုံးပြုနိုင်ပါတယ်။ ဥပမာအားဖြင့် လူတစ်ယောက်က အားလုံးသော ဟင်းချက်နည်းတွေကို စုဆောင်းပြီး၊ တစ်ခုတည်းသော database ထဲမှာ ထည့်နိုင်ပါတယ်။

Web content scraping ကို အသုံးပြုပြီး လူတစ်ယောက် သို့မဟုတ် ကုမ္ပဏီတစ်ခုက အကွောင့်ကောင်းသောအချက်အလက်များကို အလွယ်တကူ စုဆောင်းနိုင်သည်။ ယင်းနည်းလမ်းကို အောက်ပါသဘောအရ အသုံးပြုနိုင်ပါသည် -

  1. Topic-Specific Content Aggregation - လူတစ်ယောက် သို့မဟုတ် အဖွဲ့အစည်းတစ်ခုသည် အကွောင့်သီးသန့် ခေါင်းစဉ်တစ်ခုနှင့် ပတ်သက်သော အချက်အလက်များကို စုဆောင်းကာ တစ်နေရာတည်းတွင် စုပြီး ဖော်ပြနိုင်သည်။ ဥပမာအားဖြင့် Cooking recipes, Travel tips, Health advice စသည့် ခေါင်းစဉ်အမျိုးမျိုးနှင့်ပတ်သက်သော အချက်အလက်များကို အလွယ်တကူ စုဆောင်းနိုင်ပါသည်။

  2. Recipe Collection Example - ဥပမာအားဖြင့် လူတစ်ယောက်က အမျိုးမျိုးသော websites မှ ဟင်းချက်နည်းများကို scraping နည်းလမ်းဖြင့် စုဆောင်းကာ တစ်ခုတည်းသော database ထဲသို့ အချက်အလက်များကို စုစည်းထားနိုင်သည်။ ဤနည်းဖြင့် ပြီးပြည့်စုံသော ဟင်းချက်နည်း မျိုးစုံကို စုဆောင်းထားနိုင်သည်။ ယင်း database ကို သုံး၍ လိုအပ်သော ဟင်းချက်နည်းများကို လွယ်ကူစွာ ရှာဖွေနိုင်မည် ဖြစ်သည်။

  3. Content Curation for Research or News - သတင်းရေးသားသူများနှင့် သုတေသနပြုသူများသည် သတင်းပုံစံ၊ အထင်ကရနောက်ဆုံးရ သတင်းများနှင့် ပတ်သက်သော အချက်အလက်များကို စုဆောင်း၍ ပြန်လည် လေ့လာနိုင်ပါသည်။ ဥပမာအားဖြင့်, နောက်ဆုံးရ စီးပွားရေးအခြေအနေများ၊ နည်းပညာအရာများနှင့် ပတ်သက်သော သတင်းများကို သီးခြားစုဆောင်းထားခြင်းအားဖြင့် အလွယ်တကူ ပြန်လည်သုံးစွဲနိုင်သည်။

  4. Building Specialized Search Engines - Web scraping ကို အသုံးပြုကာ တစ်ခုတည်းသော domain တွင် အထူးသီးသန့် search engines များ ဖန်တီးနိုင်သည်။ ဥပမာအားဖြင့်, ကုမ္ပဏီတစ်ခုက IT resources သီးသန့် search engine တစ်ခု ဖန်တီးလိုပါက IT-related sites များမှ ကျမ်းစာရင်းများ၊ ခေါင်းစဉ်နှင့် အကြောင်းအရာများကို ရှာဖွေနိုင်စေမည် ဖြစ်သည်။

  5. Data for Machine Learning Models - Web content scraping ကို အသုံးပြုကာ လေ့လာရေးနှင့် ပတ်သက်သော အချက်အလက်များကို စုဆောင်းနိုင်ပြီး ထို data များကို machine learning models များတွင် သုံးနိုင်ပါသည်။ ဥပမာအားဖြင့်, ပုံမှန်စာပေများနှင့် ပတ်သက်သော လေ့လာရေး မော်ဒယ်များတွင် လိုအပ်သောစာသား data များကို စုဆောင်းနိုင်ပြီး model ကို ပိုမိုတိုးတက်စေမည် ဖြစ်သည်။

  6. Sentiment Analysis - Web scraping ဖြင့် ပုံမှန်သုံးစွဲသူများ၏ feedback များနှင့် ပြန်လည်သုံးသပ်ချက်များကို စုဆောင်း၍ sentiment analysis လုပ်ခြင်းဖြင့် လူ့အဖွဲ့အစည်း၏ သဘောထားကို သိရှိနိုင်သည်။ ဤအချက်အလက်များကို စုပြီး မိမိဈေးကွက်အတွင်းရှိ customers များ၏ အမြင်ကို ပိုမိုကောင်းမွန်စွာ လေ့လာနိုင်သည်။

အထွေထွေအနေဖြင့် Web content scraping သည် အချိန်နှင့် လိုအပ်ချက်နှင့် ကိုက်ညီသော အချက်အလက်များကို စနစ်တကျ စုဆောင်းရန် အထောက်အကူပြုပါသည်။

Data Integration

Web scraping ကို ပေါင်းစပ်ထားတဲ့ ဒေတာတွေကို စုဆောင်းဖို့ အသုံးပြုနိုင်ပါတယ်။ ဒါကို news portal တစ်ခု သို့မဟုတ် စျေးနှုန်းနှိုင်းယှဉ်တဲ့ site တစ်ခု ဖန်တီးဖို့ အသုံးပြုနိုင်ပါတယ်။

Web scraping ကို အသုံးပြု၍ ပေါင်းစပ်ထားသော ဒေတာများကို စုဆောင်းခြင်းဖြင့်, မတူညီသော ဝဘ်ဆိုဒ်များမှ အချက်အလက်များကို တစ်နေရာတည်းတွင် ပြန်လည်စုပေါင်းနိုင်သည်။ ဤနည်းလမ်းကို အသုံးပြု၍ အမျိုးမျိုးသော သတင်းများ၊ စျေးနှုန်းများ၊ နှိုင်းယှဉ်ချက်များကို စနစ်တကျ စုဆောင်းကာ သုံးစွဲသူများအတွက် အကျိုးရှိစေသည်။

အောက်တွင် Data Integration အတွက် Web scraping ကို အသုံးပြုနည်းများကို ရှင်းပြထားပါတယ်။

  1. News Aggregation Portal - Web scraping ကို သတင်းများနှင့် သတင်းအကြောင်းအရာများကို စုဆောင်းရန် အသုံးပြုနိုင်သည်။ ဥပမာအားဖြင့်၊ လူကြိုက်များသော သတင်းဝဘ်ဆိုဒ်များမှ နောက်ဆုံးရ သတင်းများကို scraping လုပ်၍ ငြိမ်းချမ်းမှု၊ စီးပွားရေး၊ နည်းပညာ၊ ကျန်းမာရေး စတဲ့ ပိုင်းများအလိုက် စုဆောင်းကာ တစ်နေရာတည်းတွင် ဖော်ပြနိုင်သည်။ ဒီလိုမျိုးသော သတင်း portal တစ်ခုတွင် သတင်းအချက်အလက်များကို အချိန်နှင့် တပြေးညီ စိစစ်တင်ပြနိုင်သည်။

  2. Price Comparison Sites - Web scraping နည်းလမ်းကို အသုံးပြုပြီး မတူညီသော online stores နှင့် marketplace sites မှ တိကျသော product များ၏ စျေးနှုန်းများကို စုဆောင်းနိုင်သည်။ ဥပမာအားဖြင့်, Amazon, Walmart, Best Buy စတဲ့ စျေးနှုန်းများကို ချိန်ချိန်စောင့်ကြည့်ကာ စျေးနှုန်းနိမ့်ဆုံး product ကို သုံးစွဲသူများရှေ့တွင် ပြသပေးနိုင်သည်။ ဤနည်းလမ်းဖြင့် သုံးစွဲသူများသည် လက်ရှိအချိန်တွင် စျေးနှုန်းအကောင်းဆုံးကို ရွေးချယ်နိုင်စေသည်။

  3. Hotel and Flight Aggregators - Hotel and flight booking sites များတွင်ရှိသော အချက်အလက်များကို scraping လုပ်၍, hotel rooms နှင့် flights များ၏ စျေးနှုန်း၊ အချိန်၊ အသုံးပြုနိုင်သောပမာဏများကို စုပေါင်းနိုင်သည်။ အများအားဖြင့် Agoda, Booking.com, Kayak စတဲ့ site များမှ အချက်အလက်များကို စုဆောင်းကာ စျေးနှုန်းနှိုင်းယှဉ်မှုအတွက် အသုံးပြုနိုင်သည်။

  4. E-commerce Product Integration - မတူညီသော e-commerce websites များမှ product data များကို စုပေါင်းကာ ဝဘ်ဆိုဒ်တစ်ခုတွင် လုပ်ဆောင်နိုင်သည်။ ဥပမာအားဖြင့်, မတူညီသော online shops တွင်ရှိသော ပစ္စည်းအချက်အလက်များကို ပေါင်းစည်းကာ စိတ်ကြိုက် ပုံစံဖြင့် သုံးစွဲသူများအား ပြသနိုင်သည်။

  5. Stock Market Analysis - မတူညီသော သဘောတူ ဈေးကွက်များမှ စတော့ရှယ်ယာ data များကို စုဆောင်း၍ စတော့ရှယ်ယာ ဝဘ်ဆိုဒ် တစ်ခုအဖြစ် ဖော်ပြနိုင်သည်။ Web scraping ဖြင့် ပေါ်ပေါက်လာသော နောက်ဆုံးရ ဈေးကွက်လှုပ်ရှားမှုများကို သုံးစွဲသူများအတွက် အချိန်နှင့်တပြေးညီ တင်ပြနိုင်သည်။

  6. Real Estate Listings Aggregation - အိမ်ခြံမြေဝဘ်ဆိုဒ်များမှ သတင်းအချက်အလက်များကို ပေါင်းစည်းကာ ခြံမြေလိပ်စာ၊ အကျယ်အဝန်း၊ စျေးနှုန်းနှင့် တည်နေရာ စသည်ဖြင့် စုစည်းတင်ပြနိုင်သည်။

အကျဉ်းချုပ်အားဖြင့်, Data Integration အတွက် web scraping ကို အသုံးပြုခြင်းဖြင့် မျိုးစုံသော ဝဘ်ဆိုဒ်များမှ အချက်အလက်များကို စနစ်တကျ စုဆောင်းကာ, သုံးစွဲသူများအတွက် သုံးစွဲရလွယ်ကူသော platform တစ်ခုအဖြစ် ဖန်တီးနိုင်သည်။

သတိပေးချက်: ခေါင်းစဉ်ကို ဆက်လက်ဆွေးနွေးမည်မီ Web Scraping applications တွေက ဥပဒေရေးရာ တာဝန်တွေ ဖြစ်ပေါ်နိုင်ကြောင်း မှတ်သားထားရပါမယ်။ ဥပမာအားဖြင့် Website တစ်ခုရဲ့ terms of use က အဲဒီ site မှာ scraping လုပ်တာကို တားမြစ်ထားနိုင်ပါတယ်။ ထို့အပြင် စုဆောင်းထားတဲ့ ဒေတာကို အသုံးပြုတာကလည်း အမျိုးမျိုးသော ဒေတာကာကွယ်ရေးနဲ့ ကိုယ်ရေးကာကွယ်ရေး ဥပဒေတွေက အကျုံးဝင်နိုင်ပါတယ်။ ဒါကြောင့် Web scraping project တစ်ခု စတင်မည်ဆိုရင် ဒီအချက်တွေကို တွေ့ဆုံစဉ်းစားရပါမယ်။

Python က web scraping အတွက် အထူးသင့်လျော်တဲ့ tool တစ်ခုပါ၊ အထူးသဖြင့် library တွေအများကြီး ရှိတာနဲ့ အသုံးပြုရလွယ်ကူတာကြောင့်ပါပဲ။ ဒီသင်တန်းရဲ့ အပိုင်းမှာ Python နဲ့ web scraping ရဲ့ အခြေခံအကြောင်းအရာတွေကို ဆွေးနွေးပြီး၊ security professionals တွေ အလုပ်ရှင်နေ့စဉ်မှာ အသုံးပြုနိုင်တဲ့ ဥပမာတွေကို ကြည့်ပါမယ်။


Tools for Python Web Scraping

Python တွင် web scraping အတွက် Beautiful Soup၊ "Scrapy" နှင့် "Requests" ကဲ့သို့သော library များစွာရှိသည်။

Beautiful Soup

၎င်းသည် web scraping အတွက်သုံးသော Python library တစ်ခုဖြစ်သည်။ HTML နှင့် XML files များကို parse ပြုလုပ်ရန်၊ ခွဲခြမ်းစိပ်ဖြာရန် ၎င်းကို အသုံးပြုသည်။

တကယ်တော့ Beautiful Soup သည် HTML content ထဲမှ information များကို သူ့ဘာသာသူ scrape ပြုလုပ်နိုင်သော scraper မဟုတ်ဘဲ၊ ခွဲခြမ်းစိတ်ဖြာရန်အတွက် အသုံးပြုနိုင်သည့် parsing library တစ်ခုသာ ဖြစ်သည်။ ၎င်းကို web scraping tasks တွင် အသုံးပြုနိုင်သော်လည်း၊ scraping functionality ကို ပြည့်စုံစွာ အသုံးချရန်အတွက် "requests" ကဲ့သို့သော request libraries များနှင့် ပူးပေါင်းအသုံးပြုရမည် ဖြစ်သည်။

Beautiful Soup ၏ main power သည် HTML, XML ကဲ့သို့သော structured content များကို အလိုအလျောက် parse ပြုလုပ်နိုင်ခြင်း ဖြစ်ပြီး၊ format အမျိုးမျိုးကို ခွဲခြမ်းစိတ်ဖြာနိုင်သည့် စွမ်းရည်ရှိသည်။ သို့သော် ၎င်းသည် performance ကို ထိခိုက်စေနိုင်သည့် compromise မရှိပါ။

၎င်းသည် very powerful tool တစ်ခုဖြစ်သော်လည်း၊ အလွန်ကြီးမားသော projects များတွင် ၎င်းကို အသုံးပြုရန်အတွက် prefer မပြုလုပ်ပါ။

Scrapy

၎င်းသည် Beautiful Soup ထက် ပို၍ powerful ဖြစ်ပြီး web scraping library အဖြစ် feature များစွာကို ထည့်သွင်းပေးထားပါသည်။ ၎င်းတွင် parsing, requests ကို process လုပ်ခြင်းနှင့် data storage စသည့် features များလည်း ပါဝင်ပါသည်။

ဒီ tool သည် Beautiful Soup ရဲ့ functionality များကို လုပ်ဆောင်နိုင်သည့်အပြင် ကိုယ်ပိုင် request libraries များပါ ပါဝင်သည်။ ၎င်းကို လေ့လာရန် နည်းနည်း ရှုပ်ထွေးသော်လည်း၊ ကြီးမားပြီး ရှုပ်ထွေးသော project များတွင် request optimization, user-agent manipulation, data storage automation နှင့် middleware integration ကဲ့သို့သော လုပ်ဆောင်ချက်များအတွက် လည်းအထူး အကျိုးပြုသည်။

Requests

It is used to send HTTP requests in Python. This library is used to retrieve the content of a web page. There is no parser mechanism in itself. Although it is quite sufficient in cases where the raw response meets your needs, it can be quite tiring as you will be left with string functions to perform complex operations on the returned response.

Python is a wide world to which many developers contribute, and therefore, apart from the ones we have mentioned, there are dozens of different libraries/frameworks, etc. available that are specialized for various purposes. Although we cannot explain all of them here, we recommend that you investigate these options when you have a web scraping project.

# python3 -m venv my_env
# cd my_env
# source bin/activate

# pip3 install bs4
# pip3 install requests
# vi pyscrap.py
from bs4 import BeautifulSoup
import requests

URL = "https://aws.amazon.com"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
results = soup.find("meta", {"name": "description"})

print(results)
<meta content="Amazon Web Services offers reliable, scalable, and  inexpensive cloud computing services. Free to join, pay only for what you use." name="description"/>

Here’s a line-by-line breakdown of the code:

from bs4 import BeautifulSoup
  • This line imports the BeautifulSoup class from the bs4 library, which is used for parsing HTML and XML documents.

  • BeautifulSoup makes it easier to search, navigate, and modify the HTML structure of a web page.

import requests
  • This line imports the requests library, a popular Python library used to send HTTP requests.

  • It simplifies the process of connecting to websites or APIs to retrieve data, particularly HTML content, JSON, etc.

URL = "https://letsdefend.io"
  • This line assigns the URL of the website we want to scrape to the variable URL.

  • In this example, URL is set to "https://letsdefend.io".

page = requests.get(URL)
  • This line sends a GET request to the URL using the requests.get() function.

  • It retrieves the web page’s content and stores the response (which includes the HTML) in the page variable.

soup = BeautifulSoup(page.content, "html.parser")
  • This line creates a BeautifulSoup object, soup, which parses the HTML content of the page using Python’s built-in HTML parser.

  • page.content provides the HTML content from the request, and "html.parser" tells BeautifulSoup to use Python's HTML parser to structure the content.

results = soup.find("meta", {"name": "description"})
  • This line searches the parsed HTML (soup) for a <meta> tag with an attribute name set to "description".

  • soup.find() locates the first tag that matches the given criteria and returns it as a Tag object, stored in results.

  • This is often used to extract metadata, such as descriptions or keywords, embedded in the head section of HTML documents.

print(results)
  • This line prints the result of the search for the meta tag with name="description".

  • If found, results will contain the <meta> tag, including its attributes and content. If not, it will return None.

#To create a project with Scrapy.

"Scrapy" ကို အသုံးပြုပြီး project တစ်ခု ဖန်တီးမယ်ဆိုတာ ဘာလဲ

Scrapy ဆိုတာက Python programming language ကို အသုံးပြုပြီး web scraping လုပ်ဖို့ အထူးသင့်လျော်တဲ့ framework တစ်ခုပါ။ Scrapy ကို သုံးပြီး project တစ်ခု ဖန်တီးမယ်ဆိုတာကတော့ website တွေကနေ အချက်အလက်တွေကို စုဆောင်းဖို့ အတွက် Scrapy framework ကို အသုံးပြုပြီး project တစ်ခုကို စတင်ဖန်တီးခြင်းဖြစ်ပါတယ်။

Scrapy ကို အသုံးပြုပြီး project တစ်ခု ဖန်တီးဖို့ အခြေခံလမ်းညွှန်ကို အောက်မှာ ဖော်ပြထားပါတယ်။

  1. Scrapy ကို Install လုပ်ခြင်း: Scrapy ကို သင့် computer ubuntu ထဲမှာ install လုပ်ဖို့ apt ကို အသုံးပြုနိုင်ပါတယ်။ Terminal သို့မဟုတ် Command Prompt မှာ အောက်ပါ command ကို ရိုက်ထည့်ပါ။

     sudo apt install python3-scrapy
    
  2. Project တစ်ခု ဖန်တီးခြင်း: Scrapy project တစ်ခု ဖန်တီးဖို့အတွက် Terminal သို့မဟုတ် Command Prompt မှာ အောက်ပါ command ကို အသုံးပြုပါ။

     scrapy startproject letsdefend_scraper
    

    ဒီမှာ project_name ဆိုတာ သင့် project ရဲ့ နာမည်ဖြစ်ပါတယ်။

  3. Spider တစ်ခု ဖန်တီးခြင်း: Spider ဆိုတာကတော့ သတ်မှတ်ထားတဲ့ website မှာ ဝင်ရောက်ပြီး အချက်အလက်တွေကို စုဆောင်းဖို့ Scrapy မှာ အသုံးပြုတဲ့ class တစ်ခုပါ။ Spider တစ်ခု ဖန်တီးဖို့အတွက် project folder ထဲမှာ သွားပြီး အောက်ပါ command ကို အသုံးပြုပါ။

     scrapy genspider spider_name domain
    

    ဒီမှာ spider_name ဆိုတာ သင့် spider ရဲ့ နာမည်ဖြစ်ပြီး၊ domain ဆိုတာ သင့် spider က ဝင်ရောက်မယ့် website ရဲ့ domain ဖြစ်ပါတယ်။

  4. Spider ကို Configure လုပ်ခြင်း: Spider ကို ဖန်တီးပြီးနောက်မှာ အဲဒီ Spider ရဲ့ logic ကို configure လုပ်ရပါမယ်။ Spider file ထဲမှာ start_requests() method နဲ့ parse() method တွေကို define လုပ်ပြီး၊ အချက်အလက်တွေကို ဘယ်လို စုဆောင်းမယ်ဆိုတာကို ရေးသားရပါမယ်။

  5. Spider ကို Run လုပ်ခြင်း: Spider ကို run လုပ်ဖို့ Terminal သို့မဟုတ် Command Prompt မှာ အောက်ပါ command ကို အသုံးပြုပါ။

     scrapy crawl spider_name
    

    ဒီမှာ spider_name ဆိုတာ သင့် spider ရဲ့ နာမည်ဖြစ်ပါတယ်။

ဒီအဆင့်တွေကို လိုက်နာပြီး Scrapy ကို အသုံးပြုပြီး project တစ်ခုကို ဖန်တီးနိုင်ပါပြီ။ Scrapy က အရမ်းအားကောင်းတဲ့ framework ဖြစ်ပြီး၊ web scraping လုပ်ဖို့ အလွန်လွယ်ကူတဲ့ tool တစ်ခုပါ။