Как очистить 2-й тег <div>того же класса без уникального отличительного знака - PullRequest
1 голос
/ 13 июля 2020

Я пытаюсь прочитать содержимое второго класса div из кода: div class = "eds-event-card-content__sub eds-text-bm eds-text-color - ui-600 eds-l-mar -top-1 eds-event-card-content__sub - cropped "> Начинается с 15,75 ринггитов с использованием python 3

<div class="eds-event-card-content__sub-content">

  <div class="eds-event-card-content__sub eds-text-bm eds-text-color--ui-600 eds-l-mar-top-1 
  eds-event-card-content__sub--cropped">
        <div class="card-text--truncated__one">Found8 KL Sentral • Kuala Lumpur, Kuala 
        Lumpur</div>
  </div>
  <div class="eds-event-card-content__sub eds-text-bm eds-text-color--ui-600 eds-l-mar-top-1 
  eds-event-card-content__sub--cropped">Starts at RM15.75</div></div>

My python кода:

url = 'https://www.eventbrite.com/d/malaysia--kuala-lumpur--85675181/all-events/?page=2'
response = get(url)

html_soup = BeautifulSoup(response.text, 'html.parser')

# Select all the 20 event containers from a single page
event_containers = html_soup.find_all('div', class_='search-event-card-square-image')

# Getting price of ticket
price = container.find_all('div', class_= "eds-event-card-content__sub eds-text-bm eds-text-color--ui-600 eds-l-mar-top-1 eds-event-card-content__sub--cropped").text

print("price: ", price[1])

Однако мой код не работает, он дает мне результат:

IndexError: list index out of range

, но я хотел Starts at RM15.75

Кто-нибудь может мне с этим помочь? Спасибо

1 Ответ

0 голосов
/ 13 июля 2020

Я не вижу цены в html Исходном коде. Я предполагаю, что они генерируются с использованием сценария js.

Итак, для этого случая вам нужно использовать Selenium.

Код:

# import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

import time
from webdriver_manager.chrome import ChromeDriverManager

chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=chrome_options)
driver.set_window_size(1024, 600)
driver.maximize_window()

url = 'https://www.eventbrite.com/d/malaysia--kuala-lumpur--85675181/all-events/?page=2'
# response = requests.get(url)
driver.get(url)
time.sleep(4)
html_soupdf  = BeautifulSoup(driver.page_source, 'html.parser')

# Select all the 20 event containers from a single page
event_containers = html_soup.find('ul', class_='search-main-content__events-list')

for event in event_containers.find_all('li'):
    event_time = event.find('div', class_= "eds-text-color--primary-brand eds-l-pad-bot-1 eds-text-weight--heavy eds-text-bs").text
    event_name = event.find('div', class_= "eds-event-card__formatted-name--is-clamped eds-event-card__formatted-name--is-clamped-three eds-text-weight--heavy").text
    event_price_place = event.find('div', class_ = "eds-event-card-content__sub-content")
    event_pp = event_price_place.find_all('div')
    event_place = event_pp[0].text
    try:
        event_price = event_pp[2].text
    except:
        event_price = None
    print(f"{event_name}\n{event_time}\n{event_place}\n{event_price}\n\n")

Результат:

KL International Flea Market 2020 / Bazaar Antarabangsa Kuala Lumpur
Mon, Oct 5, 10:00 AM
VIVA Shopping Mall • Kuala Lumpur, Wilayah Persekutuan Kuala Lumpur
Free


FGTSD Physical Church Service
Sun, Jul 19, 9:30 AM + 105 more events
Full Gospel Tabernacle Sri Damansara • Kuala Lumpur
Free


EFE 2020 - 16th Export Furniture Exhibition Malaysia
Thu, Aug 27, 9:00 AM
Kuala Lumpur Convention Centre • Kuala Lumpur, Kuala Lumpur
Free


International Beauty Expo (IBE) 2020
Sat, Sep 12, 11:00 AM
Malaysia International Trade and Exhibition Centre • Kuala Lumpur, Wilayah Persekutuan Kuala Lumpur
Free


Learn How To Earn USD3500 In 4 Week Using Your SmartPhone
Today at 8:00 PM + 2 more events
KL Online Event • Kuala Lumpur, Bangkok
None


Turn Customers into Raving Fans of Your Brand via Equity Crowdfunding
Thu, Aug 27, 4:00 PM
Found8 KL Sentral • Kuala Lumpur, Kuala Lumpur
Starts at RM15.75

.
.
.
.
.

Редактировать:

Я добавил возможность сделать его без заголовка.

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=chrome_options)
...