我想使用python selenium在仪表板中创建解析程序

时间:2016-01-20 13:18:58

标签: python parsing selenium

我是python selenium初学者。 我想从仪表板中创建解析程序。

这是我的python代码

此代码可以首先处理标记。但它不能去第二个,第三个标签。 我尝试'anchor_element [1] .click()',但这是一个错误的代码。 我不知道哪里出错了

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()

url = 'http://www.naver.com'

driver.get(url)

id_input = driver.find_element_by_name('id')
id_input.send_keys('jaeyeon93')

pass_input = driver.find_element_by_name('pw')
pass_input.send_keys('wodus0101')

log_button = driver.find_element_by_class_name('btn_login')

log_button.click()

time.sleep(2)
url2 = 'cafe.naver.com/startdata'

driver.get(url2)
#go to dashborad.
driver.find_element_by_xpath(".//*[@id='menuLink0']").click()
#change iframe. dashboard source code is in iframe
board_iframe = driver.find_element_by_id('cafe_main')
driver.switch_to_frame(board_iframe)


anchor_element = driver.find_elements_by_tag_name('a')
anchor_element.click()

这是我想要使用循环

单击和解析的代码
<iframe name='cafe_main' id='cafe_main'>
<form name='ArticleList'>
    <table class="board-box">
        <tbody>
            <tr>
                <td></td>
                <td class='board-list'>
                    <span>
                        <span class='aaa'>
                            <a class="m-tcol-c" onmouseover="" onclick="return dynamicParamLink('/ArticleRead.nhn?clubid=28279768&page=1&boardtype=L&articleid=91&referrerAllArticles=true', event);" href="/ArticleRead.nhn?clubid=28279768&page=1&boardtype=L&articleid=91&referrerAllArticles=true">API Store</a>
                        </span>
                    </span>
                </td>
            </tr>

            <tr>
                <td></td>
                <td class='board-list'>
                    <span>
                        <span class='aaa'>
                            <a class="m-tcol-c" onmouseover="" onclick="return dynamicParamLink('/ArticleRead.nhn?clubid=28279768&page=1&boardtype=L&articleid=90&referrerAllArticles=true', event);" href="/ArticleRead.nhn?clubid=28279768&page=1&boardtype=L&articleid=90&referrerAllArticles=true">WikiDocs - Online books platform</a>
                        </span>
                    </span>
                </td>
            </tr>

            <tr>
                <td></td>
                <td class='board-list'>
                    <span>
                        <span class='aaa'>
                            <a class="m-tcol-c" onmouseover="" onclick="return dynamicParamLink('/ArticleRead.nhn?clubid=28279768&page=1&boardtype=L&articleid=90&referrerAllArticles=true', event);" href="/ArticleRead.nhn?clubid=28279768&page=1&boardtype=L&articleid=90&referrerAllArticles=true">Third Href</a>
                        </span>
                    </span>
                </td>
            </tr>

            <tr>
                <td></td>
                <td class='board-list'>
                    <span>
                        <span class='aaa'>
                            <a class="m-tcol-c" onmouseover="" onclick="return dynamicParamLink('/ArticleRead.nhn?clubid=28279768&page=1&boardtype=L&articleid=90&referrerAllArticles=true', event);" href="/ArticleRead.nhn?clubid=28279768&page=1&boardtype=L&articleid=90&referrerAllArticles=true">4th Href</a>
                        </span>
                    </span>
                </td>
            </tr>
        </tbody>
    </table>
</form>
</iframe>

请。帮助我。

2 个答案:

答案 0 :(得分:0)

它只执行一次。要做得更多,你需要在某个地方使用for循环

编辑:

下面:

for(your_range):
    anchor_element = driver.find_elements_by_tag_name('a')
    anchor_element.click()

答案 1 :(得分:0)

您可以使用以下代码获取所有锚元素,而无需在代码中进行如此多的查询:

driver.get(url2)
###### New code ########
links = driver.find_elements_by_xpath(".//a")
for link in links:
    link.click()