您好我正在寫一個代碼來從文本文章使用Python獲取標籤。我工作很好,但是當我嘗試排序這些詞python給我'TypeError'。我環顧四周,這個錯誤,但無法弄清楚怎麼做 這裏是我的代碼:TypeError:一元操作符錯誤 - :'type';同時提取關鍵字形式的文本文件
上述問題,現在解決了,但現在我想從這個文本第二十得到最常用的單詞和外if語句我申請如果單詞在commen.txt文件中存在,則不應將其包含在word_dic中。但是當我運行這段代碼時,它給了我那些已經存在於common.txt文件中的單詞 注意:Common.txt包含大部分使用英語的單詞。如(A,中,中,下),我不希望在word_dic
#Loading Libraries
import urllib
import os
from urllib.parse import urlparse
from urllib.parse import urljoin
import urllib.request
from bs4 import BeautifulSoup
id= 1
url='http://scitechdaily.com/new-technique-reveals-internal-characteristics-of-photonic-crystals/'
def getKeywords(articletext):
common = open('C:\\Users\\Hassan Raza\\Desktop\\Mozilla tech article\\common.txt').read().split('\n')
word_dict = {articletext:float}
word_list = articletext.lower().split()
for word in word_list:
if word not in common:
if word not in word_dict:
word_dict[word] = 1
if word in word_dict:
word_dict[word] +=1
sorteddata = Counter(word_dict).most_common()
#print(sorted(word_dict.items(),key=lambda kv: (-kv[1], kv[0]),reverse=True))
def GetArticles(url,id):
file = open('C:\\Users\\Hassan Raza\\Desktop\\Mozilla tech article\\Article'+'.txt', 'w')
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
html = urllib.request.urlopen(req).read()
soup = BeautifulSoup(html,"html.parser")
title= soup.find_all('h1', {'class','title'})
for titles in title:
print(titles.text)
text = soup.find_all('div' , {'class', 'entry'})
for pg in text:
articletext=(pg.text.encode('utf8'))
getKeywords(articletext)
file.close()
GetArticles(url,id)
這裏包括是我在錯誤的條件正在逐漸:
Line 18 in print(sorted(word_dict.items(),key=lambda kv: (-kv[1], kv[0]),reverse=True)) TypeError: bad operant type for unary -: 'type'
幫我解決這個問題:)
請不要張貼鏈接到屏幕截圖。將錯誤複製並粘貼爲文本。 –