0
所以我從HTML頁面中列出了元素並計算了這些元素的頻率。但我只需要一些特定的元素,如「bb」和「nw」。所以我不知道他們在列表中的位置,我不知道如何將它們與其他元素分開。Python:獲取特定的列表元素
這是我到目前爲止的代碼:
from bs4 import BeautifulSoup
import urllib2
import re
import operator
from collections import Counter
from string import punctuation
source_code = urllib2.urlopen('https://de.wikipedia.org/wiki/Liste_von_Angriffen_auf_Fl%C3%BCchtlinge_und_Fl%C3%BCchtlingsunterk%C3%BCnfte_in_Deutschland/bis_2014')
html = source_code.read()
soup = BeautifulSoup(html, "html.parser")
text = (''.join(s.findAll(text=True))for s in soup.findAll('a'))
c = Counter((x.rstrip(punctuation).lower() for y in text for x in y.split()))
bb,nw=operator.itemgetter(1,2)(c.most_common())
print(bb,nw)
謝謝您的幫助和任何提示。
你是什麼意思的,你只需要特定的元素?你的意思是你需要他們的頻率嗎? – Peaceful