我对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]
有人能指出正确的处理方法吗?你知道吗
fullListNames
和listName
都是字符串。在fullListNames += listName
行中,您将连接这两个字符串。然后将所有名称串联成一个长字符串。你知道吗您只需要将
fullListNames
初始化为一个空列表:fullListNames = []
。然后将listName
附加到这个列表:fullListNames.append(listName)
。你知道吗相关问题 更多 >
编程相关推荐