我已经用BeautifulSoup做到了,但是有点麻烦,我想弄清楚是否可以直接用Selenium做到。
假设我有以下HTML,这些HTML在页面源中使用相同的元素但内容不同重复多次:
<div class="person">
<div class="title">
<a href="http://www.url.com/johnsmith/">John Smith</a>
</div>
<div class="company">
<a href="http://www.url.com/company/">SalesForce</a>
</div>
</div>
我需要建立一个字典,每个人的条目如下:
dict = {'name' : 'John Smith', 'company' : 'SalesForce'}
通过执行以下操作,我可以轻松地让Selenium生成每个顶级元素的内容列表:
driver.find_elements_by_class_name('person')
但是,我无法遍历列表,因为上述方法不会将范围/源范围缩小到该元素的内容。
如果我尝试做这样的事情:
people = driver.find_elements_by_class_name('person')
for person in people:
print person.find_element_by_xpath['//div[@class="title"]//a').text
我只是一次又一次地得到相同的名字。
我需要按组进行此操作,因为在我的情况下,遍历整个页面并逐个附加每个标签将不起作用(存在无限滚动,因此效率真的很低)。
有谁知道是否可以直接在Selenium中执行此操作,如果可以,如何执行?
使用find_elements_by_class_name()
让所有块,find_element_by_xpath()
以获得title
与company
每个人:
persons = []
for person in driver.find_elements_by_class_name('person'):
title = person.find_element_by_xpath('.//div[@class="title"]/a').text
company = person.find_element_by_xpath('.//div[@class="company"]/a').text
persons.append({'title': title, 'company': company})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句