1
var response = await http.get('https://www....');
dom.Document document = parser.parse(response.body);
var selectElements = document.getElementsByClassName('hello').map((item) => item.children).toList();    

I can sort and list by classes thanks to the http library, but I don't know how to do it according to attributes. How can I list by attributes?

There is an HTML output as below. I want to list the ones whose attribute data-time is 9112020 and see the results for lorem ipsum3 and lorem ipsum4.

<tr class="hello" data-country="Germany" data-time="8112020">lorem ipsum1</tr>
<tr class="hello" data-country="Turkey" data-time="8112020">lorem ipsum2</tr>
<tr class="hello" data-country="Germany" data-time="9112020">lorem ipsum3</tr>
<tr class="hello" data-country="Turkey" data-time="9112020">lorem ipsum4</tr>

1 Answer 1

1

You need to use the where function to filter your items:

  var selectElements  = document
      .getElementsByClassName('hello')
      .where((element) => element.attributes['data-time'] == '9112020')
      .map((item) => item.text)
      .toList();

here is a GitHub gist for my full code: https://gist.github.com/ali2236/14a12460a7b8f857e9e5c02583fff91c

There also seems to be a bug with the HTML parser in my example which should not happen in the real world.

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.