In CSS, attributes are properties that can be assigned to HTML or XML elements to control their appearance and behavior on a web page. Attributes can be assigned using CSS selectors and can be used to style elements, add interactive features, or provide additional information about the element.
Here are some common attributes in CSS:
class: This attribute is used to assign one or more class names to an element. Class names can be used in CSS selectors to apply styles to specific elements or groups of elements.
<p class="important">This paragraph is important.</p> |
id: This attribute is used to assign a unique identifier to an element. IDs can be used in CSS selectors to apply styles to specific elements or to link to a specific element on a web page.
<div id="header">This is the header.</div> |
style: This attribute is used to assign inline styles to an element. Inline styles are CSS styles that are applied directly to the element, rather than being defined in an external stylesheet.
<span style="color: red; font-weight: bold;">This text is red and bold.</span> |
href: This attribute is used to specify the URL of a link element. The href attribute can be used with the a element to create clickable links on a web page.
<a href="https://www.example.com/">Click here to go to Example.com</a> |
CSS attributes can be selected and manipulated using CSS selectors in Scrapy. For example, you can use the css() method to select elements with a specific class or ID, or to select elements with a specific attribute value:
from scrapy import Selector
selector = Selector(text='<a href="https://www.example.com/" class="link">Click here</a>')
# Select the href attribute value of the link element
href = selector.css('a.link::attr(href)').get()
print(href) # Output: 'https://www.example.com/'
|
In this example, we use the css() method to select the href attribute value of an a element with class name link. The ::attr(href) pseudo-element is used to select the href attribute value, rather than the element itself.