如何使用python/selenium使列表项彼此相邻而不是彼此下方

2024-06-25 23:08:31 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我正在使用的代码

with open ('Argentinie1Form.txt', 'w', encoding='utf-8') as r:
    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)


with open ('Argentinie1Form.txt', 'a', encoding='utf-8') as r:
    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:
        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:
        r.write(matches.text+'\n')

我得到的结果是:

enter image description here

但事实上我想要这样的想法: 赛车俱乐部,5号及以下是下一队的号码

我到底做错什么了


Tags: txtidtablesasdriverwithpagetable
1条回答
网友
1楼 · 发布于 2024-06-25 23:08:31

创建了一个function 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')

相关问题 更多 >