首页 > 解决方案 > scrape data and sort it using Python 2.7 and selenium

问题描述

i'm trying to scrape data in a website using selenium and python 2.7. Here is the code from the data that i want to scrape

<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>

i need to insert all that words to list and sort it. for now this is my progres

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

any help or clue would be useful for me

标签: pythonweb-scrapingselenium-chromedriver

解决方案


请注意,wordlist.sort()它不会返回list,而只是对已存在的 list 进行排序,因此您可能需要这样做

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,']

推荐阅读