ในทางวิทยาการคอมพิวเตอร์ Web scraping หมายถึงการดึงข้อมูลจากเว็บไซต์ การใช้เทคนิคนี้จะเปลี่ยนข้อมูลที่ไม่มีโครงสร้างบนเว็บให้เป็นข้อมูลที่มีโครงสร้าง
เครื่องมือขูดเว็บทั่วไปส่วนใหญ่ใน Python3 ได้แก่ −
- Urllib2
- คำขอ
- ซุปสวย
- Lxml
- ซีลีเนียม
- ซุปเครื่องกล
Urllib2 − เครื่องมือนี้ได้รับการติดตั้งไว้ล่วงหน้าด้วย Python โมดูลนี้ใช้สำหรับแยก URL การใช้ฟังก์ชัน urlopen () ดึงข้อมูล URL โดยใช้โปรโตคอลต่างๆ (FTP, HTTP เป็นต้น)
โค้ดตัวอย่าง
จาก urllib.request นำเข้า urlopenmy_html =urlopen("https://www.tutorialspoint.com/")print(my_html.read())
ผลลัพธ์
b'\r\n \r\n
คำขอ − โมดูลนี้ไม่ได้ติดตั้งไว้ล่วงหน้า เราต้องเขียนบรรทัดคำสั่งในพรอมต์คำสั่งคำขอ ส่งคำขอไปที่ HTTP/1.1
คำขอติดตั้ง pip
ตัวอย่าง
คำขอนำเข้า# รับ URLmy_req =request.get('https://www.tutorialspoint.com/') พิมพ์ (my_req.encoding) พิมพ์ (my_req.status_code) พิมพ์ (my_req.elapsed) พิมพ์ (my_req.url) print(my_req.history)print(my_req.headers['Content-Type'])
ผลลัพธ์
UTF-82000:00:00.205727https://www.tutorialspoint.com/[]text/html; charset=UTF-8
ซุปสวย − นี่คือไลบรารีการแยกวิเคราะห์ที่ใช้ในตัวแยกวิเคราะห์ต่างๆ ไลบรารีมาตรฐานของ Python จัดเตรียม parser เริ่มต้นของ BeautifulSoup สร้าง parser tree ซึ่งใช้ในการดึงข้อมูลจากหน้า HTML
สำหรับการติดตั้งโมดูลนี้ เราเขียนบรรทัดคำสั่งในพร้อมท์คำสั่ง
pip ติดตั้ง beautifulsoup4
ตัวอย่าง
จาก bs4 นำเข้า BeautifulSoup# คำขอนำเข้าคำขอนำเข้า # รับ URLmy_req =Request.get("https://www.tutorialspoint.com/")my_data =my_req.textmy_soup =BeautifulSoup(my_data) สำหรับ my_link ใน my_soup.find_all(' a'):print(my_link.get('href'))
ผลลัพธ์
https://www.tutorialspoint.com/index.htmhttps://www.tutorialspoint.com/questions/index.phphttps://www.tutorialspoint.com/online_dev_tools.htmhttps://www.tutorialspoint.com /codingground.htmhttps://www.tutorialspoint.com/current_affairs/index.htmhttps://www.tutorialspoint.com/upsc_ias_exams.htmhttps://www.tutorialspoint.com/tutor_connect/index.phphttps://www.tutorialspoint .com/programming_examples/https://www.tutorialspoint.com/whiteboard.htmhttps://www.tutorialspoint.com/netmeeting.phphttps://www.tutorialspoint.com/articles/https://www.tutorialspoint.com /index.htmhttps://www.tutorialspoint.com/tutorialslibrary.htmhttps://www.tutorialspoint.com/videotutorials/index.htmhttps://store.tutorialspoint.comhttps://www.tutorialspoint.com/html_online_training/index .asphttps://www.tutorialspoint.com/css_online_training/index.asphttps://www.tutorialspoint.com/3d_animation_online_training/index.asphttps://www.tutorialspoint.com/swift_4_online_training/index.asphttps://www.tutorialspoint .com/blo ckchain_online_training/index.asphttps://www.tutorialspoint.com/reactjs_online_training/index.asphttps://www.tutorialspoint.com/tutorialslibrary.htmhttps://www.tutorialspoint.com/computer_fundamentals/index.htmhttps://www. tutorialspoint.com/compiler_design/index.htmhttps://www.tutorialspoint.com/operating_system/index.htmhttps://www.tutorialspoint.com/data_structures_algorithms/index.htmhttps://www.tutorialspoint.com/dbms/index. htmhttps://www.tutorialspoint.com/data_communication_computer_network/index.htmhttps://www.tutorialspoint.com/academic_tutorials.htmhttps://www.tutorialspoint.com/html/index.htmhttps://www.tutorialspoint.com/ css/index.htmhttps://www.tutorialspoint.com/javascript/index.htmhttps://www.tutorialspoint.com/php/index.htmhttps://www.tutorialspoint.com/angular4/index.htmhttps://www.tutorialspoint.com/angular4/index.htm www.tutorialspoint.com/mysql/index.htmhttps://www.tutorialspoint.com/web_development_tutorials.htmhttps://www.tutorialspoint.com/cprogramming/index.htmhttps://www.tutorialspoint.com/cplusplus/ind อดีตhtmhttps://www.tutorialspoint.com/java8/index.htmhttps://www.tutorialspoint.com/python/index.htmhttps://www.tutorialspoint.com/scala/index.htmhttps://www. tutorialspoint.com/csharp/index.htmhttps://www.tutorialspoint.com/computer_programming_tutorials.htmhttps://www.tutorialspoint.com/java8/index.htmhttps://www.tutorialspoint.com/jdbc/index.htmhttps://www.tutorialspoint.com/servlets/index.htmhttps://www.tutorialspoint.com/spring/index.htmhttps://www.tutorialspoint.com/hibernate/index.htmhttps://www.tutorialspoint.com/ swing/index.htmhttps://www.tutorialspoint.com/java_technology_tutorials.htmhttps://www.tutorialspoint.com/android/index.htmhttps://www.tutorialspoint.com/swift/index.htmhttps://www. tutorialspoint.com/ios/index.htmhttps://www.tutorialspoint.com/kotlin/index.htmhttps://www.tutorialspoint.com/react_native/index.htmhttps://www.tutorialspoint.com/xamarin/index. htmhttps://www.tutorialspoint.com/mobile_development_tutorials.htmhttps://www.tutorialspoint.com/mongodb/index.htmhtt PS://www.tutorialspoint.com/plsql/index.htmhttps://www.tutorialspoint.com/sql/index.htmhttps://www.tutorialspoint.com/db2/index.htmhttps://www.tutorialspoint com/mysql/index.htmhttps://www.tutorialspoint.com/memcached/index.htmhttps://www.tutorialspoint.com/database_tutorials.htmhttps://www.tutorialspoint.com/asp.net/index.htmhttps://www.tutorialspoint.com/entity_framework/index.htmhttps://www.tutorialspoint.com/vb.net/index.htmhttps://www.tutorialspoint.com/ms_project/index.htmhttps://www.tutorialspoint com/excel/index.htmhttps://www.tutorialspoint.com/word/index.htmhttps://www.tutorialspoint.com/microsoft_technologies_tutorials.htmhttps://www.tutorialspoint.com/big_data_analytics/index.htmhttps:// www.tutorialspoint.com/hadoop/index.htmhttps://www.tutorialspoint.com/sas/index.htmhttps://www.tutorialspoint.com/qlikview/index.htmhttps://www.tutorialspoint.com/power_bi/ index.htmhttps://www.tutorialspoint.com/tableau/index.htmhttps://www.tutorialspoint.com/big_data_tutorials.htmhttps://ww w.tutorialspoint.com/tutorialslibrary.htmhttps://www.tutorialspoint.com/codingground.htmhttps://www.tutorialspoint.com/coding_platform_for_websites.htmhttps://www.tutorialspoint.com/developers_best_practices/index.htmhttps://www.tutorialspoint.com/developers_best_practices/index.htm www.tutorialspoint.com/efficient_resume_writing.htmhttps://www.tutorialspoint.com/computer_glossary.htmhttps://www.tutorialspoint.com/computer_whoiswho.htmhttps://www.tutorialspoint.com/questions_and_answers.htmhttps://www. tutorialspoint.com/multi_language_tutorials.htmhttps://itunes.apple.com/us/app/tutorials-point/id914891263?ls=1&mt=8https://play.google.com/store/apps/details?id=com tutorialspoint.onlineviewerhttps://www.windowsphone.com/s?appid=91249671-7184-4ad6-8a5f-d11847946b09/about/index.htm/about/about_team.htm/about/about_careers.htm/about/about_privacy.htm/ เกี่ยวกับ/about_terms_of_use.htmhttps://www.tutorialspoint.com/articles/https://www.tutorialspoint.com/online_dev_tools.htmhttps://www.tutorialspoint.com/free_web_graphics.htmhttps://www.tutorialspoint.com /online_file_conversion.htmhttps://www.tutorialspoint.com/shared-tutorials.phphttps://www.tutorialspoint.com/netmeeting.phphttps://www.tutorialspoint.com/free_online_whiteboard.htmhttps://www.tutorialspoint.comhttps ที่https://www.facebook.com/tutorialspointindiahttps://plus.google.com/u/0/+tutorialspointhttps://www.twitter.com/tutorialspointhttps://www.linkedin.com/company/tutorialspointhttps://www.facebook.com/tutorialspointindia www.youtube.com/channel/UCVLbzhxVTiTLiVKeGV7WEBghttps://www.tutorialspoint.com/index.htm/about/about_privacy.htm#cookies/about/faq.htm/about/about_helping.htm/about/contact_us.htm
Lxml − นี่คือไลบรารีการแยกวิเคราะห์ ไลบรารีแยกวิเคราะห์ HTML และ XML คุณภาพการผลิตที่มีประสิทธิภาพสูง ถ้าเราต้องการคุณภาพสูง ความเร็วสูงสุด เราก็ต้องใช้ไลบรารี่นี้ มีโมดูลมากมายที่เราสามารถดึงข้อมูลจากเว็บไซต์ได้
สำหรับการติดตั้งเราเขียนในพรอมต์คำสั่ง
pip ติดตั้ง lxml
ตัวอย่าง
จากการนำเข้า lxml etreemy_root_elem =etree.Element('html')etree.SubElement(my_root_elem, 'head')etree.SubElement(my_root_elem, 'title')etree.SubElement(my_root_elem, 'body')print (etree.js) tostring(my_root_elem, pretty_print =True).decode("utf-8"))
ผลลัพธ์
<body/></html></pre> <p> <strong>ซีลีเนียม</strong> − นี่คือเครื่องมือเบราว์เซอร์อัตโนมัติ เรียกอีกอย่างว่าไดรเวอร์เว็บ เมื่อเราใช้เว็บไซต์ใด ๆ เราสังเกตว่าบางครั้งเราต้องรอสักครู่ เช่น เมื่อเราคลิกปุ่มใดๆ หรือเลื่อนหน้า ในช่วงเวลานี้จำเป็นต้องมีซีลีเนียม</P> <p> สำหรับการติดตั้งซีลีเนียมเราใช้คำสั่งนี้</P> <p> pip ติดตั้งซีลีเนียม</P> <h2>ตัวอย่าง</h2> <pre>จากการนำเข้าซีลีเนียม webdrivermy_path_to_chromedriver ='/Users/Admin/Desktop/chromedriver'my_browser =webdriver.Chrome(executable_path =my_path_to_chromedriver)my_url ='https://www.tutorialspoint.com/'my_browser.get (my_url) ก่อน> <h2>ผลลัพธ์</h2> <p> <img loading='lazy' alt='Python Tools สำหรับการขูดเว็บ ' src="/article/uploadfiles/202204/2022040701542865.jpg" /></P> <p> <strong>ซุปเครื่องกล</strong> - นี่คือไลบรารี Python อื่นสำหรับการโต้ตอบกับเว็บไซต์โดยอัตโนมัติ เมื่อใช้สิ่งนี้ เราสามารถจัดเก็บและส่งคุกกี้โดยอัตโนมัติ สามารถติดตามการเปลี่ยนเส้นทาง และสามารถติดตามลิงก์และส่งแบบฟอร์ม ไม่ทำจาวาสคริปต์</P> <p> สำหรับการติดตั้ง เราสามารถใช้คำสั่งต่อไปนี้</P> <p> pip ติดตั้ง MechanicalSoup</P> <h2>ตัวอย่าง</h2> <pre>นำเข้า mechanicalsoupmy_browser =mechanicalsoup.StatefulBrowser()my_value =my_browser.open("https://www.tutorialspoint.com/")print(my_value)my_val =my_browser.get_url()print(my_val)my_va =my_browser.follow_link ("แบบฟอร์ม")print(my_va)my_value1 =my_browser.get_url()print(my_value1)</pre> <br> </article> <div class="ad ad5"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4480737146802772" crossorigin="anonymous"></script><!-- computer.wsxdn --><ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-4480737146802772" data-ad-slot="4467655591" data-ad-format="auto" data-full-width-responsive="true"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <section id="turn-page" class="t-over"> <div class="f-start f-align"> <i><img loading='lazy' src="https://th.wsxdn.com/css/img/sanjiao.svg" alt=""></i> <a class='LinkPrevArticle' href='https://th.wsxdn.com/db044w/wu448o/1004106443.html' >อ่านและเขียนไปยังไฟล์ excel โดยใช้ Python openpyxl module </a> </div> <div class="f-start f-align"> <i><img loading='lazy' src="https://th.wsxdn.com/css/img/sanjiao.svg" alt=""></i> <a class='LinkNextArticle' href='https://th.wsxdn.com/db044w/wu448o/1004106445.html' >โปรแกรม Python เพื่อใช้งานเกม Rock Paper Scissor </a> </div> </section> <section class="box1-3"> <ol class="f-between"> <li class="aList-2 aList f-between f-align"> <div> <a href="https://th.wsxdn.com/db044w/wu448o/1004106241.html" class="title"> <p class="r-over r-over-2" title="การขูดเว็บโดยใช้ Python และ Scrapy? "> การขูดเว็บโดยใช้ Python และ Scrapy? </p> </a> </div> </li><li class="aList-2 aList f-between f-align"> <div> <a href="https://th.wsxdn.com/db044w/wu448o/1004106255.html" class="title"> <p class="r-over r-over-2" title="ใช้การขูดเว็บโดยใช้ lxml ใน Python หรือไม่ "> ใช้การขูดเว็บโดยใช้ lxml ใน Python หรือไม่ </p> </a> </div> </li><li class="aList-2 aList f-between f-align"> <div> <a href="https://th.wsxdn.com/ge046y/pn443i/1004136961.html" class="title"> <p class="r-over r-over-2" title="31 เครื่องมือขูดเว็บที่ดีที่สุด "> 31 เครื่องมือขูดเว็บที่ดีที่สุด </p> </a> </div> </li><li class="aList-2 aList f-between f-align"> <div> <a href="https://th.wsxdn.com/ec041w/ge414y/1004148987.html" class="title"> <p class="r-over r-over-2" title="เครื่องมือออกแบบเว็บไซต์ที่ดีที่สุดสำหรับมืออาชีพ"> เครื่องมือออกแบบเว็บไซต์ที่ดีที่สุดสำหรับมืออาชีพ </p> </a> </div> </li> </ol> </section> </section> <aside class="box-r"> <section class="box4-1"> <strong> Python </strong> <ol> <li class="f-start"> <i> <img loading='lazy' src="https://th.wsxdn.com/css/img/article-lan.svg" alt=""> </i> <a href="https://th.wsxdn.com/db044w/wu448o/1004103141.html" title="ตรวจหาไม่มีทูเพิลใน Python "> <p class="r-over r-over-2">ตรวจหาไม่มีทูเพิลใน Python </p> </a> </li> <li class="f-start"> <i> <img loading='lazy' src="https://th.wsxdn.com/css/img/article-lan.svg" alt=""> </i> <a href="https://th.wsxdn.com/db044w/wu448o/1004105935.html" title="Python ใช้ Web Scraping ด้วย Scrapy "> <p class="r-over r-over-2">Python ใช้ Web Scraping ด้วย Scrapy </p> </a> </li> <li class="f-start"> <i> <img loading='lazy' src="https://th.wsxdn.com/css/img/article-lan.svg" alt=""> </i> <a href="https://th.wsxdn.com/db044w/wu448o/1004105955.html" title="การใช้ Web Scraping ใน Python ด้วย Scrapy "> <p class="r-over r-over-2">การใช้ Web Scraping ใน Python ด้วย Scrapy </p> </a> </li> <li class="f-start"> <i> <img loading='lazy' src="https://th.wsxdn.com/css/img/article-lan.svg" alt=""> </i> <a href="https://th.wsxdn.com/db044w/wu448o/1004106157.html" title="การนำ Web Scraping ไปใช้ใน Python ด้วย BeautifulSoup หรือไม่ "> <p class="r-over r-over-2">การนำ Web Scraping ไปใช้ใน Python ด้วย BeautifulSoup หรือไม่ </p> </a> </li> <li class="f-start"> <i> <img loading='lazy' src="https://th.wsxdn.com/css/img/article-lan.svg" alt=""> </i> <a href="https://th.wsxdn.com/db044w/wu448o/1004106231.html" title="หนังสือพิมพ์โมดูล Python สำหรับการขูดและการดูแลบทความ? "> <p class="r-over r-over-2">หนังสือพิมพ์โมดูล Python สำหรับการขูดและการดูแลบทความ? </p> </a> </li> </ol> </section> <ul> <li class="f-start f-align"> <a href="https://th.wsxdn.com/db044w/wu448o/1004105784.html" class="i-text"><p class="r-over r-over-3">โปรแกรม Python สำหรับค็อกเทล Sort </p></a> </li> <li class="f-start f-align"> <a href="https://th.wsxdn.com/db044w/wu448o/1004105788.html" class="i-text"><p class="r-over r-over-3">โปรแกรม Python สำหรับการหมุนอาร์เรย์ </p></a> </li> <li class="f-start f-align"> <a href="https://th.wsxdn.com/db044w/wu448o/1004105925.html" class="i-text"><p class="r-over r-over-3">โปรแกรม Python สำหรับดอกเบี้ยทบต้น </p></a> </li> <li class="f-start f-align"> <a href="https://th.wsxdn.com/db044w/wu448o/1004105934.html" class="i-text"><p class="r-over r-over-3">Python ใช้งานการขูดเว็บโดยใช้lxml </p></a> </li> <li class="f-start f-align"> <a href="https://th.wsxdn.com/db044w/wu448o/1004105954.html" class="i-text"><p class="r-over r-over-3">การใช้การขูดเว็บโดยใช้ lxml ในการเขียนโปรแกรม Python </p></a> </li> </ul> <ul class="types f-between"> <li><a class='childclass' href='https://th.wsxdn.com/db044w/hf444z/' target="_self">การเขียนโปรแกรม C</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/bz445u/' target="_self">C++</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/tr446l/' target="_self">Redis</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/rp447j/' target="_self">การเขียนโปรแกรม BASH</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/wu448o/' target="_self">Python</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/nl449f/' target="_self">Java</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/ig450a/' target="_self">ฐานข้อมูล</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/zx451s/' target="_self">HTML</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/ec452w/' target="_self">Javascript</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/db453v/' target="_self">การเขียนโปรแกรม</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/if454a/' target="_self">CSS</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/ge455y/' target="_self">Ruby</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/xv456p/' target="_self">SQL</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/jh457b/' target="_self">IOS</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/om458g/' target="_self">Android</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/mk459f/' target="_self">MongoDB</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/rp460j/' target="_self">MySQL</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/qn461i/' target="_self">C#</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/jh465b/' target="_self">PHP</a></li> <li><a class='childclass' href='https://th.wsxdn.com/db044w/kh471c/' target="_self">SQL Server</a></li> </ul> </aside> </section> <footer> <section class="msg f-center container"> <span class="f-start"> ลิขสิทธิ์ © <a href="https://th.wsxdn.com">https://th.wsxdn.com</a> สงวนลิขสิทธิ์ </span> </section> </footer> </body> </html>