Python Селен превращает второй элемент в списке в переменную - PullRequest
1 голос
/ 19 апреля 2020

Я удаляю данные с веб-сайта, и мне нужно вставить каждый текст элемента li в отдельную строку таблицы MySQL.

Источник

 https://printcopy.info/?mod=erc&brand=Kyocera&model=TASKalfa+2460ci&page=1

Эти коды выводит весь текст из каждого li

parent = driver.find_elements_by_class_name("ercRow")
for link in parent:
    links = link.find_elements_by_tag_name('li')
    for l in links:
        print(l.text)

Results

Code:...
Description:...
Cause:...
Remedy:...

Теперь мне нужно превратить каждый li в переменную on, чтобы я мог вставить их в таблицу mysql следующим образом :

id |   code       |     desc       |    caus      |   reme
 1    code...           desc...         cause...      reme..
 2    code...           desc...         cause...      reme..
 3    code...           desc...         cause...      reme..

Я пытался:

parent = driver.find_elements_by_class_name("ercRow")
for link in parent:
    links = link.find_elements_by_tag_name('li')
    for l in links:
        print(l[0].text)
        print(l[1].text)
        print(l[2].text)
        print(l[3].text)

Ошибка:

    print(l[0].text)
        TypeError: 'WebElement' object is not subscriptable

Любая помощь будет принята с благодарностью. Спасибо.

1 Ответ

1 голос
/ 19 апреля 2020

Нет необходимости использовать Selenium, поскольку желаемый контент доступен в исходном коде без включенного javascript, поэтому мы можем использовать BeautifulSoup, то есть:

from bs4 import BeautifulSoup as bs
import requests

mod = "erc"
brand = "Kyocera"
model = "TASKalfa+2460ci"

# get total pages
u = f"https://printcopy.info/?mod={mod}&brand={brand}&model={model}"
soup = bs(requests.get(u).text, "html5lib")

# find the total number of pages
pages = int([i.findAll('option') for i in soup.findAll('select', {"id": "selectNumPages"} )][0][-1].text) + 1
# print(pages)

for page in range(1, pages):
    u = f"https://printcopy.info/?mod={mod}&brand={brand}&model={model}&page={page}"
    soup = bs(requests.get(u).text, "html5lib")
    ercRow = soup.findAll("ul", {"class": "ercRow"})
    for ul in ercRow:
        lis = ul.findAll("li")
        code = lis[0].text.strip("Code: ")
        description = lis[1].text.strip("Description: ")
        causes = lis[2].text.strip("Causes: ")
        remedy = lis[3].text.strip("Remedy: ")
        print(code, description, causes, remedy, sep="\n")
        # insert the values on db...

Выход:

C0070
FAX PWB incompatible detection error
Abnormal detection of FAX control PWB incompatibility in the initial communication with the FAX control PWB, any normal communication command is not transmitted.
1 Checking the FAX PWB The incompatible FAX PWB is installed. Install the FAX PWB for the applicable model. 2 Firmware upgrade The FAX firmware is faulty. Reinstall the FAX firmware. 3 Replacing the main PWB The main PWB is faulty. Replace the main PWB.
C0100
Backup memory device error
An abnormal status is output from the flash memory.
1 Resetting the main power The flash memory does not operate properly. Turn off the power switch and unplug the power plug. After 5s passes, reconnect the power plug and turn on the power switch. 2 Checking the main PWB The connector or the FFC is not connected properly. Or, the wire, FFC, the PWB is faulty. Clean the terminal of the connectors on the main PWB, reconnect the connector of the wire, and reconnect the FFC terminal. If the wire or the FFC is faulty, repair or replace them. If not resolved, replace the main PWB.

...
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...