我正在尝试理解分裂功能,我试图在网上找到但我无法找到关于分裂的实用例子的好文档,所以我在这里问了几个问题,这样它会帮助初学者试图学习分裂:
首先,我混淆了splinter中的实际css选择器,我到处都有两种方法:
browser.find_by_css()
或
browser.find_by_css_selector()
它们之间有什么区别,为什么第二个不能在当前的分裂中工作?
现在我的原始问题是如何选择任何类下的任何标签,如何选择任何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 & Installation</a></li>
<li><a href="https://wiki.python.org/moin/BeginnersGuide/Examples">Code sample and snippets for Beginners</a></li>
</ul>
</div>
然后:
如何选择<p>
class="medium-widget
success-story-category"
代码数据
第二:如何选择&#34; href&#34;第一个<li>
标记
第三:如何在第一个<li></li>
现在,如果有<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>
现在如何使用find_by_css方法选择:<nav id="mainnav" class="python-navigation main-navigation do-not-print" role="navigation">
id(不使用find_by_id)
如何使用find_by_css
<a>
链接
答案 0 :(得分:6)
我在这里找到了答案,我将解释这对其他程序员有帮助:
第一件事browser.find_by_css_selector()
无效,我使用的find_by_css方法完美无缺,因此我更喜欢find_by_css
方法。
如何通过选择<p>
class="medium-widget
success-story-category"
代码数据
我们可以选择格式为的任何类:
div[class="class_name"]
或div[any_style_element="value"]
我们可以选择课程
class="medium-widget
success-story-category"
通过
div[class="medium-widget
success-story-category"]
我们可以通过('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 & 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 & 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)`
我们可以通过
捕捉课堂中的图像 ('div[class="image_class_name"] img')
然后result["href" or "src"]
示例:
假设我必须选择该图像,然后我可以通过此代码获取它:
find_h=browser.find_by_css('h1[class="site-headline"] img')
for i in find_h:
print(i["src"])
接下来的问题是如何选择
所以,如果我有这个HTML代码:
<div class="medium-widget success-story-category last">
<h2 class="widget-title"><span aria-hidden="true" class="icon-success-stories"></span>Friendly & 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 & 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"]:nth-child(1) > ul > li:nth-child(2) > a
请记住div[class="medium-widget success-story-category last"]:nth-child(1)
中的第n个孩子(2)没有选择此类的第二个嵌套div而不是nth-child(2)选择第二个medium-widget success-story-category last
类(如你所见)是两个具有相同名称medium-widget success-story-category last
)的类。
上一个问题的最后答案:
如果有<class_name id="something">
:
然后选择
class_name[id="something"]