Search code examples
htmlparsingscrapycss-selectorshtml-parsing

How to get html tag attribute with square brackets using css selector?


So I want to parse one page and it has several items with following structure:

<span itemprop="telephone" [class]="revealtel?'':'invisible'" class="">11111111</span>
<span itemprop="telephone" [class]="revealmainfax?'':'invisible'" class="">222222222</span>

I am using Scrapy and CSS selectors to parse data. But I cannot understand how to get tel or fax numbers. Itemprop is the same in both cases, so it cannot be used. So, how to select element with [class]="revealtel?'':'invisible' or [class]="revealmainfax?'':'invisible' attributes? Maybe not using CSS Selectors, but Xpath? I am not strong with Xpaths though...

Thanks in advance for helping me :)


Solution

  • Using xpath to get a list of all text from elements with telephone as itemprop.

    faxnum = None
    telnum = None
    numbers = response.xpath('//span[@itemprop="telephone"]')
    for element in numbers:
        text = element.extract()
        if re.search('revealmainfax', text):
            faxnum = element.xpath('./text()')
        else:
            telnum = element.xpath('./text()')