获取具有相同路径的元素的信息
Getting information of elements having same paths
我有一条这样的路
<div class="accordion-group">
...
<span class="country-container u-text-ellipsis">
此容器包含有关国家/地区及其相对值的信息。
<a itemprop="url" class="country-name country-name--link" href="/top-websites/Switzerland"><Switzerland"</a>
我想从这里增加国家。
我还有与这个国家相关的其他信息,我想提取这些信息。
路径是
<div class="acccordion-group">
...
<span class="traffic-share-valueNumber js-countValue">3.95%</span>
容器中包含多个信息,路径同上。示例:
我试过这个:
driver.find_element_by_xpath('//span[@class="traffic-share-valueNumber js-countValue"]').get_attribute('textContent')
它只适用于一个值。
你能解释一下如何获取有关国家及其百分比的所有信息吗?如果需要,很乐意提供更多信息。
我想要这个预期输出 (df):
Country Percentage
Italy 87.81%
Switzerland 3.95%
Germany 2.75%
... ...
您可以获得所有国家
countries = driver.find_elements_by_xpath("//a[@class='country-name country-name--link']")
以及
的百分比
percentages = driver.find_elements_by_xpath("//span[@class='traffic-share-valueNumber js-countValue']")
现在您可以遍历列表,获取它们的文本并打印或放入 df。
要打印结果,您可以执行以下操作:
for country, percentage in zip(countries, percentages):
print(country.text + '\t' + percentage.text)
我有一条这样的路
<div class="accordion-group">
...
<span class="country-container u-text-ellipsis">
此容器包含有关国家/地区及其相对值的信息。
<a itemprop="url" class="country-name country-name--link" href="/top-websites/Switzerland"><Switzerland"</a>
我想从这里增加国家。
我还有与这个国家相关的其他信息,我想提取这些信息。
<div class="acccordion-group">
...
<span class="traffic-share-valueNumber js-countValue">3.95%</span>
容器中包含多个信息,路径同上。示例:
我试过这个:
driver.find_element_by_xpath('//span[@class="traffic-share-valueNumber js-countValue"]').get_attribute('textContent')
它只适用于一个值。
你能解释一下如何获取有关国家及其百分比的所有信息吗?如果需要,很乐意提供更多信息。 我想要这个预期输出 (df):
Country Percentage
Italy 87.81%
Switzerland 3.95%
Germany 2.75%
... ...
您可以获得所有国家
countries = driver.find_elements_by_xpath("//a[@class='country-name country-name--link']")
以及
的百分比percentages = driver.find_elements_by_xpath("//span[@class='traffic-share-valueNumber js-countValue']")
现在您可以遍历列表,获取它们的文本并打印或放入 df。
要打印结果,您可以执行以下操作:
for country, percentage in zip(countries, percentages):
print(country.text + '\t' + percentage.text)