Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Splitter中选择类、div和tag?_Python_Selenium_Automation_Beautifulsoup_Splinter - Fatal编程技术网

Python 如何在Splitter中选择类、div和tag?

Python 如何在Splitter中选择类、div和tag?,python,selenium,automation,beautifulsoup,splinter,Python,Selenium,Automation,Beautifulsoup,Splinter,我试图了解splinter的功能,我试图在web上查找,但我找不到关于splinter的实用示例的好文档,所以我在这里问了几个问题,以便帮助尝试学习splinter的初学者: 首先,我混淆了Splitter中实际的css选择器是什么,我到处都可以看到两种方法: browser.find_by_css() 或 它们之间的区别是什么?为什么第二个在当前splinter中不起作用 现在我的原始问题是如何选择任何类下的任何标记,如何选择任何id下的任何标记 我试图找到,但我在一个关于“如何在下拉列表

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

首先,我混淆了Splitter中实际的css选择器是什么,我到处都可以看到两种方法:

browser.find_by_css() 

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

现在我的原始问题是如何选择任何类下的任何标记,如何选择任何id下的任何标记

我试图找到,但我在一个关于“如何在下拉列表中选择选项值”的小条上发现了大多数stackoverflow问题,小条文档非常好,但问题是他们没有足够的实用示例来说明他们的方法

因此,如果我有以下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>
<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>
  • 现在如何选择:
    ,使用find\u by\u css方法(不使用find\u by\u id)

  • 如何使用按css查找链接


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

第一件事
browser.find_by_css_selector()
不起作用,我使用了find_by_css方法,它工作得很好,所以我更喜欢
find_by_css
方法

如何通过选择
class=“medium”窗口小部件来选择标签数据
成功故事类别“

我们可以选择格式为:

div[class=“class\u name”]
div[any\u style\u element=“value”]

我们可以选课
class=“中等”小部件
成功故事类别“
通过
div[class=“medium”窗口小部件
成功故事类别“]

我们可以通过
('div[class=“medium widget success story category”]p')

我们还可以找到:

find\u h=browser.find\u by\u css('div[class=“medium widget success story category last”]:第n个孩子(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>`
我们可以用相机拍摄一个班级的图像

('div[class=“image\u class\u name”]img')
然后
结果[“href”或“src”]

例如:

假设我必须选择该图像,那么我可以通过以下代码获得它:

find_h=browser.find_by_css('h1[class="site-headline"] img')
for i in find_h:
    print(i["src"])
下一个问题是如何选择
  • 标记:我们可以选择
  • 标记usng nth child(n):

    因此,如果我有以下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>
    
    <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>
    
    请记住
    div[class=“medium widget success story category last”]中的第n个子(2):第n个子(1)
    不选择该类的第二个嵌套div,而是第n个子(2)选择第二个
    medium widget success story category last
    class(如您所见,有两个类具有相同的名称
    medium widget success story category last

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

    如果存在

    然后选择like

    class_name[id="something"]
    

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

    第一件事
    browser.find_by_css_selector()
    不起作用,我使用了find_by_css方法,它工作得很好,所以我更喜欢
    find_by_css
    方法

    如何通过选择
    class=“medium”窗口小部件来选择标签数据
    成功故事类别“

    我们可以选择格式为:

    div[class=“class\u name”]
    div[any\u style\u element=“value”]

    我们可以选课
    class=“中等”小部件
    成功故事类别“
    通过
    div[class=“medium”窗口小部件
    成功故事类别“]

    我们可以通过
    ('div[class=“medium widget success story category”]p')

    我们还可以找到:

    find\u h=browser.find\u by\u css('div[class=“medium widget success story category last”]:第n个孩子(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>`
    
    我们可以用相机拍摄一个班级的图像

    ('div[class=“image\u class\u name”]img')
    然后
    结果[“href”或“src”]

    例如:

    假设我必须选择该图像,那么我可以通过以下代码获得它:

    find_h=browser.find_by_css('h1[class="site-headline"] img')
    for i in find_h:
        print(i["src"])
    
    下一个问题是如何选择
  • 标记:我们可以选择
  • 标记usng nth child(n):

    因此,如果我有以下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>
    
    <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>
    
    请记住
    div[class=“medium widget success story category last”]中的第n个子(2):第n个子(1)
    不选择该类的第二个嵌套div,而是第n个子(2)选择第二个
    medium widget success story category last
    class(如您所见,有两个类具有相同的名称
    medium widget success story category last

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

    如果存在

    然后选择like

    class_name[id="something"]
    

    你最好一次只问一个问题time@iamsankalp89是的,我考虑过这一点,但如果我问一个问题,根据我的怀疑,这是不好的,我认为,因为所有的都是从同一件事和同一个模块相关,所以我一起问。没关系,这是很难回答的once@iamsankalp89我应该一个一个单独问他们吗,兄弟?会有很多问题:/是的,你可以在一个问题中问2-3个问题,所以其他问题可以提供简单的解决方案。你最好一次只问一个问题time@iamsankalp89是的,我考虑过这一点,但如果我问一个问题,根据我的怀疑,这是不好的,我认为,因为所有的都是从同一件事和相同的模块相关,所以我一起问1.没关系,很难回答once@iamsankalp89兄弟,我应该一个接一个地问他们吗?会有很多问题:/是的,你可以在一个问题中问2-3个问题,这样其他问题可以很容易地提供解决方案