使用Selenium和Python将文本转换为数组

2024-04-16 15:16:15 发布

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

我对python和selenium还很陌生,我想知道如何从网页中提取一组文本并将其输入到数组中。目前,我所拥有的是一个方法,它不使用数组,而是使用字符串并不整齐地显示它。你知道吗

# returns a list of names in the order it is displayed
def gather_names(self):  
    fullListNames = ""
    hover_names = self.browser.find_elements_by_xpath("//div[contains(@class, 'recent-names')]") #xpath to the names that will need to be hovered over
    for names in hover_names:
        self.hover_over(names) #hover_over is a method which takes an xpath and will then hover over each of those elements
        self.wait_for_element("//div[contains(@class, 'recent-names-info')]", 'names were not found') #Checking to see if it is displayed on the page; otherwise, a 'not found' command will print to console
        time.sleep(3) #giving it time to find each element, otherwise it will go too fast and skip over one
        listName = names.find_element_by_xpath("//div[contains(@class, 'recent-names-info')]").text #converts to text
        fullListNames += listName #currently adding every element to a string
return fullListNames

这个的输出看起来像

name_on_page1name_on_page2name_on_page3

名称之间没有任何空格(如果找不到将其合并到数组中的方法,我想更改)。你知道吗

当我尝试使fullListNames成为一个数组时,我在获取字符串的每个字符和类似的输出时遇到了问题

[u'n', u'a', u'm', u'e', u'_', u'o', u'n']....

最好,我需要一个

[name1, name2, name3]

有人能指出正确的处理方法吗?你知道吗


Tags: theto方法selfnamesisonit
2条回答

fullListNameslistName都是字符串。在fullListNames += listName行中,您将连接这两个字符串。然后将所有名称串联成一个长字符串。你知道吗

您只需要将fullListNames初始化为一个空列表:fullListNames = []。然后将listName附加到这个列表:fullListNames.append(listName)。你知道吗

You are using string concatnation here ..
fullListNames += listName // thats the problem please refer below code i have replaced the selenium components you can add it depending upon your requirement.
also fullListNames should be an array
fullListNames =[]


def gather_names1():
    fullListNames = []
    #hover_names = self.browser.find_elements_by_xpath("//div[contains(@class, 'recent-names')]") #xpath to the names that will need to be hovered over
    # #for names in hover_names:
    #     self.hover_over(names) #hover_over is a method which takes an xpath and will then hover over each of those elements
    #     self.wait_for_element("//div[contains(@class, 'recent-names-info')]", 'names were not found') #Checking to see if it is displayed on the page; otherwise, a 'not found' command will print to console
    #     time.sleep(3) #giving it time to find each element, otherwise it will go too fast and skip over one
    #     listName = names.find_element_by_xpath("//div[contains(@class, 'recent-names-info')]").text #converts to text
    for i in range(10):

        listName="user"+str(i)
        fullListNames.append(listName)#currently adding every element to a string
    print fullListNames
    return fullListNames

gather_names1()
i got the below output
['user0', 'user1', 'user2', 'user3', 'user4', 'user5', 'user6', 'user7', 'user8', 'user9']

相关问题 更多 >