Skip to main content
 首页 » 编程设计

python之使用 Python 2.7 和 selenium 抓取数据并对其进行排序

2024年12月31日7Terrylee

我正在尝试使用 selenium 和 python 2.7 抓取网站中的数据。这是我要抓取的数据中的代码

<textarea>let, either, and, have, rather, because, your, with, other, that, neither, since, however, its, will, some, own, than, should, wants, they, got, may, what, least, else, cannot, like, whom, which, who, why, his, these, been, had, the, all, likely, their, must, our</textarea> 

我需要插入所有这些词来列出和排序它。现在这是我的进步

wordlist = [] 
data = browser.find_element_by_tag_name("textarea") 
words = data.get_attribute() 
wordlist.append(words) 
print words 
print wordlist.sort() 

任何帮助或线索都会对我有用

请您参考如下方法:

请注意,wordlist.sort() 不会返回列表,而只是对现有列表进行排序,因此您可能需要做/p>

wordlist.sort() 
print wordlist 

或尝试以下代码以获得所需的输出

data = driver.find_element_by_tag_name("textarea") 
words = data.get_attribute('value') 
sorted_list = sorted(words.split(', ')) 
print sorted_list 
# ['all,', 'and,', 'because,', 'been,', 'cannot,', 'either,', 'else,', 'got,', 'had,', 'have,', 'his,', 'however,', 'its,', 'least,', 'let,', 'like,', 'likely,', 'may,', 'must,', 'neither,', 'other,', 'our', 'own,', 'rather,', 'should,', 'since,', 'some,', 'than,', 'that,', 'the,', 'their,', 'these,', 'they,', 'wants,', 'what,', 'which,', 'who,', 'whom,', 'why,', 'will,', 'with,', 'your,']