硒python返回所有输出

时间:2018-09-27 12:31:03

标签: python

此代码的目的是抓取一堆URL,然后从每个网页中提取标题。 然后在其他函数中使用输出。

代码如下:

from selenium import webdriver


class DataEngine:
    def __init__(self):
        self.urls = open(r"C:\Users\Sayed\Desktop\script\links.txt").readlines()
        self.driver = webdriver.Chrome(r"D:\Projects\Tutorial\Driver\chromedriver.exe")

    def title(self):
        for url in self.urls:
            self.driver.get(url)
            title = self.driver.find_element_by_xpath('//*[@id="leftColumn"]/h1').text
            return title

    def rename(self):
        names = self.title()
        for name in names:
            print(name)


x = DataEngine()
x.rename()

这是我所期望的:

标题(1)

标题(2)

标题(3)

标题(4)

以下是输出:

T

i

t

l

e

1

1 个答案:

答案 0 :(得分:2)

为每个URL建立一个结果列表,当前您仅返回一个(第一个)结果,这就是为什么这样打印的原因:

from selenium import webdriver

class DataEngine:
    def __init__(self):
        self.urls = open(r"C:\Users\Sayed\Desktop\script\links.txt").readlines()
        self.driver = webdriver.Chrome(r"D:\Projects\Tutorial\Driver\chromedriver.exe")

    def title(self):
        titles = []
        for url in self.urls:
            self.driver.get(url)
            title = self.driver.find_element_by_xpath('//*[@id="leftColumn"]/h1').text
            titles.append(title)
        return titles

    def rename(self):
        names = self.title()
        for name in names:
            print(name)


x = DataEngine()
x.rename()