如何使用python / selenium获取彼此相邻而不是彼此相邻的列表项

时间:2018-09-21 09:41:19

标签: python selenium

下面是我正在使用的代码

<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://d3js.org/d3.v3.min.js"></script>

<div id="chart"></div>

我得到的输出是:

enter image description here

但是实际上我想要这样的想法: 赛车俱乐部,5和下面的这支下一支球队及其号码

我在这里怎么了?

1 个答案:

答案 0 :(得分:0)

创建了一个功能concat_func并定义了2个空列表,我在其中添加数据,然后将这些列表合并为一个

例如

a = ['x', 'z']
b = ['y', 'w']
concat_func = lambda x, y: str(x) + "," + str(y)     
# then
output = list(map(concat_func, a, b))
# output would be ['x,y', 'z,w'] 

尝试一下:

driver = webdriver.Chrome("C:/Users/gdebr/Desktop/chromedriver.exe")
driver.get("https://int.soccerway.com/national/argentina/primera-division/20182019/regular-season/r47779/")
wait = WebDriverWait(driver, 10)
elm = wait.until(EC.presence_of_element_located((By.XPATH,"""//*[@id="page_competition_1_block_competition_tables_7_1_4"]""")))
elm.click()
time.sleep(2)

concat_func = lambda x, y: str(x) + "," + str(y)

with open ('Argentinie1Form.txt', 'a', encoding='utf-8') as r:
    teams_list_append = []
    MatchesPlayed_list_append = []
    teams_list = driver.find_elements_by_xpath("//table[contains(@id, 'page_competition_1_block_competition_tables_7_block_competition_form_table_1_table')]/tbody/tr/td[3]/a[1]")
    for items in teams_list:
        teams_list_append.append(items.get_attribute('title'))
        # r.write(items.get_attribute('title')+'\n')
    MatchesPlayed_list = driver.find_elements_by_xpath("//table[contains(@id, 'page_competition_1_block_competition_tables_7_block_competition_form_table_1_table')]/tbody/tr/td[4]")
    for matches in MatchesPlayed_list:
        MatchesPlayed_list_append.append(matches.text)
        # r.write(matches.text+'\n')
    output = list(map(concat_func, teams_list_append, MatchesPlayed_list_append))

    for i in output:
        r.write(i + '\n')