def extractallcss(self, url): # Fetch the webpage content try: response =…
- User
def extract_all_css(self, url): # Fetch the webpage content try: response = requests.get(url, timeout=10) response.raise_for_status() except requests.RequestException as e: print(f"Failed to fetch the webpage: {e}") return [], "", "" soup = BeautifulSoup(response.text, 'html.parser') # Extract inline styles inline_styles = [tag.get('style', '') for tag in soup.find_all(attrs={'style': True})] # Extract embedded styles embedded_styles = ' '.join([style.string for style in soup.find_all('style') if style.string]) # Extract external stylesheet URLs stylesheet_urls = [link['href'] for link in soup.find_all('link', rel='stylesheet') if 'href' in link.attrs] # Fetch and concatenate external stylesheets external_styles = '' for stylesheet_url in stylesheet_urls: absolute_url = urljoin(url, stylesheet_url) try: resp = requests.get(absolute_url, timeout=10) resp.raise_for_status() external_styles += resp.text + '\n' except requests.RequestException: print(f"Failed to fetch stylesheet: {absolute_url}") return soup, inline_styles, embedded_styles, external_styles \n def extract_css_variables(self, stylesheets): variables = {} for stylesheet in stylesheets: parsed_stylesheet = tinycss2.parse_stylesheet(stylesheet, skip_comments=True, skip_whitespace=True) for rule in parsed_stylesheet: if rule.type == 'qualified-rule': declarations = tinycss2.parse_declaration_list(rule.content, skip_comments=True, skip_whitespace=True) for decl in declarations: if decl.type == 'declaration' and decl.name.startswith('--'): var_name = decl.name var_value = tinycss2.serialize(decl.value).strip() variables[var_name] = var_value return variables def resolve_css_variable(self, value, variables): var_pattern = re.compile(r'var((--[\w-]+)(?:,\s*([^)]+))?)') while True: match = var_pattern.search(value) if not match: break var_name, fallback = match.groups() var_value = variables.get(var_name, fallback or '') value = value[:match.start()] + var_value + value[match.end():] return value def extract_color_from_value(self, value): color_pattern = re.compile(r'(#[0-9a-fA-F]{3,8}|rgba?([^)]+)|hsla?([^)]+)|\b[a-zA-Z]+\b)') match = color_pattern.search(value) return match.group(1) if match else None \n def extract_relevant_css(self, inline_styles, embedded_styles, external_styles): relevant_properties = { 'background': [ r'^background(?:-color)?', r'^background', r'^.btn', r'^\.button', r'^.cta', r'^\.primary', r'^.secondary', r'^\.action', r'^.submit', r'^\.link-button', r'^.custom-button', r'^input[[^]]type=["']?(button|submit)["']?]', ], 'text': [ r'^color', ], 'font': [ r'^font(?:-family|-size|-weight|-style)?', r'^text-transform', r'^letter-spacing', r'^html', ], 'other': [ r'^box-shadow', r'^border(?:-color)?', ]} pattern = '|'.join([f'({item})' for sublist in relevant_properties.values() for item in sublist]) pattern = re.compile(pattern, re.IGNORECASE) relevant_css = [] all_stylesheets = [embedded_styles, external_styles] variables = self.extract_css_variables(all_stylesheets) def get_relevant_declarations(declarations, variables, is_button=False): relevant_styles = [] for decl in declarations: if decl.type == 'declaration': property_name = decl.lower_name property_value = tinycss2.serialize(decl.value).strip() property_value = self.resolve_css_variable(property_value, variables) if pattern.match(property_name) or (is_button and property_name in ['color', 'border-color']): relevant_styles.append(f"{property_name}: {property_value};") if property_name == 'background': color = self.extract_color_from_value(property_value) if color: relevant_styles.append(f"background-color: {color};") return relevant_styles combined_styles = embedded_styles + '\n' + external_styles stylesheet = tinycss2.parse_stylesheet(combined_styles, skip_comments=True, skip_whitespace=True) for rule in stylesheet: if rule.type == 'qualified-rule': selector = tinycss2.serialize(rule.prelude).strip() declarations = tinycss2.parse_declaration_list(rule.content, skip_comments=True, skip_whitespace=True) is_button = any(re.match(btn_pattern, selector) for btn_pattern in relevant_properties['button']) relevant_styles = get_relevant_declarations(declarations, variables, is_button) if relevant_styles: relevant_css.append(f"{selector} {{\n " + "\n ".join(relevant_styles) + "\n}}") for i, style_text in enumerate(inline_styles): if not style_text.strip(): continue declarations = tinycss2.parse_declaration_list(style_text, skip_comments=True, skip_whitespace=True) relevant_styles = get_relevant_declarations(declarations, variables, True) # Treat all inline styles as potentially button-related if relevant_styles: relevant_css.append(f"/ inline style {i+1} */ {{\n " + "\n ".join(relevant_styles) + "\n}}") return "\n\n".join(relevant_css) Hãy cải tiến các hàm này để lấy được nhiều thông tin button color hơn để đạt kết quả chính xác! Hiện tại button color đang bị sai.
- Assistant
To improve the accuracy of extracting button colors from CSS, we can enhance several aspects of your current implementation:
-
Expand Button Selectors: Ensure that you cover a broader range of button selectors, including pseudo-classes (e.g.,
:hover,:active) and more complex selectors. -
Improve Color Extraction: Enhance the
extract_color_from_valuefunction to handle more color formats and multiple color properties within a single value. -
Handle Inheritance and Specificity: Ensure that styles are correctly applied based on CSS specificity and inheritance rules.
-
Process Button States: Extract colors not just for the default state but also for various button states like
:hover,:active, etc.
Below is the improved version of your functions with these enhancements:
import requests from bs4 import BeautifulSoup from urllib.parse import urljoin import tinycss2 import re class CSSExtractor: def extract_all_css(self, url): # Fetch the webpage content try: response = requests.get(url, timeout=10) response.raise_for_status() except requests.RequestException as e: print(f"Failed to fetch the webpage: {e}") return [], "", "", "" soup = BeautifulSoup(response.text, 'html.parser') # Extract inline styles inline_styles = [tag.get('style', '') for tag in soup.find_all(attrs={'style': True})] # Extract embedded styles embedded_styles = ' '.join([style.string for style in soup.find_all('style') if style.string]) # Extract external stylesheet URLs stylesheet_urls = [link['href'] for link in soup.find_all('link', rel='stylesheet') if 'href' in link.attrs] # Fetch and concatenate external stylesheets external_styles = '' for stylesheet_url in stylesheet_urls: absolute_url = urljoin(url, stylesheet_url) try: resp = requests.get(absolute_url, timeout=10) resp.raise_for_status() external_styles += resp.text + '\n' except requests.RequestException: print(f"Failed to fetch stylesheet: {absolute_url}") return soup, inline_styles, embedded_styles, external_styles def extract_css_variables(self, stylesheets): variables = {} for stylesheet in stylesheets: parsed_stylesheet = tinycss2.parse_stylesheet(stylesheet, skip_comments=True, skip_whitespace=True) for rule in parsed_stylesheet: if rule.type == 'qualified-rule': declarations = tinycss2.parse_declaration_list(rule.content, skip_comments=True, skip_whitespace=True) for decl in declarations: if decl.type == 'declaration' and decl.name.startswith('--'): var_name = decl.name var_value = tinycss2.serialize(decl.value).strip() variables[var_name] = var_value return variables def resolve_css_variable(self, value, variables): var_pattern = re.compile(r'var\((--[\w-]+)(?:,\s*([^)]+))?\)') while True: match = var_pattern.search(value) if not match: break var_name, fallback = match.groups() var_value = variables.get(var_name, fallback or '') value = value[:match.start()] + var_value + value[match.end():] return value def extract_color_from_value(self, value): color_patterns = [ r'(#[0-9a-fA-F]{3,8})', r'(rgba?\([^)]+\))', r'(hsla?\([^)]+\))', r'\b(aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|purple|red|silver|teal|white|yellow)\b' ] color_pattern = re.compile('|'.join(color_patterns), re.IGNORECASE) matches = color_pattern.findall(value) # Flatten the list of tuples and filter out empty strings colors = [match for group in matches for match in group if match] return colors if colors else None def extract_relevant_css(self, inline_styles, embedded_styles, external_styles): relevant_properties = { 'background': [ r'^background(?:-color)?$', r'^background-image$', r'^background$', ], 'button': [ r'^button$', # <button> elements r'^\.btn$', # .btn class r'^\.button$', r'^\.cta$', r'^\.primary$', r'^\.secondary$', r'^\.action$', r'^\.submit$', r'^\.link-button$', r'^\.custom-button$', r'^a\.[\w-]*button[\w-]*$', # <a class="...button..."> r'^input\[[^\]]*type=["\']?(button|submit)["\']?\]$', # <input type="button" ...> r'^button\.[\w-]*$', # Add more button-related selectors as needed ], 'text': [ r'^color$', r'^opacity$', ], 'font': [ r'^font(?:-family|-size|-weight|-style)?$', r'^text-decoration$', r'^text-transform$', r'^line-height$', r'^letter-spacing$', ], 'body': [ r'^body$', r'^html$', ], 'variables': [ r'^--.*?(?:color|font|bg|background|button|text)$', ], 'other': [ r'^box-shadow$', r'^(?:linear|radial)-gradient$', r'^border(?:-color)?$', r'^outline(?:-color)?$', ] } # Compile patterns for performance compiled_relevant_properties = { key: [re.compile(pattern, re.IGNORECASE) for pattern in patterns] for key, patterns in relevant_properties.items() } relevant_css = [] all_stylesheets = [embedded_styles, external_styles] variables = self.extract_css_variables(all_stylesheets) def get_relevant_declarations(declarations, variables, is_button=False): relevant_styles = [] for decl in declarations: if decl.type == 'declaration': property_name = decl.lower_name property_value = tinycss2.serialize(decl.value).strip() property_value = self.resolve_css_variable(property_value, variables) # Check if the property matches any relevant pattern is_relevant = any(pattern.match(property_name) for pattern in compiled_relevant_properties['background'] + compiled_relevant_properties['button'] + compiled_relevant_properties['text'] + compiled_relevant_properties['font'] + compiled_relevant_properties['body'] + compiled_relevant_properties['variables'] + compiled_relevant_properties['other']) if not is_relevant and is_button: # Additionally consider color and border-color for buttons if property_name in ['color', 'border-color', 'background-color']: is_relevant = True if is_relevant: relevant_styles.append(f"{property_name}: {property_value};") # Special handling for background-color extraction if property_name.startswith('background'): colors = self.extract_color_from_value(property_value) if colors: for color in colors: relevant_styles.append(f"background-color: {color};") return relevant_styles combined_styles = embedded_styles + '\n' + external_styles stylesheet = tinycss2.parse_stylesheet(combined_styles, skip_comments=True, skip_whitespace=True) for rule in stylesheet: if rule.type == 'qualified-rule': selector = tinycss2.serialize(rule.prelude).strip() declarations = tinycss2.parse_declaration_list(rule.content, skip_comments=True, skip_whitespace=True) # Determine if the selector targets a button is_button = any(re.match(btn_pattern, selector) for btn_pattern in compiled_relevant_properties['button']) # Also check for button pseudo-classes if not is_button: is_button = any(re.match(btn_pattern + r':(hover|active|focus|disabled)', selector) for btn_pattern in compiled_relevant_properties['button']) relevant_styles = get_relevant_declarations(declarations, variables, is_button) if relevant_styles: relevant_css.append(f"{selector} {{\n " + "\n ".join(relevant_styles) + "\n}}") # Process inline styles for i, style_text in enumerate(inline_styles): if not style_text.strip(): continue declarations = tinycss2.parse_declaration_list(style_text, skip_comments=True, skip_whitespace=True) relevant_styles = get_relevant_declarations(declarations, variables, True) # Treat all inline styles as potentially button-related if relevant_styles: relevant_css.append(f"/* inline style {i+1} */ {{\n " + "\n ".join(relevant_styles) + "\n}}") return "\n\n".join(relevant_css)Detailed Improvements
-
Expanded Button Selectors:
- Pseudo-classes: Added handling for pseudo-classes like
:hover,:active,:focus, and:disabledto capture different button states. - Additional Selectors: Included more comprehensive button selectors and patterns to cover various naming conventions.
- Pseudo-classes: Added handling for pseudo-classes like
-
Enhanced
extract_color_from_value:- Multiple Colors: Modified the function to return all color matches within a property value, not just the first one.
- Expanded Color Patterns: Included more color names and ensured that all common color formats are captured.
-
Improved Relevance Checking:
- Compiled Patterns: Precompiled regex patterns for better performance and cleaner matching.
- Flexible Matching: Adjusted the relevance logic to consider a broader range of properties and scenarios where button colors might be set.
-
Handling Inline Styles as Buttons:
- Assumption: Treated all inline styles as potentially button-related for more thorough extraction, ensuring that any inline style affecting a button is captured.
-
Comments and Readability:
- Inline Comments: Added comments to explain each step, making the code easier to understand and maintain.
- Structured Code: Organized the code into clear sections for fetching CSS, extracting variables, resolving variables, and extracting relevant styles.
Additional Recommendations
-
Specificity Handling: To further enhance accuracy, consider implementing CSS specificity rules to determine which styles take precedence when multiple rules target the same element.
-
JavaScript-Injected Styles: If buttons are styled dynamically via JavaScript, you might need to execute JavaScript (e.g., using Selenium or Puppeteer) to capture those styles.
-
Caching Stylesheets: To improve performance, especially when processing multiple pages or large stylesheets, implement caching mechanisms for external stylesheets.
-
Error Handling: Enhance error handling to log or manage different types of exceptions that might occur during CSS parsing.
By implementing these improvements, your CSS extraction functions should more accurately identify and extract button colors, accounting for a wider range of scenarios and CSS complexities.
-