import requests from bs4 import BeautifulSoup import re import logging…
- User
import requests from bs4 import BeautifulSoup import re import logging
logging.basicConfig(level=logging.INFO) logger = logging.getLogger(name)
def extract_css(url): try: # Tải nội dung trang web response = requests.get(url) response.raise_for_status() soup = BeautifulSoup(response.text, 'html.parser')
# Trích xuất tất cả CSS all_css = "" # CSS nội tuyến for style in soup.find_all('style'): all_css += style.string or "" # CSS từ các file external 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 = url + css_url if css_url.startswith('/') else 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"Không thể tải CSS từ {css_url}: {e}") print(len(all_css)) return extract_relevant_css(all_css) except Exception as e: logger.error(f"Lỗi khi trích xuất CSS: {e}") return ""def extract_relevant_css(css_content): relevant_css = ""
# Regex patterns cho các thuộc tính cần thiết patterns = { 'body_styles': r'body\s*{[^}]*}', 'background': r'(?:background|background-color):\s*([^;]+)', 'button_styles': r'(?:\.button|button|\.btn|\[type="button"\]|input\[type="submit"\])(?:[^{]+{[^}]*})', 'text_color': r'color:\s*([^;]+)', 'font_style': r'font-(?:family|style|weight|size):\s*([^;]+)' } for prop, pattern in patterns.items(): matches = re.findall(pattern, css_content, re.IGNORECASE | re.DOTALL) if matches: relevant_css += f"/* {prop.replace('_', ' ').title()} */\n" for match in matches: if prop in ['body_styles', 'button_styles']: relevant_css += f"{match}\n" else: relevant_css += f"{prop}: {match};\n" relevant_css += "\n" # Trích xuất các biến CSS (custom properties) css_vars = re.findall(r'(--[^:]+):\s*([^;]+)', css_content) if css_vars: relevant_css += "/* CSS Variables */\n" for var, value in css_vars: relevant_css += f"{var}: {value};\n" relevant_css += "\n" # Trích xuất media queries liên quan media_queries = re.findall(r'@media[^{]+{([^}]+)}', css_content) if media_queries: relevant_css += "/* Relevant Media Queries */\n" for query in media_queries: if any(re.search(pattern, query, re.IGNORECASE) for pattern in patterns.values()): relevant_css += f"@media ... {{\n{query}\n}}\n\n" return relevant_cssSử dụng hàm
url = "https://www.vsatech.net/" # URL thực tế result = extract_css(url) print(result) 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).
- Assistant