首页 > 解决方案 > 使用 beautifulsoup 从 Eventbrite 抓取数据

问题描述

链接到我要抓取的页面

我正在探索和理解python中的beautiful-soup,所以我决定尝试网络抓取Eventbrite的事件数据。我想知道为什么我的刮板功能没有接受页面中列出的任何事件。由于某种原因,数据框为空。是不是因为我叫错了班级?我知道该网站有一个 API,但我想在使用 API 之前先尝试网页抓取。

到目前为止,这是我的代码

import requests
from bs4 import BeautifulSoup
import pandas as pd
event = []
location = []
price = []
date = []

eventbrite_url = "https://www.eventbrite.com/d/ca--san-diego/art-events/"  
try:
    page = requests.get(kpbs_url)

    soup = BeautifulSoup(page.text, 'html.parser')

    items = soup.find_all("li", {"class": "item"})
    for item in items:
        event.append(item.find('div', {"class": "eds-is-hidden-accessible"}).text.strip())
        location.append(item.find('div', {"class": "card-text--truncated__one"}).text.strip())
        date.append(item.find('div', {"class":"eds-text-color--primary-brand eds-l-pad-bot-1 eds-text-weight--heavy eds-text-bs"}).text.strip())
        try:
            price.append(item.find('div', {"class": "eds-media-card-content__sub eds-text-bm eds-text-color--grey-600 eds-1-mar-top-1 eds-media-card-content__sub--cropped"}).text.strip())
        except:
            price.append('Free')

    final_df = pd.DataFrame(
    {'Event': event,
     'Location': location,
     'Price': price,
     'Date':date
    })
except Exception as e:
    print(e)
    print("continuing....")

标签: pythonweb-scrapingbeautifulsoup

解决方案


推荐阅读