如何使用CSS选择器获取所有href并查找元素?

时间:2019-04-17 23:43:38

标签: python loops selenium for-loop

我正在使用下面的代码,我想获取元素中的所有Href不仅是第一个Href,而且当我使用它时,我只会得到第一个Href

communities = driver.find_elements_by_css_selector('div.coin-link-row:nth-child(4) > div:nth-child(2)>a')

sources = driver.find_elements_by_css_selector('div.coin-link-row:nth-child(5) > div:nth-child(2)>a')
with open('example2.csv', 'a') as myFile:
        writer = csv.writer(myFile)
        for community, source in zip(communities, sources):
             writer.writerow([ community.get_attribute('href'), source.get_attribute('href')])

我想从中收集链接的html代码,我想通过css选择器收集所有href,我需要一些东西,例如在css选择器路径中的字母a之前全部添加单词

<?xml version="1.0" encoding="UTF-8"?>
<div class="d-flex flex-column mx-3 mx-sm-5 mx-md-0">
    <div class="coin-link-row mb-md-0">
        <span class="coin-link-title mr-2">Market Cap</span>
        <div class="d-flex flex-wrap">
            <span class="coin-tag mr-1 text-white bg-dark font-weight-bold">Rank #1</span>
        </div>
    </div>
    <div class="coin-link-row mb-md-0">
        <span class="coin-link-title mr-2">Website</span>
        <div class="d-flex flex-wrap">
            <a target="_blank" rel="nofollow" class="coin-link-tag" href="https://www.bitcoin.org">
                <span class="">bitcoin.org</span>
            </a>
        </div>
    </div>
    <div class="coin-link-row mb-md-0">
        <span class="coin-link-title mr-2">Explorers</span>
        <div class="d-flex flex-wrap">
            <a target="_blank" rel="nofollow" class="coin-link-tag" href="https://blockchair.com/bitcoin/blocks">
                <span class="">blockchair.com</span>
            </a>
            <a target="_blank" rel="nofollow" class="coin-link-tag" href="https://blockchain.info/">
                <span class="">blockchain.info</span>
            </a>
            <a target="_blank" rel="nofollow" class="coin-link-tag" href="https://live.blockcypher.com/btc/">
                <span class="">live.blockcypher.com</span>
            </a>
            <a target="_blank" rel="nofollow" class="coin-link-tag" href="https://bitcoinblockexplorers.com/">
                <span class="">bitcoinblockexplorers.com</span>
            </a>
            <a target="_blank" rel="nofollow" class="coin-link-tag" href="https://btc.tokenview.com/">
                <span class="">btc.tokenview.com</span>
            </a>
        </div>
    </div>
    <div class="coin-link-row mb-md-0">
        <span class="coin-link-title mr-2">Community</span>
        <div class="d-flex flex-wrap">
            <a target="_blank" rel="nofollow" class="coin-link-tag" href="https://reddit.com/r/Bitcoin">
                <i class="fab mr-sm-1 fa-reddit" />
                <span class="d-none d-sm-inline">Reddit</span>
            </a>
            <a target="_blank" rel="nofollow" class="coin-link-tag" href="https://twitter.com/btc">
                <i class="fab mr-sm-1 fa-twitter" />
                <span class="d-none d-sm-inline">Twitter</span>
            </a>
            <a target="_blank" rel="nofollow" class="coin-link-tag" href="https://www.facebook.com/134466763256650">
                <i class="fab mr-sm-1 fa-facebook" />
                <span class="d-none d-sm-inline">Facebook</span>
            </a>
            <a target="_blank" rel="nofollow" class="coin-link-tag" href="https://bitcointalk.org/">
                <span class="">bitcointalk.org</span>
            </a>
        </div>
    </div>
    <div class="coin-link-row mb-md-0">
        <span class="coin-link-title mr-2">Source Code</span>
        <div class="d-flex flex-wrap">
            <a target="_blank" rel="nofollow" class="coin-link-tag" href="https://github.com/bitcoin">
                <i class="fab mr-sm-1 fa-github" />
                <span class="d-none d-sm-inline">Github</span>
            </a>
        </div>
    </div>
    <div class="coin-link-row mb-md-0">
        <span class="coin-link-title mr-2">Tags</span>
        <div class="d-flex flex-wrap">
            <span class="coin-tag mr-1">Cryptocurrency</span>
        </div>
    </div>
</div>

0 个答案:

没有答案