0

Here is the HTML code

<!DOCTYPE html>
<html lang="en">
<div class="container" id="content-area">
    <div class="flex-row flex-baseline flex-space-between" data-id="1826" id="info">
      <h1 class="no-margin">XYZ</h1>
        <div class="new-stack" id="sublists">Added</div>
    </div>
</div>

I am looking to pull the data-id attribute inside div tag. Here is what I am trying using CSS Selector

>>> response.css("#content-area div")[0].css("::attr[data-id]").get()

And I got below error

cssselect.parser.SelectorSyntaxError: Got pseudo-element ::attr not at the end of a selector

Here is how I solved it by combining CSS and XPATH Selectors.

>>> response.css("#content-area div")[0].xpath("@data-id").get()
'1826'

Is there any solution which can do this using just CSS Selector?

1 Answer 1

1

You need to use () instead of []

>>> response.css("#content-area div")[0].css("::attr(data-id)").get()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.