Như chúng ta đã biết, Selenium là một công cụ tự động hóa dựa trên web giúp chúng ta tự động hóa các trình duyệt. Selenium là một công cụ thử nghiệm nguồn mở, có nghĩa là chúng tôi có thể dễ dàng tải xuống từ Internet và sử dụng nó. Với sự trợ giúp của selen, chúng tôi cũng có thể loại bỏ dữ liệu từ các trang web. Ở đây, trong bài viết này, chúng ta sẽ thảo luận về cách loại bỏ nhiều trang bằng Selenium. & NBSP;
Có thể có nhiều cách để xóa dữ liệu từ các trang web, chúng tôi sẽ thảo luận về một trong số chúng. Vòng lặp qua số trang là cách đơn giản nhất để xóa dữ liệu. Chúng ta có thể sử dụng một bộ đếm gia tăng để thay đổi một trang sang trang khác. Như nhiều lần, vòng lặp của chúng tôi sẽ chạy, chương trình sẽ loại bỏ dữ liệu từ các trang web.
URL trang đầu tiên:
//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=1Cuối cùng, các số trang duy nhất sẽ tăng lên như trang = 1, trang = 2 ngay bây giờ, hãy xem URL trang thứ hai.page=1, page=2… Now, Let see for second page URL.
URL trang thứ hai:
//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=2Bây giờ, hãy để thảo luận về cách tiếp cận
Installation:
Bước đầu tiên của chúng tôi, trước khi viết một dòng mã duy nhất. Chúng tôi phải cài đặt selenium để sử dụng lớp WebDriver. Thông qua đó chúng tôi có thể khởi tạo các trình duyệt và nhận trang web từ URL được nhắm mục tiêu.selenium for using webdriver class. Through which we can instantiate the browsers and get the webpage from the targeted URL.
pip install seleniumSau khi selen cài đặt thành công. Bây giờ, chúng tôi có thể đi đến bước tiếp theo để cài đặt gói tiếp theo của chúng tôi. & NBSP;
Gói tiếp theo là WebDriver_Manager, hãy để cài đặt nó trước,webdriver_manager, Let install it first,
pip install webdriver_managerỪ! Chúng tôi đã hoàn thành việc cài đặt các gói quan trọng hoặc cần thiết
Bây giờ, hãy xem việc thực hiện dưới đây:
- Ở đây trong chương trình này, với sự trợ giúp của For Loop, chúng tôi sẽ loại bỏ hai trang web vì chúng tôi chỉ chạy vòng lặp hai lần. Nếu chúng ta muốn loại bỏ nhiều trang hơn, vì vậy, chúng ta có thể tăng số lượng vòng lặp.
- Lưu trữ URL trang trong một chuỗi biến Page_URL và tăng số lượng trang của nó bằng bộ đếm vòng lặp.
- Bây giờ, hãy khởi tạo trình duyệt web Chrome
- Mở URL trang trong trình duyệt Chrome bằng đối tượng trình điều khiển
- Bây giờ, xóa dữ liệu từ trang web bằng phương thức định vị phần tử như find_elements_by_class_name. Phương pháp này sẽ trả về một danh sách các loại yếu tố. & nbsp; Chúng tôi sẽ lưu trữ tất cả các dữ liệu cần thiết bên trong biến danh sách như tiêu đề, giá cả, mô tả và xếp hạng.find_elements_by_class_name method. This method will return a list of types of elements. We will store all necessary data inside the list variable such as title, price, description, and rating.
- Lưu trữ tất cả các dữ liệu như danh sách danh sách của một sản phẩm. Trong Element_list, chúng tôi sẽ lưu trữ danh sách kết quả này.
- Cuối cùng, in phần tử_list. Sau đó đóng đối tượng trình điều khiển.element_list. Then close the driver object.
Python3
from selenium import webdriver
from //webscraper.io/test-sites/e-commerce/static/computers/laptops?page=20import //webscraper.io/test-sites/e-commerce/static/computers/laptops?page=22
//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=23//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=24 //webscraper.io/test-sites/e-commerce/static/computers/laptops?page=25
//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=26 //webscraper.io/test-sites/e-commerce/static/computers/laptops?page=27//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=28 //webscraper.io/test-sites/e-commerce/static/computers/laptops?page=29pip install selenium0pip install selenium1pip install selenium2223pip install selenium221
pip install selenium7pip install selenium8//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=24 pip install webdriver_manager0
pip install selenium7pip install webdriver_manager2
pip install selenium7pip install webdriver_manager4//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=24 pip install webdriver_manager6pip install webdriver_manager7pip install webdriver_manager8
pip install selenium7pip install xlsxwriter0//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=24 pip install webdriver_manager6pip install xlsxwriter3pip install webdriver_manager8
pip install selenium7pip install xlsxwriter6//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=24 pip install webdriver_manager6pip install xlsxwriter9pip install webdriver_manager8
pip install selenium7from2//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=24 pip install webdriver_manager6from5pip install webdriver_manager8
pip install selenium7//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=26 from9//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=28 //webscraper.io/test-sites/e-commerce/static/computers/laptops?page=29pip install selenium0selenium 3selenium 4
selenium 5selenium 6
selenium 7selenium 8
selenium 9
Output:
Lưu trữ dữ liệu trong tệp Excel:
Bây giờ, chúng tôi sẽ lưu trữ dữ liệu từ Element_List đến Excel File bằng gói XLSXWRITER. Vì vậy, trước tiên, chúng tôi phải cài đặt gói xlsxwriter này.element_list to Excel file using xlsxwriter package. So, First, we have to install this xlsxwriter package.
pip install xlsxwriterSau khi cài đặt, hoàn thành. Hãy cùng xem mã đơn giản mà qua đó chúng ta có thể chuyển đổi danh sách các phần tử thành tệp Excel.list of elements into an Excel file.
Python3
import0import1import2
pip install selenium7import4//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=24 import6
pip install selenium7//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=26 import9//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=28 webdriver1webdriver2
selenium 5webdriver4webdriver5webdriver6
Đầu tiên, chúng tôi đang tạo một sổ làm việc có tên result.xlsx. Sau đó, chúng tôi sẽ coi danh sách sản phẩm duy nhất là một hàng duy nhất. Liệt kê danh sách là một hàng và dữ liệu của nó là các cột bên trong tệp excel đang bắt đầu dưới dạng hàng số 0 và cột số 0. & nbsp;result.xlsx. After that, We will consider the list ofa single product as a single row. Enumerate the list as a row and its data as columns inside the Excel file which is starting as a row number 0 and column number 0.
Bây giờ, hãy để Lừa xem việc thực hiện của nó:
Python3
import webdriver8
from selenium import webdriver
from //webscraper.io/test-sites/e-commerce/static/computers/laptops?page=20import //webscraper.io/test-sites/e-commerce/static/computers/laptops?page=22
//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=23//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=24 //webscraper.io/test-sites/e-commerce/static/computers/laptops?page=25
//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=26 //webscraper.io/test-sites/e-commerce/static/computers/laptops?page=27//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=28 //webscraper.io/test-sites/e-commerce/static/computers/laptops?page=29pip install selenium0pip install selenium1pip install selenium2223pip install selenium221
pip install selenium7pip install selenium8//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=24 pip install webdriver_manager0
pip install selenium7pip install webdriver_manager2
pip install selenium7pip install webdriver_manager4//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=24 pip install webdriver_manager6pip install webdriver_manager7pip install webdriver_manager8
pip install selenium7pip install xlsxwriter0//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=24 pip install webdriver_manager6pip install xlsxwriter3pip install webdriver_manager8
pip install selenium7pip install xlsxwriter6//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=24 pip install webdriver_manager6pip install xlsxwriter9pip install webdriver_manager8
pip install selenium7from2//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=24 pip install webdriver_manager6from5pip install webdriver_manager8
pip install selenium7//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=26 from9//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=28 //webscraper.io/test-sites/e-commerce/static/computers/laptops?page=29pip install selenium0selenium 3selenium 4
selenium 5selenium 6
import0import1import2
pip install selenium7import4//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=24 import6
pip install selenium7//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=26 import9//webscraper.io/test-sites/e-commerce/static/computers/laptops?page=28 webdriver1webdriver2
selenium 5webdriver4webdriver5webdriver6
selenium 9
Output:
Đầu tiên, chúng tôi đang tạo một sổ làm việc có tên result.xlsx. Sau đó, chúng tôi sẽ coi danh sách sản phẩm duy nhất là một hàng duy nhất. Liệt kê danh sách là một hàng và dữ liệu của nó là các cột bên trong tệp excel đang bắt đầu dưới dạng hàng số 0 và cột số 0. & nbsp;
Nhấn vào đây để tải xuống tệp đầu ra.