select class , div , 如何在碎片中标记?

How select class , div , tag in splinter?

我想了解 splinter 的功能,我试图在网上找到,但我找不到关于 splinter 的实用示例的好文档,所以我在这里问了几个问题,以便它可以帮助正在尝试学习 splinter 的初学者:

首先我搞不清楚什么是实际的 css select或者在 splinter 中,我到处都能看到两种方法:

browser.find_by_css() 

browser.find_by_css_selector()

它们之间有什么区别,为什么第二个在当前 splinter 中不起作用?

现在我最初的问题是如何 select 任何 class 下的任何标签,如何 select 任何 id 下的任何标签?

我试图找到,但我在 "how to select option values in dropdown" 上的 splinter 上发现了大多数 Whosebug 问题,而且 splinter 文档非常好,但问题是他们的方法没有足够的实用示例。

所以如果我有这个 html 代码:

<div class="medium-widget success-story-category">
                        <h2 class="widget-title"><span aria-hidden="true" class="icon-get-started"></span>Getting Started</h2>
<p>Python can be easy to pick up whether you're a first time programmer or you're experienced with other languages. The following pages are a useful first step to get on your way writing programs with Python!</p>
<ul>
    <li><a href="https://wiki.python.org/moin/BeginnersGuide/Programmers">Beginner's Guide, Programmers</a></li>
    <li><a href="https://wiki.python.org/moin/BeginnersGuide/NonProgrammers">Beginner's Guide, Non-Programmers</a></li>
    <li><a href="https://wiki.python.org/moin/BeginnersGuide/Download">Beginner's Guide, Download &amp; Installation</a></li>
    <li><a href="https://wiki.python.org/moin/BeginnersGuide/Examples">Code sample and snippets for Beginners</a></li>
</ul>

                    </div>

然后:

现在如果有<class_name id="something">喜欢:

<nav id="mainnav" class="python-navigation main-navigation do-not-print" role="navigation">


<ul class="navigation menu" role="menubar" aria-label="Main Navigation">



    <li id="about" class="tier-1 element-1   with-supernav" aria-haspopup="true">
        <a href="/about/" title="" class=" current_item selected selected">About</a>

我在这里找到了答案,我将进行解释,以便对其他程序员有所帮助:

首先 browser.find_by_css_selector() 不起作用,我使用了 find_by_css 方法,效果很好,所以我更喜欢 find_by_css 方法。

如何通过 selecting class="medium-widget success-story-category"

来 select <p> 标记数据

我们可以select任何class格式为:

div[class="class_name"]div[any_style_element="value"]

我们可以selectclass class="medium-widget success-story-category" 经过 div[class="medium-widget success-story-category"]

我们可以 select

标记 ('div[class="medium-widget success-story-category"] p')

我们也可以通过 :

找到

find_h=browser.find_by_css('div[class="medium-widget success-story-category last"]:nth-child(2)')

当 html 是

`<div class="row">

                    <div class="medium-widget success-story-category">
                        <h2 class="widget-title"><span aria-hidden="true" class="icon-get-started"></span>Getting Started</h2>
<p>Python can be easy to pick up whether you're a first time programmer or you're experienced with other languages. The following pages are a useful first step to get on your way writing programs with Python!</p>
<ul>
    <li><a href="https://wiki.python.org/moin/BeginnersGuide/Programmers">Beginner's Guide, Programmers</a></li>
    <li><a href="https://wiki.python.org/moin/BeginnersGuide/NonProgrammers">Beginner's Guide, Non-Programmers</a></li>
    <li><a href="https://wiki.python.org/moin/BeginnersGuide/Download">Beginner's Guide, Download &amp; Installation</a></li>
    <li><a href="https://wiki.python.org/moin/BeginnersGuide/Examples">Code sample and snippets for Beginners</a></li>
</ul>

                    </div>

                    <div class="medium-widget success-story-category last">
                        <h2 class="widget-title"><span aria-hidden="true" class="icon-success-stories"></span>Friendly &amp; Easy to Learn</h2>
<p>The community hosts conferences and meetups, collaborates on code, and much more. Python's documentation will help you along the way, and the mailing lists will keep you in touch.</p>
<ul>
    <li><a href="/community/workshops/">Conferences and Workshops</a></li>
    <li><a href="http://docs.python.org">Python Documentation</a></li>
    <li><a href="/community/lists">Mailing Lists</a> and <a href="/community/irc/">IRC channels</a></li>
</ul>

                    </div>

                </div>`

通过使用:

`find_h=browser.find_by_css('div[class="row"]:nth-child(1) > div:nth-child(1) > p')
for i in find_h:
    print(i.text)`

我们可以通过

捕获class中的图像

('div[class="image_class_name"] img') 然后 result["href" or "src"]

示例:

假设我必须select那个图像然后我可以通过这个代码得到它:

find_h=browser.find_by_css('h1[class="site-headline"] img')
for i in find_h:
    print(i["src"])

下一个问题是如何 select

  • 标记:我们可以 select
  • 标记 usng nth-child(n) :

    所以如果我有这个 html 代码:

    <div class="medium-widget success-story-category last">
                            <h2 class="widget-title"><span aria-hidden="true" class="icon-success-stories"></span>Friendly &amp; Easy to Learn</h2>
    <p>The community hosts conferences and meetups, collaborates on code, and much more. Python's documentation will help you along the way, and the mailing lists will keep you in touch.</p>
    <ul>
        <li><a href="/community/workshops/">Conferences and Workshops</a></li>
        <li><a href="http://docs.python.org">Python Documentation</a></li>
        <li><a href="/community/lists">Mailing Lists</a> and <a href="/community/irc/">IRC channels</a></li>
    </ul>
                        </div>
    
    
    
    <div class="medium-widget success-story-category last">
                            <h2 class="widget-title"><span aria-hidden="true" class="icon-success-stories"></span>Friendly &amp; Easy to Learn</h2>
    <p>The community hosts conferences and meetups, collaborates on code, and much more. Python's documentation will help you along the way, and the mailing lists will keep you in touch.</p>
    <ul>
        <li><a href="/community/workshops/">Conferences and Workshops</a></li>
        <li><a href="http://docs.python.org">Python Documentation</a></li>
        <li><a href="/community/lists">Mailing Lists</a> and <a href="/community/irc/">IRC channels</a></li>
    </ul>
                        </div>
    

    然后我们可以 select href link 任何

  • 通过使用

    div[class="medium-widget success-story-category last"]:nth-child(1) > ul > li:nth-child(2) > a
    

    请记住 div[class="medium-widget success-story-category last"]:nth-child(1) 中的第 nth-child(2) 不是 select 这个 class 的第二个嵌套 div 而是第 nth-child(2) select second medium-widget success-story-category last class (你可以看到有两个 class 同名 medium-widget success-story-category last) .

    最后一个问题的最后答案:

    如果有<class_name id="something"> :

    然后 select 喜欢

    class_name[id="something"]