从HTML字符串中删除所有间距

时间:2017-03-02 02:09:33

标签: html python-2.7 python-requests python-collections

我尝试实现删除所有空格和空格字符的代码,然后计算页面中出现的前3个字母数字字符。我的问题是双重的。

1)我用于分割的方法似乎不起作用,我不确定为什么它不能正常工作。据我所知,加入然后拆分应该从html源代码中删除所有空格和空格,但它不是(请参阅下面的amazon示例中的第一个返回值)。

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

你在返回的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

1 个答案:

答案 0 :(得分:0)

要处理空白,您需要使用HTMLParser.HTMLParser及其unescape方法的实例来删除任何原始HTML字符。要计算字符数,您应该查看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])