【Python】スクレイピングにおける要素特定は、XPathかCSSセレクターか

スクレイピングにおいて、HTML要素の特定する手段は、XPathとCSSセレクターの2つがあります。Pythonでスクレイピングを始めた当初、どっちで要素を特定するか迷いました。迷わないように参考になる情報をまとめておきました。

XPathとCSSセレクターの比較

XPathかCSSセレクターかですが、ライブラリから見るとCSSセレクターの一択な感があります。しかし、表現の幅の広さでみるとXPathの方が圧倒的に有利です。

ライブラリのXPathとCSSセレクターのサポート状況

この通り使うライブラリによっては、選択の余地なしです。

ライブラリ XPath CSSセレクター
lxml
Beautiful Soup ×
selenium
pyquery ×

ちなみに、lxmlのCSSセレクターは、パッケージ内にlxml.cssselectモジュールという形で提供されています。

XPathとCSSセレクターの要素特定の書き方の比較

下にある通り、CSSセレクターの方が記述が簡単です。とても覚えやすいです。しかし、残念なことに、XPathにできるのに「表現不可」なものが存在します。

ターゲット要素 XPath CSSセレクタ
全要素 //* *
p要素 //p p
p要素の全要素 //p/* p>*
ID属性がfooの要素 //*[@id=’foo’] #foo
クラス属性がfooを含む要素 //*[contains(@class,’foo’)] .foo
title属性を含む要素 //*[@title] *[title]
pの最初の子要素 //p/*[0] p>*:first-child
要素aを持っ全てp要素 //p[a] 表現不可
pと同系列の次の要素 //p/following-sibling::*[0] p + *
pと同系列の前の要素 //p/preceding-sibling::*[0] 表現不可
src属性が”.jpg”で終わるimg要素 //img[ends-with(@src,”.jpg”)] img[src$=”.jpg”]
直下のテキストが文字列であるp要素 //p[text()=”文字列”] 表現不可

Chromeを使って、要素を特定する文字列を簡単に知る方法

Chromeのデベロッパーツールで、要素を特定できる文字列の取得が可能です。

Windowsの場合、F12でツールを開いて、Elementタブを右クリックすると、取得できます。

XPathの記述を取得

取得結果 //*[@id=”rso”]/div[4]/div/div/div[1]/a[1]/h3

CSSセレクターの記述を取得

取得結果 #rso > div:nth-child(4) > div > div > div.yuRUbf > a:nth-child(1) > h3

実際にスクレイピングしていると、CSSセレクターのみ使用

CSSのみを使う理由は、2つあります。

XPathの「直下のテキストが文字列であるp要素」は便利で、これがあればfor文で回す必要がなく、記述も短く済み非常に便利です。ですが、それ以外の特定方法の出番はないです。

CSSセレクターでたどり難い要素は、先に親要素を見つけ、そこからfor文を回して見つけています。for文内で、正規表現を使って、より正確に探り当てます。もちろん遅いです。しかし、WEBサーバにアクセスするスクレイピングは処理スピードを求める必要はないのです。

コメント

  1. She had a feeling that theprincipal express to Rome passed through at nigh Why hadn,t thatwoman Kate Lumley arrived yet? She had forgotten all about her,リアル えろ

  2. ラブドール 女性 用copyrghtlaw means that no one owns a Unted States copyrght n these works,so the Foundaton (and you!) can copy and dstrbute t n the UntedStates wthout permsson and wthout payng copyrghtroyalte Specal rules,

  3. ll want to make t tdy.? ?sad Valancy honestly.高級 ラブドール

  4. axb ドール より:

    the marks of prolonged and sordid negligence.Thedoor,ラブドール av

  5. –Her hair was curiously plaited,andinterwoven with bobbins of human bone–one eye-lid was painted greeand the other yellow,エロ 人形

  6. エロ 人形and do the business of the stableindifferent well–I can dress a horse,and shoe him,

  7. I ducked it.He slewed around with the effort.ラブドール

  8. girl ?said s when the door wasshut.sex doll“I do not know how you will ever make him amends for his kindnesor me either,

  9. 人形 エロ より:

    and that also on theisland,almost concealed amongst the foliage of those lovely trees,ラブドール えろ

  10. that,regardless of the sentiments of either,ラブドール 風俗

  11. 人形 えろ より:

    えろ 人形Illustration]Till Elizabeth entered the drawing-room and looked invain for Wickham among the cluster of red coats there assembled,adoubt of his being present had never occurred to her.

  12. ?Elizabeth could hardly help smiling,エロ リアルas she assured her that had notbeen the case.

  13. ダッチワイフincontestable et très fort,est le premier pour lequel nous devonstravailler particulièrement à la conversion des infidèles des coloniesde notre patrie.

  14. nihil non illum huic libro tribuere,multa licet cura,ラブドール えろ

  15. ラブドール 無 修正 より:

    ラブドールpeux-tu,si tum’aimes tant soit peu,

  16. mires lo que hazes.Y este refran entiendo le yo ansi: que antes que la muger o el hombre se casen,エロ ラブドール

  17. hermano._–No quiero sino desesperar; si oyes que hize desatino alguno,エロ ラブドール

  18. Empie?a tú,Caliope.ラブドール 販売

  19. ラブドール 販売_Fors etiam nostris,inuidit quaestibus aures.

  20. Padre,” was his thought,コスプレ えろ

  21. because I’m poor,リアル ラブドールand can’t enjoy my life as other girls do.

  22. the drunkard,the soldier of fortune,リアル ラブドール

  23. The _Bhagavada_,ラブドール 中出しwhich _Niebuhr_,

  24. In this code a pin-point’sdifference in the position of the letters would have made all thedifference.ラブドール ブログThat is why they are trying to send it to Germany,

  25. ラブドール より:

    _ ?cui?,セックス コスプレ_to_ or _for whom?_ ?cui?,

  26. irontech dollas if a torch had been touched to myblood do I suffer.If thou hadst eyes to see through these rocks thouwouldst have beheld a maiden carrying about in her body a living tombof corruption which came to her at the hand of Herod and back of him,

  27. 銉┿儢銉夈兗銉?銈儕銉嬨兗gaunt female showed in the doorway.She wore a soiled printdress; her face was sallow and grimy.

  28. niin kuin poikaviikarit py?ritt?v?t rottaah?nn?st?,ラブドール オナニーja heitti h?net majaan,

  29. ラブドール エロaunque no mal escrito,es cosapegadiza é impertinente,

  30. In the second place I want you to make absolutely certain that not apenny of my money goes to Sundle s Dogs Home.The man has beeninsolent to me,ラブドール sex

  31. Mira la _Hipotyposis del excelente Joan de Mena o del Bachiller Rojasde Montaluan_… (_Phrases Ciceronis,ラブドール エロ

  32. ラブドール えろXI En su prólogo esboza una teoríadel arte docente,y en la dedicatoria á Diego de Acevedo y Fonsecajustifica la materia misma de su libr aunque vuelve á declarar que leescribió á manera de pasatiempo: ?Y como ya los a?os pasados tuviesevacacion de graves y penosos estudios,

  33. que tampoco erade Meléndez,ラブドール えろsino del Maestro Alba_.

  34. ラブドール エロporque en ellos acaba la tradición delos Davos y los Siros,y penetra en el arte el tipo del fámulo libre,

  35. — arabialaisen olen vanginnut k?skysi mukaan,sill?kyyhkysesi oli taitava ja tuli nopeasti,ラブドール オナニー

  36. I have as yet seen nothing but his favourable side,who frequently confers with him,えろ 人形

  37. rg888 より:

    Just wish to say your article is as amazing. The clearness in your publish is just nice
    and that i could suppose you are knowledgeable
    in this subject. Fine together with your permission allow me to
    take hold of your feed to stay updated with forthcoming post.
    Thanks a million and please carry on the enjoyable work.

  38. by what means didst Thou convey Thyselfinto that breast? He used to read (as Simplicianus said) the holyScripture,ラブドール 安いmost studiously sought and searched into all the Christianwritings,

  39. 人形 エロ より:

    ’ Now is therenot here a third principle which is often found to come to theassistance of reason against desire,ラブドール オナニーbut never of desire againstreason? This is passion or spirit,

  40. MEETANG168 より:

    Undeniably consider that which you stated. Your favorite reason seemed to be at the net the simplest thing to take into account of.
    I say to you, I certainly get annoyed while people think about worries that
    they plainly don’t recognise about. You controlled to hit the nail upon the top and defined out the
    entire thing without having side-effects , other people
    could take a signal. Will likely be again to get more. Thanks

  41. Kodpung88 より:

    Wow, marvelous weblog layout! How lengthy have you been running
    a blog for? you made blogging look easy.
    The overall glance of your website is great, let
    alone the content!

  42. gmax より:

    Hello my family member! I want to say that this article is awesome, nice written and include almost all significant infos.
    I’d like to look extra posts like this .

  43. ダッチワイフ 販売I went along,carrying my child.

  44. okslot より:

    I am curious to find out what blog platform you’re using?
    I’m experiencing some minor security issues with my latest blog and I’d like to find something more secure.
    Do you have any solutions?

  45. U31 より:

    Whats up are using WordPress for your blog platform?

    I’m new to the blog world but I’m trying to get started and set up my own. Do you require any
    coding knowledge to make your own blog? Any help would be really appreciated!

  46. d55 より:

    Way cool! Some extremely valid points! I appreciate you
    writing this article and the rest of the
    website is very good.

  47. ラブドール より:

    pero voyme por el hilo de las gentes; entendido he,por mis pecados,ドール エロ

  48. ラブドール より:

    Desconfiado estoy de las mugeres,ラブドール 通販porque son flacas,

  49. family,cultural beliefs,リアル ドール

  50. Here’s a more comprehensive list of reasons why women don’t report marital rape or even understand that what has happened to them as rape:1.Misunderstandings about a woman’s role in marriage and marital responsibilities.ラブドール エロ

  51. and healthy respectful relationships (i.e.オナホ

  52. ラブドール エロa savvy businessman,a skilled musician,

  53. we were so tired I don’t remember if we both came again or not. セックス ロボットIt was so fucking hot, we were afraid nearby campers heard us.

  54. ”She suggests incorporating resistance training into your regime three times a week.“One study found the larger the muscles you work on,人形 エロ

  55. The last thing is simply to be aware of how you store your doll so it doesn’t get damaged or dentedえろ 人形.

  56. ラブドール sexhe said quietly.If anybodycame in after you–why,

  57. That is rather a curious request,ラブドール sexwith a smile; and youdon t usually have diamond necklaces in a stockbroker s office–if Imay be allowed to make that critical remark.

  58. was remindedof the Dante of convention,ラブドール sexthough Dante never wore a top-hat or founda Derby Day crowd so entirely absorbing.

  59. not? de sa main,és endroits plusmemorables (comme il estait l’un des plus practics gentils hommesde son temps esdictes langues,ラブドール えろ

  60. ラブドール オナニーjossa Vaiteliaatodottivat h?nen tietojaan.H?n tuli horjuen aukeamalle,

  61. olet tullut suurenpyhyyden hetkell?.Ja h?n selosti ohikulkeneen Msimba Msamban tekojaja osoitti murtunutta kattoa,ラブドール sex

  62. beaucoup pourla conversion des pécheurs,puisque c’est pour eux surtout queNotre-Seigneur travaille,ラブドール

  63. Así lo testifican su poema _El infelicerobo de Helena_,ラブドール えろsu colección de romances sobre _La antigua,

  64. se dapor imitador nada menos que de Menandro: _Venerat in linguam nuper peregrina latinam Haec de Menandri fabula rapta sinu…_Su argumento recuerda mucho el del _Eunuco de salvo queel seductor no se hace pasar por eunuco,ダッチワイフ

  65. ラブドール より:

    ?tendría algúnconocimiento de las griegas? No sería inverisímil el caso,porque yaen su tiempo las ense?aban en Salamanca Nebrija y Arias Barbosa,ダッチワイフ

  66. ラブドール より:

    commequand Vous restates trois jours à Jérusalem; sauf le cas où lavocation que Vous aviez demandait que Vous ne Vous rendiez pas à leursdésirs,Vous Vous y rendiez en tout,ラブドール

  67. ラブドール より:

    ラブドール sexor hate,orsorrow.

  68. ラブドール より:

    invadida y perturbada por el hipérbaton latino,ラブドール エロde que hacen groseroy servil calco los alumnos de la detestable escuela de D.

  69. What kind of voice is it? Must be curious to hear after their ownstrong basse Connoisseur Suppose they wouldn,t feel anything after.コスプレ セックス

  70. since we were as littleas this child,and much less,エロ い コスプレ

  71. ?“ Lucie? It is out of the Consolation and restoration you havebrought to me,that these remembrances arise,下着 エロ

  72. ラブドール より:

    ラブドール.. Comme il estfaux!… Comme c’est bien le cri que pousse la nature humaine quand ellen’est pas divinisée ce chant qui voudrait être unchant de joie et malgré lui,est si plaintif,

  73. エロ 下着 より:

    The undertaking was one of great difficulty and danger; f as we could hardly expect to find much,if any,コスプレ アダルト

  74. ストッキング えろNo living man can make out the meaning of a word in theRestrictive Rules from Webster’s dictionary.You must get it from thehistory of the Church.

  75. 熟女 t バックthat I have been too fond of my own notions; which I confess we are allapt to be,when application and study have warmed our heads with them.

  76. We give you ten minutes to get to your stations.ラブドール av?As Bradshaw left,

  77. ラブドール より:

    せっくす どー るHad he foreseen thisinconvenience he might have made shift to obviate the consequences,byobtaining permission to appear in the character of s kinsman,

  78. ラブドール より:

    sbattlement which would be empty at this hour,and watch the moon riseout of the sea.リアル えろ

  79. ラブドール より:

    sex ドールand kind,and sociable,

  80. エロ 人形s (said he) that we could hardly get along–I wish thoseminions of fortune would fall upon more laudable ways of spending theirmoney.– Sir,

  81. he took hold of my bottle,instead ofhis ow filling a large glas drank to the health of Mrs Tabitha.エロ 人形

  82. リアル えろ“Which mistress? ?“There are four,?said Francesca,

  83. remained on it unconsciously,リアル えろand withno effort at all.

  84. a queer reply,ダッチワイフ エロthe vicar though“In February? ?he called after her sarcastically.

  85. manipulation,constant criticism,えろ 人形

  86. their observation (but perhaps only anecdotal evidence like my experience with the tissues),or a well-developed theoretical framework.エロ ラブドール

  87. きもおじ家の新人さん?のご紹介です。ラブドール エロ正しい使用法などを写真付きでご紹介しております。どうぞよろしくお願いいたします。

  88. And thanks to the release of the bonding chemical oxytocin during sex,セックス ドールthe act can also deepen love,

  89. ここでは、ラブドールとのセックスえろ 人形にどのようなメリットがあるのか解説していきます。

  90. 販売されているメーカーや商品によって異なりますが、オナドールオプションは大型サイズのドールになるほど機能が充実してきます。

  91. もちろん、脱着式ホールは伸縮性が高く伸びますが、セックス ロボットそれを踏まえてもキッツキツになるかと思います。

  92. live improved after which you can 初音 ミク ラブドールdelight in a cheerful and enjoy a contented and Fortunate sourcing time at Produced-in-China.com. Have not observed what you would like?

  93. ラブドール より:

    <a href="https://www.erdoll.com/tag/siliconelovedoll.htmlラブドール 男Providing a woman instructions to self-pleasure is almost always the first step in sex therapy for anorgasmia.Additional steps are trying common finger motions,

  94. ‘I’m meant to wank with this? It looks like a coffee cup!’ We’re staring, ラブドール 女性 用brows furrowed, at what looks like a reusable thermos.

  95. who had been at the feast,had hid herself in thepigeonhouse.ドール エロ

  96. アダルト コスプレHans had been at home for a but Elsie did not come; then saidhe: ‘What a clever Elsie I have; she is so industrious that she does noteven come home to eat.’ But when evening came and she still stayed away,

  97. Olympus’ hundred heads arise;Where far apart the Thunderer fills his throne,O’er all the gods superior and alone.コスプレ せっくす

  98. They it was who by universal prescription commanded three of thePequod,ラブドール 激安s boats as headsmen.

  99. えろ 人形s manners and politenes He protested that,except LadyCatherine and her daughter,

  100. s flank Though in the course of hiscontinual voyagings Ahab must often before have noticed a similarsight,人形 エロto any monomaniac man,

タイトルとURLをコピーしました