我在这里找到了答案,我将对此进行解释,以便对其他程序员有所帮助:
第一件事browser.find_by_css_selector()
不起作用,我使用了 find_by_css 方法,效果很好,所以我更喜欢find_by_css
method.
如何选择<p>
通过选择来标记数据class="medium-widget
success-story-category"
我们可以选择格式为的任何类:
div[class="class_name"]
or div[any_style_element="value"]
我们可以选择班级class="medium-widget
success-story-category"
by
div[class="medium-widget
success-story-category"]
我们可以选择
tag by ('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)')
or
当 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"])
接下来的问题是如何选择
标签:我们可以选择
使用 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 & 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>
然后我们可以选择任何的href链接
通过使用
div[class="medium-widget success-story-category last"]:nth-child(1) > ul > li:nth-child(2) > a
请记住 nth-child(2) 在div[class="medium-widget success-story-category last"]:nth-child(1)
不选择此类的第二个嵌套 div 而是选择 nth-child(2) 选择第二个medium-widget success-story-category last
类(如您所见,有两个具有相同名称的类medium-widget success-story-category last
) .
最后一个问题的最后答案:
如果有<class_name id="something">
:
然后选择喜欢
class_name[id="something"]