Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scraping hex codes
#1
Question 
Hi guys,

I've got a python script which reads product urls from csv which have pre selected variants the pages.

The script visits the pages and should just be scraping only the selected variant hex code but its scraping all of them

Can anyone identify why?

Here is the element with the selected variant hex code
[Image: V71gLuv]

Here is my script

import requests
from bs4 import BeautifulSoup
import csv

def get_hex_color(url):
    response = requests.get(url)
    if response.status_code == 200:
        soup = BeautifulSoup(response.content, 'html.parser')
        color_elements = soup.find_all(class_='athenaProductVariations_colorSwatchInner')
        hex_colors = [element.get('style').split(':')[-1].strip() for element in color_elements]
        return hex_colors
    else:
        print(f"Failed to fetch data from {url}")
        return []

def process_urls(input_file, output_file):
    with open(input_file, 'r') as csvfile:
        reader = csv.DictReader(csvfile)
        rows = list(reader)

        for row in rows:
            url = row['URL']
            hex_colors = get_hex_color(url)
            row['Variant Metafield: custom.color [color]'] = ', '.join(hex_colors)

    with open(output_file, 'w', newline='') as outfile:
        fieldnames = rows[0].keys() if rows else []
        writer = csv.DictWriter(outfile, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(rows)

# Replace 'input.csv' and 'output.csv' with your file names
input_file = 'input.csv'
output_file = 'output.csv'

process_urls(input_file, output_file)
Thanks in advance!
Reply


Messages In This Thread
Scraping hex codes - by sophia_adams - Nov-30-2023, 04:12 PM
RE: Scraping hex codes - by snippsat - Nov-30-2023, 07:31 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020