Создание цикла для последовательного открытия ссылок

avatar
Resultados Oficiais
9 августа 2021 в 05:01
73
1
0

Этот сайт:
https://int.soccerway.com/international/europe/european-championships/c25/

EUROPE

European Championship
                     2020
                         Group Stage
                         Final Stages
EC Qualification
WC Qualification Europe
UEFA Nations League
Baltic Cup

Он имеет две ссылки в левом боковом меню в Group Stage и Final Stages:

https://int.soccerway.com/international/europe/european-championships/2020/group-stage/r38188/
https://int.soccerway.com/international/europe/european-championships/2020/s13030/final-stages/

У меня получается собирать ссылки, но когда я пытаюсь открыть страницы одну за другой, он останавливается только на первой ссылке и не открывает вторую, что мне нужно изменить?

url = "https://int.soccerway.com/international/europe/european-championships/c25/"

driver.get(url)
links_level_2 = driver.find_elements_by_xpath("//ul[contains(@class,'level-2')]/li/a")
for link_level_2 in links_level_2:
    level_2 = link_level_2.get_attribute("href")
    driver.get(level_2)
Источник

Ответы (1)

avatar
Ram
9 августа 2021 в 06:17
0

Это легко сделать с помощью beautifulsoup.

Поскольку вы четко не упомянули, что такое links, я предполагаю, что вы пытаетесь извлечь links под ul, глядя на наш код.

Вот код, использующий beautifulsoup. Это дает вам ссылки под <ul> из обеих упомянутых ссылок.

import bs4 as bs
import requests

urls = ['https://int.soccerway.com/international/europe/european-championships/2020/group-stage/r38188/', 'https://int.soccerway.com/international/europe/european-championships/2020/s13030/final-stages/']
headers = {"User-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"}

#Code to get the ULs
for url in urls:
    resp = requests.get(url, headers=headers)
    soup = bs.BeautifulSoup(resp.text, 'lxml')
    ls = soup.find('ul', class_='level-2').findAll('li')
    for i in ls:
        print(i.find('a')['href'])
    print('\n')

/international/europe/european-championships/2020/group-stage/r38188/
/international/europe/european-championships/2020/group-stage/group-a/g10136/
/international/europe/european-championships/2020/group-stage/group-b/g10137/
/international/europe/european-championships/2020/group-stage/group-c/g10138/
/international/europe/european-championships/2020/group-stage/group-d/g10139/
/international/europe/european-championships/2020/group-stage/group-e/g10140/
/international/europe/european-championships/2020/group-stage/group-f/g10141/
/international/europe/european-championships/2020/s13030/final-stages/


/international/europe/european-championships/2020/group-stage/r38188/
/international/europe/european-championships/2020/s13030/final-stages/