2017-03-02 1 views
0

모든 공백 및 공백 문자를 제거하는 코드를 구현하려고하고 페이지에서 발생하는 상위 3 개의 영숫자를 계산합니다. 제 질문은 두 가지입니다.HTML 문자열에서 모든 공백 제거

1) 분할을 위해 사용하는 방법이 작동하지 않는 것 같습니다. 왜 작동하지 않는지 잘 모르겠습니다. 내 지식에 합류하면 스프레드는 html 소스 코드에서 공백과 공백을 모두 제거해야하지만 그렇지 않습니다 (아래의 아마존 예제에서 반환 된 첫 번째 값 참조).

2)는 most_common 작업에 대단히 익숙하지 그리고 난 "http://amazon.com"나는 다음과 같은 출력을 얻을 내 코드를 테스트 할 때 :

The top 3 occuring alphanumeric characters in the html of http://amazon.com 
: [(u' ', 258), (u'a', 126), (u'e', 126)] 

U 자 반환 된 most_common에 무엇을 의미 하는가를 (3) 값?

내 현재 코드 :

import requests 
import collections 


url = raw_input("please eneter the url of the desired website (include http://): ") 

response = requests.get(url) 
responseString = response.text 

print responseString 

topThreeAlphaString = " ".join(filter(None, responseString.split())) 

lineNumber = 0 

for line in topThreeAlphaString: 
    line = line.strip() 
    lineNumber += 1 

topThreeAlpha = collections.Counter(topThreeAlphaString).most_common(3) 

print "The top 3 occuring alphanumeric characters in the html of", url,": ", topThreeAlpha 
+0

이는 유니 코드 문자열을 의미합니다. 공백'' ".join (...)'을 사용하여'join()'하고 그냥 빈 문자열''"로 조인하면됩니다. .join (...)' – AChampion

답변

0

공백 돌봐, 당신은 주위에 거짓말을 어떤 원시 HTML 문자를 제거하는 HTMLParser.HTMLParser하고 unescape 방법의 인스턴스를 사용할 수 있습니다. 문자를 계산하려면 collections.Counter을 확인해야합니다.

import requests 
from collections import Counter 
from HTMLParser import HTMLParser 

response = requests.get('http://www.example.com') 
responseString = response.text 

parser = HTMLParser() 
c = Counter(''.join(parser.unescape(responseString).split()) 

print(c.most_common()[:3])