(Scrapy) How to get the CSS rule for a HTML element?
<p>I am building a crawler using Scrapy. I need to get the font-family assigned to a particular HTML element.</p>

<p>Let's say there is a css file, styles.css, which contains the following:</p>

<pre><code>p {
    font-family: "Times New Roman", Georgia, Serif;
}
</code></pre>

<p>And in the HTML page there is text as follows:</p>

<pre><code>&lt;p&gt;Hello how are you?&lt;/p&gt;
</code></pre>

<p>Its easy for me to extract the text using Scrapy, however I would also like to know the font-family applied to <em>Hello how are you?</em></p>

<p>I am hoping it is simply a case of (imaginary XPATH) <code>/p[font-family]</code> or something like that.</p>

<p>Do you know how I can do this?</p>

<p>Thanks for your thoughts.</p>

<p>You need to download and parse css seperately. For css parsing you can use <a href="https://pythonhosted.org/tinycss/" rel="nofollow">tinycss</a> or even regex:</p>

<pre><code>import tinycss
class MySpider(Spider):
    name='myspider'
    start_urls = [
        'http://some.url.com'
    ]
    css_rules = {}

def parse(self, response):
    # find css url and parse it
    css_url = response.xpath("").extract_first()
    yield Request(css_url, self.parse_css)

def parse_css(self, response):
    parser = tinycss.make_parser()
    stylesheet = parser.parse_stylesheet(response.body)
    for rule in stylesheet.rules:
        if not getattr(rule, 'selector'):
            continue 
        path = rule.selector.as_css()
        css =  [d.value.as_css() for d in rule.declarations]
        self.css_rules[path] = css
</code></pre>

<p>Now you have a dictionary with css paths and their attributes that you can use later in your spider request chain to assign some values:</p>

<pre><code>def parse_item(self, response):
    item = {}
    item['name'] = response.css('div.name').extract_first()
    name_css = []
    for k,v in css_rules.items():
        if 'div' in k and '.name' in k:
            name_css.append(v)
    item['name_css'] = name_css
</code></pre>

python xpath scrapy