Давайте сделаем парсинг с Selenium!

Сбор данных иногда становится довольно сложной задачей, или, лучше сказать, рудиментарной или скучной. Да, именно мы, специалисты по данным и аналитики данных, были в таких ситуациях. Но как мы можем забыть наш собственный доверенный python, который помогает сделать вещи вполне осуществимыми, не так ли!

Поскольку Python предлагает несколько замечательных библиотек, таких как Selenium, Scrapy и BeautifulSoup, которые помогают в очистке и сборе данных. Эти библиотеки автоматизируют процесс открытия сайтов, собирающих данные и сохраняющих их либо в базах данных, таких как MongoDB, SQLite, либо сохраняющих собранные данные в виде фрейма данных с использованием панд библиотека. В этом мы создадим автоматического бота, который будет очищать веб-сайты.

Но перед этим мы всегда должны просматривать файл robots.txt веб-сайта, который помогает узнать, какой тип информации нам разрешено очищать, а какой нет.

Итак, в этой статье мы собираемся создать проект, который поможет нам извлечь информацию о книгах, их названиях и имени автора из звучащего названия веб-сайта.

Здесь мы будем использовать последнюю версию Selenium – 4.7.2.

Прежде всего, необходимо создать виртуальную среду, перед началом любого проекта на питоне настоятельно рекомендуется создать ее.

# open the folder which you have named as per your requirement in the vscode.
# open the terminal 
python -m virtualenv venv
# this will create a virtual env where all your libraries and their versions
# going to be saved.
# to activate the venv in the cmd follow below steps
cd venv
cd Scripts
activate .

# this will activate the venv just like below image

Теперь, переходя к основной части проекта, установите необходимые библиотеки, такие как selenium, и при установке библиотеки также загрузите драйверы, например, я использую хром, поэтому я установил драйвер хрома. Если вы также использовали Chrome для парсинга веб-сайта, вы можете загрузить драйвер Chrome в соответствии с вашей версией Chrome по ссылке. Во-вторых,драйвер должен быть помещен в ту же папку, что и та, что я показал ниже.

## import all the essential libraries
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import pandas as pd
import time

chr_options = Options()
chr_options.add_experimental_option("detach", True)
chr_options.add_experimental_option('excludeSwitches', ['enable-logging'])

## the above code helps to open up the site for a longer duration which I have
## observed, otherwise if we won't metntion Options() and add_experimental_option()
## the site will open and close instantly

## next step would be the specify the path of the chrome driver that we have placed
## in the folder

driver = webdriver.Chrome(service=Service('C:/Users/projects/selenium_driver/chromedriver.exe'),options = chr_options)

## now with the help of driver we will open up the website in the browser

driver.get("https://www.audible.in/adblbestsellers?ref=a_search_t1_navTop_pl1cg0c1r0&pf_rd_p=45682ad3-a4ca-473f-b9dc-aedd02f1e5fb&pf_rd_r=3BP1A3BP16YAWMFQ9CJ5&pageLoadId=f1oszbm5bPQ5DB2o&creativeId=bbff6a49-335a-4726-a316-1f6a3c7070bb")

## to maximize the window
driver.maximize_window()

"""
## while scraping the data Xpath plays quite an important role while 
## figuring out the way or inspecting the elements.
## to inspect the elements on the website the command Ctrl+Shift+I
## in the website which we are trying to inspect first debug the Javascript
## for that use the command Ctrl+Shift+P then in the pop-up box type Javascript
## click on disable Javascript
## the arrow on the very top left near the Elements will help you to inspect the
## elements and with some basics of Xpath one can figure out the path of the particular elements
## that we want to collect

## in this we will be getting data for all the pages in the audible bestseller collection
## for that we will look at the bottom where the pages tab and icons are given
"""

pagination = driver.find_element(By.XPATH,"//ul[contains(@class,'pagingElements')]")
pages = pagination.find_elements(By.TAG_NAME,"li")

"""
the first pagination will find the elements of the page tab followed by the 
list of the numbers that are there in the tab
after that we will create the integer value of the last page element
*note here we specify the value as -2 because the arrow in the last is considered to be
-1
"""
last_page = int(pages[-2].text)

current_page =1

## create list to store the book_title and author_name

book_title =[]
author_name =[]

"""
Loop will help in collecting the data untill we'll scrape all the pages
"""
while current_page<= last_page:

    time.sleep(2)
## the container will contain the list values of the books that have been saved 
## which includes all the title, authorname, rating etc. basically the productlist
    container = driver.find_element(By.CLASS_NAME,'adbl-impression-container')

    #//div[contains(@class,'adbl-impression-container')]//div//ul//li[contains(@class,'productListItem')]

    products = container.find_elements(By.XPATH,'.//li[contains(@class, "productListItem")]')
## prodcuts will contain all the productlist we have use XPATH here and in each product 
## all the book titles, author names, reviews will be available which we can find via Xpath



    for product in products:
        # print(product.text)
        book_title.append((product.find_element(By.XPATH,'.//h3//a').text))
        author_name.append(product.find_element(By.XPATH,'.//li[contains(@class,"authorLabel")]//span//a').text)
    
    current_page+=1
## we will store the data in the list and after that we will increase the count
## of the new page and it will click on the next page using the click() function

    try:
        next_page = driver.find_element(By.XPATH,"//span[contains(@class,'nextButton')]")
        
        next_page.click()
    except:
        pass

audible_data = pd.DataFrame({'book_title':book_title,
                'author_name':author_name})
audible_data.to_csv("pagination_data.csv",index=False)

## after this we will store the data in the dataframe format

"""
if one wants to look for a separate loop to get information about the title or 
author name one can use the below code. If not this can be commented out as well
"""
name_of_book = driver.find_elements(By.XPATH,'//h3//a')
for name in name_of_book:
    print(name.text)

author_name = driver.find_elements(By.XPATH,'//li[contains(@class,"authorLabel")]//span//a')


for a_name in author_name:
    print(a_name.text)

"""
lastly it is necessary to close the open window as well once we are done with
scraping and collecting the data
"""
driver.quit()

Надеюсь, вам понравился учебник по парсингу с помощью селена. Если вам это нравится, следите за мной, чтобы узнать больше таких статей, которые я публикую еженедельно.