Получить название от <a>с целого Selenium Python - PullRequest
0 голосов
/ 26 февраля 2019

Я пытаюсь взять некоторые адреса со страницы.Я хочу извлечь только заголовок, этот заголовок - адрес, который я хочу.

Вот HTML

<li class="brd-bottom-1">
   <a href="#" title="AVDA. A. RENDIC ESQ. AVDA. P. A. CERDA" class="position-relative vue-accordion-link-stations">
     <i class="fas fa-map-marker-alt position-absolute s-display-none"></i> 
     <i class="fas fa-caret-down position-absolute s-display-none"></i> 
     <div class="font-size-16 marg-bottom-5">Avda. a. rendic esq. avda. p. a. cerda 
        <i class="fas fa-caret-down marg-left-5 m-display-none l-display-none"></i></div>  

Вот мой код Python

    driver = webdriver.Chrome(r"C:\Users\heju8004\Documents\Archivos de Python\chromedriver.exe")
    driver.get("https://ww2.copec.cl/stations?check=punto")
    for i in range(15):
        driver.get("https://ww2.copec.cl/stations?check=punto")
        driver.find_element_by_xpath("//*[@id='root']/div[1]/div/ul/li[1]/a").click()
        string = "//*[@id='root']/div[1]/div/ul/li[1]/ul/li/span[{}]".format(i+1)     
        driver.find_element_by_xpath(string).click()
        time.sleep(2)
        resultSet = driver.find_element_by_xpath("//*[@id='root']/div[2]/div[2]/div[2]/ul")
        options = resultSet.find_elements_by_tag_name("li")
        for option in options:
            otraOption = option.find_element_by_xpath("//a")
            print(otraOption.title)

Ответы [ 3 ]

0 голосов
/ 26 февраля 2019

Итак, я сделал простую модификацию вашего кода.Просто перебирая «li», через который вы хотите перебрать, и захватывая все теги внутри него.Обратите внимание, что я использую 'find_elements_by_tag_name', обратите внимание на 's', чтобы сделать множественное число для захвата ВСЕХ тегов.

  driver = webdriver.Chrome(r"C:\Users\heju8004\Documents\Archivos de Python\chromedriver.exe")
        driver.get("https://ww2.copec.cl/stations?check=punto")
        for i in range(15):
            driver.get("https://ww2.copec.cl/stations?check=punto")
            driver.find_element_by_xpath("//*[@id='root']/div[1]/div/ul/li[1]/a").click()
            string = "//*[@id='root']/div[1]/div/ul/li[1]/ul/li/span[{}]".format(i+1)     
            driver.find_element_by_xpath(string).click()
            time.sleep(2)
            resultSet = driver.find_element_by_xpath("//*[@id='root']/div[2]/div[2]/div[2]/ul")
            options = resultSet.find_elements_by_tag_name("li")
            for option in options:
                otraOption = option.find_elements_by_tag_name("a")
                titles = otraOption.get_attribute('title')
                print(titles)

Как только у вас есть все теги, вы можете извлечь заголовки из тегов.

0 голосов
/ 26 февраля 2019

Самым быстрым и простым решением является использование пакета requests без Selenium.Вы можете получить все станции для всех регионов за один запрос:

import requests

url = 'https://ww2.copec.cl/stations/get_stations.json?pagoclick_filter=true&geohash=66jc8&limit=2000'
response = requests.get(url)

stations = response.json()["stations"]
for station in stations:
    print("region: %s, title: %s" % (station["region"], station["title"]))

Поля, которые вы можете получить для каждой станции:

"id": 36578,
"company": "copec",
"title": "AV. ANDRES BELLO N\u00ba 2722",
"description": "AV. ANDRES BELLO N\u00ba 2722",
"email": "60109@escopec.cl",
"commune": "LAS CONDES",
"region": "METROPOLITANA",
"phone": "56-2-2311097",
"uuid": "f4976280-277e-4d30-8a0f-03de7ad9d3bb",
"latitude": "-33.4137958011",
"longitude": "-70.6044896114",
"distance": "8218km",
"decimal_distance": 8218.08,
"copec_id": 60109,
"user_distance": "5km",
"user_decimal_distance": 4.65,
"diesel": 611.0,
"kerosene": null,
"gasoline_93": 784.0,
"gasoline_95": 822.0,
"gasoline_97": 849.0,
"gnc": null,
"glp": null,
"pronto": false,
"punto": true,
"dpaso": false,
"cajero": true,
"bano": false,
"lavamax": true,
"zervo": false,
"voltex": false,
"cupon": true,
"tct": false,
"lanpass": true,
"taxiamigo": true,
"mobil": false,
"pagoclick": true,
"autoservicio": false,
"tae": false,
"renova": true,
"lavamax_autoservicio": false,
"lavamax_automatico": true,
"glp_service": false,
"gnc_service": false,
"full_zervo": false,
"mixta_zervo": false,
"tct_premium": false,
"fusion": true,
"wifi": true,
"pantallas": true,
"bluemax_surtidor": false,
"ip": true,
"bluemax_bidon": false,
"agenda_online": false,
"chiletur": true,
"pronto_ruta": false,
"sala_descanso": false,
"nueva_imagen": true,
"telemedicion": true,
"lub": false,
"cyclist": true,
"camaras": false,
"promae": false,
"lavado_manual": false,
"lavado_automatico": false,
"lubricantes": false,
"tienda": false,
"nave_de_lubricacion": false,
"farmacia": false,
"tarjeta_de_credito": false,
"carga_electrica": false
0 голосов
/ 26 февраля 2019

Поиск по тегу:

driver = webdriver.Chrome(r"C:\Users\heju8004\Documents\Archivos de Python\chromedriver.exe"
# ...
for i in range(15):
    # ... etc
    current_title = driver.find_element_by_tag_name('a').get_attribute('title')
    # ...

Редактировать:

Как упоминалось в комментариях, лучший способ - сначала извлечь все элементы <a>, а затем извлечь то, что вам нужно:

driver = webdriver.Chrome(r"C:\Users\heju8004\Documents\Archivos de Python\chromedriver.exe"
# ... navigate to page
a_elements = driver.driver.find_elements_by_tag_name('a') # notice plural 'elements'
titles = []
for element in a_elements:
    try:
        titles.append(element.get_attribute('title')
    except Exception as e:
        print(f'No element found for {element} with error: {e}')
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...