import requests from bs4 import BeautifulSoup import logging import tinycss2…
- User
import requests from bs4 import BeautifulSoup import logging import tinycss2
logging.basicConfig(level=logging.INFO) logger = logging.getLogger(name)
def extract_css(url): try: # Download webpage content response = requests.get(url) response.raise_for_status() soup = BeautifulSoup(response.text, 'html.parser')
# Collect all CSS content all_css = "" # Inline CSS for style in soup.find_all('style'): all_css += style.string or "" # External CSS files for link in soup.find_all('link', rel='stylesheet'): css_url = link.get('href') if css_url: if not css_url.startswith(('http:', 'https:')): css_url = requests.compat.urljoin(url, css_url) try: css_response = requests.get(css_url) css_response.raise_for_status() all_css += css_response.text except requests.RequestException as e: logger.warning(f"Cannot load CSS from {css_url}: {e}") return extract_relevant_css(all_css) except Exception as e: logger.error(f"Error extracting CSS: {e}") return ""def extract_relevant_css(css_content): relevant_css = "" parsed_css = tinycss2.parse_stylesheet(css_content, skip_comments=True, skip_whitespace=True)
# Define selectors and properties of interest selectors_of_interest = { 'background_color': ['body', 'html'], 'button_color': ['button', '.button', '.btn', '[type="button"]', 'input[type="submit"]'], 'text_color': ['body', 'html', 'p'], 'font_style': ['body', 'html'] } properties_of_interest = { 'background_color': ['background-color', 'background'], 'button_color': ['background-color', 'background', 'color', 'border-color'], 'text_color': ['color'], 'font_style': ['font-family', 'font-style', 'font-weight', 'font-size'] } extracted_properties = { 'background_color': [], 'button_color': [], 'text_color': [], 'font_style': [] } for rule in parsed_css: if rule.type != 'qualified-rule': continue # Get the selector selector = tinycss2.serialize(rule.prelude).strip() declarations = tinycss2.parse_declaration_list(rule.content) for prop in extracted_properties.keys(): if any(sel in selector for sel in selectors_of_interest[prop]): for declaration in declarations: if declaration.type != 'declaration': continue name = declaration.name.lower() value = tinycss2.serialize(declaration.value).strip() if name in properties_of_interest[prop]: extracted_properties[prop].append({ 'selector': selector, 'property': name, 'value': value }) # Build the relevant CSS string for prop, styles in extracted_properties.items(): if styles: relevant_css += f"/* {prop.replace('_', ' ').title()} */\n" for style in styles: relevant_css += f"{style['selector']} {{ {style['property']}: {style['value']}; }}\n" relevant_css += "\n" return relevant_cssUse the function
url = "https://www.vsatech.net/" # Replace with the actual URL result = extract_css(url) print(result) \n Cho đoạn code trích xuất các dòng css code từ web url. Mục đích là lấy các dòng css code liên quan đến các thuộc tính sau: background_color: str = Field(description="the primary background color used on the website. Must be a valid hex color code and not transparent.") button_color: str = Field(description="The primary color used for buttons on the website. Must be a valid hex color code, not transparent.") text_color: str = Field(description="the primary text color used on the website. Must be a valid hex color code and not transparent.") font_style: str = Field(description="the main font style used on the website.") \n Để sau đó LLMs sẽ dựa vào đó để phân tích, trích xuất ra các properties trên. Làm thế nào lấy không thừa (thừa thì tốn prompt dẫn đến tăng cost), cũng không thiếu (thiếu thì không đủ thông tin, phân tích sai). Hiện tại button color dùng LLMs trích xuất từ các css code trên không chính xác, hãy cải tiến giúp tôi. Có thể những thông tin liên quan đến button color đang bị thiếu. Bạn có thể cân nhắc xem thông tin về CSS variable cũng như body trong CSS code.
- Assistant
To improve the accuracy of extracting CSS properties—especially for
button_color—you need to ensure that all relevant styles, including CSS variables, are captured. CSS variables (--variable-name) are often used to define colors and other styles, and failing to include their definitions can lead to incomplete or inaccurate extraction.Here’s how you can enhance your existing code:
-
Extract CSS Variable Definitions: Collect all CSS variable declarations from the stylesheet. Typically, these are defined under the
:rootselector but can be present in other selectors as well. -
Include Variable Definitions in Relevant CSS: Ensure that these variables are included in the
relevant_cssso that the LLM can resolve them when analyzing the styles. -
Enhance Selector Matching: Expand the selectors of interest for better coverage. For instance, some websites might use more specific classes for buttons like
.cta-button,.primary-btn, etc. You can make the selector matching more flexible or include additional common button classes.
Below is the revised code with these improvements:
import requests from bs4 import BeautifulSoup import logging import tinycss2 logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def extract_css(url): try: # Download webpage content response = requests.get(url) response.raise_for_status() soup = BeautifulSoup(response.text, 'html.parser') # Collect all CSS content all_css = "" # Inline CSS for style in soup.find_all('style'): all_css += style.string or "" # External CSS files for link in soup.find_all('link', rel='stylesheet'): css_url = link.get('href') if css_url: if not css_url.startswith(('http:', 'https:')): css_url = requests.compat.urljoin(url, css_url) try: css_response = requests.get(css_url) css_response.raise_for_status() all_css += css_response.text except requests.RequestException as e: logger.warning(f"Cannot load CSS from {css_url}: {e}") return extract_relevant_css(all_css) except Exception as e: logger.error(f"Error extracting CSS: {e}") return "" def extract_relevant_css(css_content): relevant_css = "" parsed_css = tinycss2.parse_stylesheet(css_content, skip_comments=True, skip_whitespace=True) # Define selectors and properties of interest selectors_of_interest = { 'background_color': ['body', 'html'], 'button_color': ['button', '.button', '.btn', '[type="button"]', 'input[type="submit"]'], 'text_color': ['body', 'html', 'p'], 'font_style': ['body', 'html'] } properties_of_interest = { 'background_color': ['background-color', 'background'], 'button_color': ['background-color', 'background', 'color', 'border-color'], 'text_color': ['color'], 'font_style': ['font-family', 'font-style', 'font-weight', 'font-size'] } # Initialize extracted properties extracted_properties = { 'background_color': [], 'button_color': [], 'text_color': [], 'font_style': [] } # Initialize CSS variables css_variables = "" for rule in parsed_css: if rule.type != 'qualified-rule': continue # Serialize selector selector = tinycss2.serialize(rule.prelude).strip() declarations = tinycss2.parse_declaration_list(rule.content) # Check for CSS variable declarations for declaration in declarations: if declaration.type != 'declaration': continue name = declaration.name.lower() if name.startswith('--'): # CSS Variable value = tinycss2.serialize(declaration.value).strip() css_variables += f"{selector} {{ {name}: {value}; }}\n" # Extract properties of interest for prop in extracted_properties.keys(): if any(sel == selector or sel in selector.split(',') for sel in selectors_of_interest[prop]): for declaration in declarations: if declaration.type != 'declaration': continue name = declaration.name.lower() value = tinycss2.serialize(declaration.value).strip() if name in properties_of_interest[prop]: extracted_properties[prop].append({ 'selector': selector, 'property': name, 'value': value }) # Include CSS variables at the top to ensure they are available for resolution if css_variables: relevant_css += "/* CSS Variables */\n" relevant_css += css_variables relevant_css += "\n" # Build the relevant CSS string for prop, styles in extracted_properties.items(): if styles: relevant_css += f"/* {prop.replace('_', ' ').title()} */\n" for style in styles: relevant_css += f"{style['selector']} {{ {style['property']}: {style['value']}; }}\n" relevant_css += "\n" return relevant_css # Use the function url = "https://www.vsatech.net/" # Replace with the actual URL result = extract_css(url) print(result)Key Improvements Explained
-
CSS Variables Extraction:
- Identification: The code now checks if a declaration's name starts with
--, indicating a CSS variable. - Collection: All CSS variable declarations are collected and stored in the
css_variablesstring. - Inclusion: These variable definitions are prepended to the
relevant_cssto ensure they are available for resolving anyvar(--variable-name)usages in the styles of interest.
- Identification: The code now checks if a declaration's name starts with
-
Enhanced Selector Matching:
- Exact Matching: Changed
if any(sel in selector for sel in selectors_of_interest[prop])toif any(sel == selector or sel in selector.split(',') for sel in selectors_of_interest[prop])to ensure more precise matching, especially when multiple selectors are combined using commas. - Flexibility: You can further enhance the selectors by including more common button classes or using regex matching if needed.
- Exact Matching: Changed
-
Comments for Clarity:
- Added comments like
/* CSS Variables */and/* Property Name */to clearly separate different sections of the extracted CSS. This aids the LLM in understanding the context when processing the CSS.
- Added comments like
-
Robustness:
- The extraction process now gracefully handles multiple selectors separated by commas.
- Ensures that even if a selector has multiple parts, as long as one part matches the selectors of interest, it will be included.
Additional Recommendations
-
Handle
@importRules: Some stylesheets use@importto include other CSS files. You might want to extend theextract_cssfunction to handle these imports recursively. -
Dynamic Styles: If the website uses JavaScript to inject or modify styles dynamically, this static extraction method might miss those. Consider using a headless browser like Selenium or Playwright for more comprehensive extraction if necessary.
-
Optimizing Selector Matching: For better coverage, especially for
button_color, consider using more advanced selector matching or gathering all styles that apply to button elements, regardless of their class or type. -
Caching External CSS: To improve performance and reduce repeated network requests, especially when processing multiple URLs, implement caching for external CSS files.
By incorporating these improvements, your CSS extraction process should provide a more complete and accurate set of styles for the LLM to analyze, ensuring precise extraction of
background_color,button_color,text_color, andfont_style. -