import requests import re from bs4 import BeautifulSoup from urllib.parse import urljoin…
- User
import requests import re from bs4 import BeautifulSoup from urllib.parse import urljoin
def extract_all_css(url): response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser')
codeall_css = [] # Extract inline styles for tag in soup.find_all(style=True): all_css.append(tag['style']) # Extract internal stylesheets for style in soup.find_all('style'): all_css.append(style.string) # Extract external stylesheets for link in soup.find_all('link', rel='stylesheet'): stylesheet_url = urljoin(url, link.get('href')) try: css_response = requests.get(stylesheet_url) all_css.append(css_response.text) except: print(f"Không thể tải stylesheet từ {stylesheet_url}") return ' '.join(all_css)def extract_specific_css(all_css): # Định nghĩa các regex patterns patterns = { 'background_color': r'background-color:\s*([^;]+)', 'text_color': r'color:\s*([^;]+)', 'button_color': r'(button|.btn).?{[^}]background-color:\s([^;]+)', 'font_style': r'font-family:\s([^;]+)' }
coderesults = {} for key, pattern in patterns.items(): matches = re.findall(pattern, all_css, re.IGNORECASE) if matches: if key == 'button_color': results[key] = [match[1] for match in matches] else: results[key] = matches return resultsSử dụng các hàm
web_url = "https://www.vsatech.net/" all_css = extract_all_css(web_url) specific_css = extract_specific_css(all_css)
print("Kết quả trích xuất CSS:") for key, value in specific_css.items(): print(f"{key}: {value}") \n Hãy cải tiến đoạn code trên sao cho nó lấy các dòng CSS code không thừa cũng không thiếu, đủ để sau đó sẽ dùng LLMs trích xuất chính xác các trường 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.")
- Assistant