Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium将列表中的第二项转换为变量_Python_Selenium - Fatal编程技术网

Python Selenium将列表中的第二项转换为变量

Python Selenium将列表中的第二项转换为变量,python,selenium,Python,Selenium,我从一个网站上抓取数据,我需要将每个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_elemen

我从一个网站上抓取数据,我需要将每个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)
结果

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

任何帮助都将不胜感激。谢谢。

无需使用
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.

...

哇,太谢谢你了。最后一个问题,如果页面范围超过20,该怎么办?其他一些型号有37页,有些有20页……等等。欢迎您:)您可以键入任何您想要的号码,即
range(37)
。如果我的回答帮助你,请考虑接受是正确的答案,并给它1 +,谢谢!完成。再次感谢。我只是想看看是否有办法让它自动获取页数,而不是我手动给它打电话。谢谢。我已经更新了答案,可以动态添加要报废的总页数。我绝对不会;),确保捕捉到它可能抛出的错误,请尝试
,除了
。德国劳埃德船级社!