1
$\begingroup$

I try parse hub titles and links from this site: https://habr.com/en/hubs/, but code returns empty list. Can not understand why.

Dataset[ Cases[  Import[StringJoin["https://habr.com/en/hubs/", "page", 
ToString[1]], "XMLObject"],   XMLElement[
"div", {}, {XMLElement[
  "a", {"class" -> "tm-hub__title", "href" -> link_}, 
  {___, XMLElement["span", {}, {name_}], ___}]}] :>    Association["HabName" -> name, "HabAlias" -> FileBaseName[link]],   Infinity]]
$\endgroup$

2 Answers 2

1
$\begingroup$
Cases[Import[
   StringJoin["https://habr.com/en/hubs/", "page", ToString[1]], 
   "XMLObject"], 
  XMLElement[
    "a", {OrderlessPatternSequence[___, 
      "class" -> "tm-hub__title", ___, "href" -> link_, ___]}, {___, 
     XMLElement["span", _, {name_}], ___}] :> 
   Association["HabName" -> name, "HabAlias" -> FileBaseName[link]], 
  Infinity] // Dataset

Cropped result:

enter image description here

Notes:

  • div is removed, because found it unnecessary
  • Added ___ for attributes
  • Use OrderlessPatternSequence to cover all the permutations in attributes (could be removed in this case)
$\endgroup$
1
  • $\begingroup$ Thank you very much for answer. $\endgroup$
    – katifa sh
    Oct 1, 2021 at 14:59
1
$\begingroup$

The structure of the page probably changed. Replacing the rule inside your Cases command with the one below fixes it:

XMLElement["div", {}, 
  XMLElement["a", {___, "class" -> "tm-hub__title", "href" -> link_}, 
    {___, XMLElement["span", {}, {name_}], ___}]] :> 
  Association["HabName" -> name, "HabAlias" -> FileBaseName[link]]
$\endgroup$
1
  • $\begingroup$ Thank you very much for answer. $\endgroup$
    – katifa sh
    Oct 1, 2021 at 14:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.