Skip to content

querySelector is not working properly #1027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Livinglist opened this issue May 8, 2019 · 2 comments
Closed

querySelector is not working properly #1027

Livinglist opened this issue May 8, 2019 · 2 comments

Comments

@Livinglist
Copy link

Livinglist commented May 8, 2019

The code snippet below is supposed to scrap all the sentences showed on https://jisho.org/search/%E6%97%A5%20%23sentences; however, the sentences after the execution of the function does not contain proper results.

For example, the selector #secondary > div > ul > li:nth-child(2) > div.sentence_content > ul > li:nth-child(1) > span.unlinked should return the element <span class="unlinked">今日</span> instead, it retuens null

List<Sentence> parseHtml() async {
    List<Sentence> sentences = [];
    Client client = Client();
    Response response = await client.get('https://jisho.org/search/%E6%97%A5%20%23sentences');
    var doc = parse(response.body);
    
    int childIndex;
    for (int i = 1; i <= 20; i++) {
      childIndex = 1;
      List<Token> tokens = [];
      while (true) {
        var element =
            doc.querySelector('#secondary > div > ul > li:nth-child($i) > div.sentence_content > ul > li:nth-child($childIndex) > span.unlinked');
        //print(element.text);
        if (element == null) break;
        var text = element.text;
        print("the i is $i and the currentIndex is $childIndex and the text is $text");
        element =
            doc.querySelector('#secondary > div > ul > li:nth-child($i) > div.sentence_content > ul > li:nth-child($childIndex) > span.furigana');
        if (element == null) {
          tokens.add(Token(text: text));
        } else {
          var furigana = element.text;
          tokens.add(Token(text: text, furigana: furigana));
        }
        childIndex++;
      }
        var sentence = Sentence(tokens: tokens);
        print(sentence.text);
        this.sentences.add(sentence);
        sentences.add(Sentence());
    }
    return sentences;
  }

class Token {
  String text;
  String furigana;

  Token({@required this.text, this.furigana});
}

class Sentence {
  List<Token> tokens;
  String text = '';

  Sentence({this.tokens}) {
    for (var token in tokens) {
      text += token.text;
    }
  }
}
@chen-zeyu
Copy link

"li:nth-child(2)" is not yet supported , #1018

@Livinglist
Copy link
Author

Duplicate of #84

@mosuem mosuem transferred this issue from dart-archive/html Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants