Docsity

Exam Build a Scraper, Prove d'esame di Marketing

Toggle sidebar
bg1
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import pandas as pd
import time
#Set ChromeDriver path
service = Service(chromedriver_path)
#Start WebDriver
driver = webdriver.Chrome(service=service)
#Open page
url = "https://en.wikipedia.org/wiki/List_of_Marvel_Cinematic_Universe_films"
driver.get(url)
time.sleep(5)
#Find the box office table
tables = driver.find_elements(By.TAG_NAME, "table")
box_office_table = tables[10]
#Get table rows
rows = box_office_table.find_elements(By.TAG_NAME, "tr")
#Extract data into a list
data = []
for row in rows[1:]:
cells = row.find_elements(By.TAG_NAME, "td")
if len(cells) < 8:
print(f"Skipping row due to missing columns: {len(cells)} found")
print(cells)
continue
movie = cells[0].text.strip()
release_date = cells[1].text.strip()
domestic_box = cells[2].text.strip()
international_box = cells[3].text.strip()
worldwide_box = cells[4].text.strip()
rank_us = cells[5].text.strip()
rank_worldwide = cells[6].text.strip()
budget = cells[7].text.strip()
data.append([movie, release_date, domestic_box, international_box,
worldwide_box, rank_us, rank_worldwide, budget])
#Close
driver.quit()
#Save to Pandas DataFrame
columns = ["Movie", "Release Date", "Domestic Box Office", "International Box
Office",
"Worldwide Box Office", "US Rank", "Worldwide Rank", "Budget"]
df = pd.DataFrame(data, columns=columns)
pf2

Anteprima parziale del testo

Scarica Exam Build a Scraper e più Prove d'esame in PDF di Marketing solo su Docsity!

from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By import pandas as pd import time #Set ChromeDriver path service = Service(chromedriver_path) #Start WebDriver driver = webdriver.Chrome(service=service) #Open page url = "https://en.wikipedia.org/wiki/List_of_Marvel_Cinematic_Universe_films" driver.get(url) time.sleep(5) #Find the box office table tables = driver.find_elements(By.TAG_NAME, "table") box_office_table = tables[10] #Get table rows rows = box_office_table.find_elements(By.TAG_NAME, "tr") #Extract data into a list data = [] for row in rows[1:]: cells = row.find_elements(By.TAG_NAME, "td") if len(cells) < 8: print(f"Skipping row due to missing columns: {len(cells)} found") print(cells) continue movie = cells[0].text.strip() release_date = cells[1].text.strip() domestic_box = cells[2].text.strip() international_box = cells[3].text.strip() worldwide_box = cells[4].text.strip() rank_us = cells[5].text.strip() rank_worldwide = cells[6].text.strip() budget = cells[7].text.strip() data.append([movie, release_date, domestic_box, international_box, worldwide_box, rank_us, rank_worldwide, budget]) #Close driver.quit() #Save to Pandas DataFrame columns = ["Movie", "Release Date", "Domestic Box Office", "International Box Office", "Worldwide Box Office", "US Rank", "Worldwide Rank", "Budget"] df = pd.DataFrame(data, columns=columns)

#Save as a CSV df.to_csv("marvel_movies.csv", index=False,encoding="utf-8-sig") print("Data saved as marvel_movies.csv")