furas

Python - Selenium - sappi.com (Stackoverflow)

Aug 26th, 2025 (edited)
322
0
Never
7
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. # date: 2025.08.26
  2.  
  3. # [pandas - Python 3.9, get in MS excel ALL physical addresses from the URL -> https://www.sappi.com/en-gb/about-us/locations - Stack Overflow](https://stackoverflow.com/questions/79746723/python-3-9-get-in-ms-excel-all-physical-addresses-from-the-url-https-www-s)
  4.  
  5. # https://www.sappi.com/en-gb/about-us/locations
  6.  
  7. from selenium import webdriver
  8. #import time
  9.  
  10. # ---
  11.  
  12. import selenium
  13. print('Selenium:', selenium.__version__)
  14.  
  15. # ---
  16.  
  17. url = "https://www.sappi.com/en-gb/about-us/locations"
  18.  
  19. #driver = webdriver.Chrome()
  20. driver = webdriver.Firefox()
  21.  
  22. driver.get(url)
  23.  
  24. #time.sleep(5)
  25.  
  26. html = driver.page_source
  27.  
  28. driver.close()
  29.  
  30. # ----
  31.  
  32. from bs4 import BeautifulSoup
  33. import pandas as pd
  34.  
  35. soup = BeautifulSoup(html, 'html.parser')
  36.  
  37. location_cards = soup.find_all('article', class_='node-type__location')
  38.  
  39. if len(location_cards) == 0:
  40.     print("Can't find data !!!")
  41. else:
  42.     data = []
  43.     for card in location_cards:
  44.  
  45.         item = card.find('h3')
  46.         name = item.get_text(strip=True, separator='|') if item else ""
  47.  
  48.         item = card.find('li', class_='location-address')
  49.         address = item.get_text(strip=True, separator='|') if item else ""
  50.  
  51.         # there is no `location-country`
  52.         #item = card.find('li', class_='location-country')
  53.         item = card.find('span', class_='address-country')
  54.         country = item.get_text(strip=True, separator='|') if item else ""
  55.  
  56.         data.append({
  57.             'Name': name,
  58.             'Address': address,
  59.             'Country': country
  60.         })
  61.  
  62.     # Convert to DataFrame and export to Excel
  63.     df = pd.DataFrame(data)
  64.     df.to_excel('sappi_locations.xlsx', index=False)
  65.     #df.to_csv('sappi_locations.csv', index=False)
  66.  
  67.     print("Data exported to sappi_locations.xlsx")
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment