runningmanjoe
runningmanjoe

Reputation: 47

Targeting id's with scrapy css selector

HTML = '
    <div class="box">
        <div id="grid">
            <div class="content">hello</div>
        </div>
    </div>
'

--stuff happens here--

response.css('div.thread [*] div.mix').getall()

*how do I search for divs without a class? No matter what suggestion I find online I keep getting the error "expected something got delim..."

Upvotes: 3

Views: 3065

Answers (4)

Barco
Barco

Reputation: 11

There's also the Xpath way in Scrapy.

response.xpath('//div[@id="grid"]/div/text()').get()

Upvotes: 0

stranac
stranac

Reputation: 28256

If you want to select the div with a certain id, you can do what the other answers suggest.

However, if you want to select any div without a class attribute, use:

.css('div:not([class])')

Upvotes: 0

gangabass
gangabass

Reputation: 10666

You need to use # for an id attribute:

response.css('div#grid')

Or you can select it by attribute notation:

response.css('div[id="grid"]')

Upvotes: 6

ThePyGuy
ThePyGuy

Reputation: 1035

You could try something like this. The logic is it will find None and fail the first if check if there is no class and then append it to the no_class_divs list. If you want to find based on ID then accept @gangabass answer. If you want to find div elements that have no class attribute then my answer should work.

no_class_divs = []
divs = response.css("div")

for div in divs:
    if div.css('::attr(class)').extract_first():
        continue
    else:
        no_class_divs.append(div)

Upvotes: 0

Related Questions