Python/Selenium组合多个列表并通过可视tex显示

2024-09-26 18:20:49 发布

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

我正在自学如何使用Python/Selenium。你知道吗

我有一个代码块,列出了网页上所有可用的衬衫,然后列出了所有可用的颜色,但我希望衬衫/颜色显示在一起,而不是两个单独的列表。你知道吗

到目前为止我得到的是:

shirts = driver.find_elements_by_xpath("""//*[@id="container"]/article/div/h1/a""")
for shirt in shirts:
    text = shirt.text
    print text
colors = driver.find_elements_by_xpath("""//*[@id="container"]/article/div/p/a""")
for color in colors:
    text = color.text
    print text

以上代码的结果如下:

Contrast Zip Up Hooded Sweatshirt
Contrast Zip Up Hooded Sweatshirt
Contrast Zip Up Hooded Sweatshirt
Contrast Zip Up Hooded Sweatshirt
Contrast Zip Up Hooded Sweatshirt
Contrast Zip Up Hooded Sweatshirt
Contrast Crewneck
Contrast Crewneck
Contrast Crewneck
Contrast Crewneck
Contrast Crewneck
Contrast Crewneck
Jet Sleeve Zip Up Hooded Sweatshirt
Jet Sleeve Zip Up Hooded Sweatshirt
Jet Sleeve Zip Up Hooded Sweatshirt
Jet Sleeve Zip Up Hooded Sweatshirt
Jet Sleeve Zip Up Hooded Sweatshirt
Navy
Red
Heather Grey
Dark Green
Light Brown
Black
Heather Grey
Light Brown
Black
Dark Green
Red
Navy
Violet
Light Pine
Black
White
Navy

Tags: 代码text颜色ziplightblackupjet
1条回答
网友
1楼 · 发布于 2024-09-26 18:20:49

使用^{}方法:

shirts = driver.find_elements_by_xpath("//*[@id='container']/article/div/h1/a")
colors = driver.find_elements_by_xpath("//*[@id='container']/article/div/p/a")
for shirt, color in zip(shirts, colors):
    shirt_text = shirt.text
    color_text = color.text
    print shirt_text, color_text

相关问题 更多 >

    热门问题