【问题标题】:scrape data and sort it using Python 2.7 and selenium使用 Python 2.7 和 selenium 抓取数据并对其进行排序
【发布时间】:2019-05-19 19:25:30
【问题描述】:

我正在尝试使用 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()

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

【问题讨论】:

  • 您的问题能具体一点吗?例如,是否发生了意外错误?
  • 如果我的问题不够清楚,我很抱歉。当我尝试运行该代码时打印它工作正常的单词但是当我在排序后尝试打印单词列表时,输出为无

标签: python web-scraping selenium-chromedriver


【解决方案1】:

注意wordlist.sort() 不返回列表,而只是排序存在的列表,所以你可能需要这样做

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

【讨论】:

  • 这个工作正常。介意告诉我sorted_list = sorted(words.split(', ')) 是做什么的?但是当我运行我的代码时我遇到了问题。这是输出[u'able', u'after', u'almost', u'any', u'because', u'been', u'could', u'did', u'does', u'else', u'ever', u'for', u'get', u'had', u'has', u'have', u'her', u'hers', u'his', u'however', u'into', u'its', u'let', u'like', u'likely', u'may', u'most', u'off', u'other', u'rather', u'say', u'says', u'than', u'that', u'the', u'then', u'these', u'they', u'tis', u'too', u'wants', u'were', u'while', u'whom', u'with', u'would', u'yet', u'you', u'your']
  • sorted 接收列表作为参数并返回新的 排序 列表。如果你的意思是你的问题是每个字符串前面的u,那么它就是not really a problem。这就是 unicode 字符串在 Python 2.x 中的样子
【解决方案2】:

我能够使用以下代码重现您的问题:

words = ["hello", "world", "abc", "def"]

wordlist = []
wordlist.append(words)

print(words)
print(wordlist.sort())

这个输出:

['hello', 'world', 'abc', 'def']
None

我认为这是您遇到的问题。

为了解决这个问题,我做了两件事: 1) wordlist.append(words) for wordlist = words.copy() - 这会复制数组,而不是将数组附加到数组元素和 2) 将 wordlist.sort() 移出打印函数 - sort 不返回任何内容,并且是就地排序,因此不返回任何内容。

所以,完整的更新示例是:

words = ["hello", "world", "abc", "def"]

wordlist = []
wordlist = words.copy()
wordlist.sort()

print(words)
print(wordlist)

现在输出排序列表(根据您的需要):

['hello', 'world', 'abc', 'def']
['abc', 'def', 'hello', 'world']

【讨论】:

  • 我从网站打印单词时得到的输出不在列表形式中。看起来这就是问题所在。输出就像这样also, could, since, let, least, their, among, have, was, may, across, just, been, our, whom, some
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多