Computer >> Máy Tính >  >> Lập trình >> Python

Công cụ Python cho Web cạo

Trong khoa học máy tính Web cạo có nghĩa là trích xuất dữ liệu từ các trang web. Sử dụng kỹ thuật này để chuyển đổi dữ liệu phi cấu trúc trên web thành dữ liệu có cấu trúc.

Hầu hết các công cụ Scraping web phổ biến trong Python3 là -

  • Urllib2
  • Yêu cầu
  • BeautifulSoup
  • Lxml
  • Selenium
  • MechanicalSoup

Urllib2 - Công cụ này được cài đặt sẵn Python. Mô-đun này được sử dụng để trích xuất URL. Sử dụng hàm urlopen () để tìm nạp URL bằng các giao thức khác nhau (FTP, HTTPetc.).

Mã mẫu

 from urllib.request import urlopenmy_html =urlopen ("https://www.tutorialspoint.com/") print (my_html.read ()) 

Đầu ra

 b ' \ r \ n   \ r \ n  Parallax Scrolling, Java Cryptography, YAML, Python Data Science, Java i18n, GitLab, TestRail, VersionOne, DBUtils, Common CLI, Seaborn, Ansible, LOLCODE, Current Affairs 2018, Apache Commons Collections  \ r \ n  \ r \ n  \ r \ n  \ r \ n  \ r \ n  \ r \ n 
\ r \ n
\ r \ n
\ r \ n Trang chủ \ r \ n
\ r \ n
\ r \ n Q / A \ r \ n
\ r \ n
\ r \ n Công cụ \ r \ n
\ r \ n
​​\ r \ n Coding Ground \ r \ n
\ r \ n \ r \ n
\ r \ n Ghi chú của UPSC \ r \ n
\ r \ n
\ r \ n Gia sư Trực tuyến \ r \ n
\ r \ n
\ r \ n….

Yêu cầu - Mô-đun này không được cài đặt sẵn, chúng tôi phải viết dòng lệnh trong dấu nhắc lệnh. Yêu cầu gửi yêu cầu tới HTTP / 1.1.

yêu cầu cài đặt pip

Ví dụ

 nhập yêu cầu # get URLmy_req =request.get ('https://www.tutorialspoint.com/') print (my_req.encoding) print (my_req.status_code) print (my_req.elapsed) print (my_req.url) print (my_req.history) print (my_req.headers ['Content-Type']) 

Đầu ra

 UTF-82000:00:00.205727https://www.tutorialspoint.com/ [] text / html; bảng mã =​​UTF-8 

BeautifulSoup - Đây là một thư viện phân tích cú pháp được sử dụng trong các trình phân tích cú pháp khác nhau. Thư viện tiêu chuẩn của Python cung cấp trình phân tích cú pháp mặc định của BeautifulSoup. Nó xây dựng một cây phân tích cú pháp được sử dụng để trích xuất dữ liệu từ trang HTML.

Để cài đặt mô-đun này, chúng tôi viết dòng lệnh trong dấu nhắc lệnh.

pip cài đặt beautifulsoup4

Ví dụ

 từ bs4 nhập BeautifulSoup # nhập yêu cầu nhập yêu cầu # lấy URLmy_req =request.get ("https://www.tutorialspoint.com/") my_data =my_req.textmy_soup =BeautifulSoup (my_data) cho my_link trong my_soup.find_all (' a '):print (my_link.get (' href ')) 

Đầu ra

 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 / Progra_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_algorithm/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/mysql/index.htmhttps://www.tutorialspoint.com/web_development_tutorials.htmhttps://www.tutorialspoint.com/cprogramming/index.htmhttps://www.tutorialspoint.com/cplusplus/ind ex.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/effective_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 / 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 ://www.facebook.com/tutorialspointindiahttps://plus.google.com/u/0/+tutorialspointhttps://www.twitter.com/tutorialspointhttps://www.linkedin.com/company/tutorialspointhttps:// 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 - Đây là thư viện phân tích cú pháp, thư viện phân tích cú pháp HTML và XML hiệu suất cao, chất lượng sản xuất. Nếu chúng ta muốn chất lượng cao, tốc độ tối đa, thì chúng ta phải sử dụng thư viện này. Nó có nhiều mô-đun mà chúng tôi có thể trích xuất dữ liệu từ trang web.

Để cài đặt, chúng tôi viết trong Command prompt

pip cài đặt lxml

Ví dụ

 from lxml import 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. tostring (my_root_elem, pretty_print =True) .decode ("utf-8")) 

Đầu ra

    <body /> </html> </pre> <p>
 <strong> Selenium </strong> - Đây là một công cụ tự động hóa trình duyệt, nó còn được gọi là trình điều khiển web. Khi chúng tôi sử dụng bất kỳ trang web nào, chúng tôi nhận thấy rằng đôi khi chúng tôi phải đợi một lúc, chẳng hạn như khi chúng tôi nhấp vào bất kỳ nút nào hoặc cuộn trang, trong thời điểm này, Selenium là cần thiết. </P> <p>
 Để cài đặt selen, chúng tôi sử dụng lệnh này </P> <p>
 pip cài đặt selen </P> <h2> Ví dụ </h2> <pre> from selenium import webdrivermy_path_to_chromedriver ='/ Users / Admin / Desktop / chromedriver'my_browser =webdriver.Chrome (execute_path =my_path_to_chromedriver) my_url =' https://www.</tutorialspoint.com/'my_browser.get(my_url)(my_url). trước> <h2> Đầu ra </h2> <p>
 <img loading='lazy' alt='Công cụ Python cho Web cạo ' src="/article/uploadfiles/202204/2022040701542865.jpg" /> </P> <p>
 <strong> MechanicalSoup </strong> - Đây là một thư viện Python khác để tự động hóa tương tác với các trang web. Bằng cách sử dụng điều này, chúng tôi có thể tự động lưu trữ và gửi cookie, có thể theo dõi chuyển hướng và có thể theo liên kết và gửi biểu mẫu. Nó không chạy JavaScript. </P> <p>
 Để cài đặt, chúng ta có thể sử dụng lệnh sau </P> <p>
 pip cài đặt MechanicalSoup </P> <h2> Ví dụ </h2> <pre> import 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 ("biểu mẫu") print (my_va) my_value1 =my_browser.get_url () print (my_value1) </pre> <br>
                </article>

                <div class="ad5">
<script language='javascript' src='https://vn.wsxdn.com/css/ad/vn2.js'></script>

                </div>

            </section>

            
            <div id="turn-page" class="f-between">
                <div class="page up f-align">
                    <i class="prev"></i>
                    <section>
                        <a class='LinkPrevArticle' href='https://vn.wsxdn.com/fd034x/ur348m/1003106443.html' >Đọc và ghi vào tệp excel bằng mô-đun Python openpyxl </a>     
                    </section>
                </div>
                <div class="page down f-align">
                    <section>
                        <a class='LinkNextArticle' href='https://vn.wsxdn.com/fd034x/ur348m/1003106445.html'  >Chương trình Python để triển khai trò chơi Rock Paper Scissor </a>   
                    </section>
                    <i class="next"></i>
                </div>
             </div>

             <section class="box4-l-list">

                <div class="imgList f-between banner">

                   
                    
                    <ul class="f-between">

 
<li class="f-between f-align"><div>
<a href="https://vn.wsxdn.com/fd034x/ur348m/1003105954.html" class="text r-over r-over-3" title="Triển khai quét web bằng lxml trong Lập trình Python ">Triển khai quét web bằng lxml trong Lập trình Python </a>
</div></li>
 
<li class="f-between f-align"><div>
<a href="https://vn.wsxdn.com/fd034x/ur348m/1003106241.html" class="text r-over r-over-3" title="Web Scraping sử dụng Python và Scrapy? ">Web Scraping sử dụng Python và Scrapy? </a>
</div></li>
 
<li class="f-between f-align"><div>
<a href="https://vn.wsxdn.com/fd034x/ur348m/1003106255.html" class="text r-over r-over-3" title="Triển khai quét web bằng lxml trong Python? ">Triển khai quét web bằng lxml trong Python? </a>
</div></li>
 
<li class="f-between f-align"><div>
<a href="https://vn.wsxdn.com/yw036q/nl343f/1003136961.html" class="text r-over r-over-3" title="31 Công cụ Scraping Web Tốt nhất ">31 Công cụ Scraping Web Tốt nhất </a>
</div></li>
 
<li class="f-between f-align"><div>
<a href="https://vn.wsxdn.com/ec031w/zx314s/1003148987.html" class="text r-over r-over-3" title="Công cụ thiết kế web tốt nhất dành cho chuyên gia">Công cụ thiết kế web tốt nhất dành cho chuyên gia</a>
</div></li>
     


			

                    </ul>
                </div>

            </section>



           

        </section>

        <aside class="box-r box4-r">

            <div class="articleList">
                <em>Bài báo nổi tiếng</em>
                <ol>
 
<li><a href="https://vn.wsxdn.com/fd034x/ur348m/1003103141.html" class="t-over" title="Kiểm tra Không có Tuple bằng Python ">Kiểm tra Không có Tuple bằng Python </a></li>
 
<li><a href="https://vn.wsxdn.com/fd034x/ur348m/1003105425.html" class="t-over" title="CGI trong Python là gì? ">CGI trong Python là gì? </a></li>
 
<li><a href="https://vn.wsxdn.com/fd034x/ur348m/1003105762.html" class="t-over" title="Chương trình Python cho Stooge Sort ">Chương trình Python cho Stooge Sort </a></li>
 
<li><a href="https://vn.wsxdn.com/fd034x/ur348m/1003105763.html" class="t-over" title="Chương trình Python cho Sieve of Eratosthenes ">Chương trình Python cho Sieve of Eratosthenes </a></li>
 
<li><a href="https://vn.wsxdn.com/fd034x/ur348m/1003105935.html" class="t-over" title="Triển khai Python Web Scraping với Scrapy ">Triển khai Python Web Scraping với Scrapy </a></li>
 
<li><a href="https://vn.wsxdn.com/fd034x/ur348m/1003105955.html" class="t-over" title="Triển khai Web Scraping bằng Python với Scrapy ">Triển khai Web Scraping bằng Python với Scrapy </a></li>
 
<li><a href="https://vn.wsxdn.com/fd034x/ur348m/1003106157.html" class="t-over" title="Triển khai Web Scraping bằng Python với BeautifulSoup? ">Triển khai Web Scraping bằng Python với BeautifulSoup? </a></li>
 
<li><a href="https://vn.wsxdn.com/fd034x/ur348m/1003106231.html" class="t-over" title="Mô-đun Python Báo cho bài viết cóp nhặt &giám tuyển? ">Mô-đun Python Báo cho bài viết cóp nhặt &giám tuyển? </a></li>
 
</ol>
            </div>

            <section>

                <strong class="list-title">
                    <p>Máy Tính</p>
                    <span></span>
                </strong>

           
                <ol class="texts">
 
<li>
<a href="https://vn.wsxdn.com/fd034x/ur348m/1003105766.html" class="t-title"><strong class="r-over r-over-2">Chương trình Python cho QuickSort </strong></a>
</li>
 
<li>
<a href="https://vn.wsxdn.com/fd034x/ur348m/1003105770.html" class="t-title"><strong class="r-over r-over-2">Chương trình Python để Hợp nhất Sắp xếp </strong></a>
</li>
 
<li>
<a href="https://vn.wsxdn.com/fd034x/ur348m/1003105774.html" class="t-title"><strong class="r-over r-over-2">Chương trình Python cho Sắp xếp đống </strong></a>
</li>
 
<li>
<a href="https://vn.wsxdn.com/fd034x/ur348m/1003105781.html" class="t-title"><strong class="r-over r-over-2">Chương trình Python để cắt một thanh </strong></a>
</li>
 
<li>
<a href="https://vn.wsxdn.com/fd034x/ur348m/1003105783.html" class="t-title"><strong class="r-over r-over-2">Chương trình Python để thay đổi tiền xu </strong></a>
</li>
 
<li>
<a href="https://vn.wsxdn.com/fd034x/ur348m/1003105784.html" class="t-title"><strong class="r-over r-over-2">Chương trình Python để sắp xếp cocktail </strong></a>
</li>
                                     
</ol>
</section>

            <ul class="typeList container f-start box4-typeList">
                <li><a class='childclass' href='https://vn.wsxdn.com/fd034x/sq344k/' target="_self">lập trình C</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/ay345t/' target="_self">C ++</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/zx346r/' target="_self">Redis</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/nk347f/' target="_self">Lập trình BASH</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/ur348m/' target="_self">Python</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/ca349u/' target="_self">Java</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/hf350z/' target="_self">Cơ sở dữ liệu</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/om351g/' target="_self">HTML</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/tr352l/' target="_self">Javascript</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/ay353s/' target="_self">Lập trình</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/jg354b/' target="_self">CSS</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/nl355g/' target="_self">Ruby</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/mk356e/' target="_self">SQL</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/ge357z/' target="_self">IOS</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/ec358x/' target="_self">Android</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/ol359g/' target="_self">MongoDB</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/vt360n/' target="_self">MySQL</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/vt361n/' target="_self">C#</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/wu365o/' target="_self">PHP</a></li>
<li><a class='childclass' href='https://vn.wsxdn.com/fd034x/us371m/' target="_self">SQL Server</a></li>
            </ul>

        </aside>

    </section>


    <footer class="container">

        <span class="container f-center">
                Bản quyền ©
                <a href="https://vn.wsxdn.com">https://vn.wsxdn.com</a>
                Đã đăng ký Bản quyền
            </span>


    </footer>


</body>
</html>