使用 BeautifulSoup 从 HTML 页面获取内容类型
我试图获取我抓取的页面的字符编码,但在某些情况下它会失败。这就是我正在做的事情:
resp = urllib2.urlopen(request)
self.COOKIE_JAR.extract_cookies(resp, request)
content = resp.read()
encodeType= resp.headers.getparam('charset')
resp.close()
这是我的第一次尝试。但是,如果字符集以 None
类型返回,我会这样做:
soup = BeautifulSoup(html)
if encodeType == None:
try:
encodeType = soup.findAll('meta', {'http-equiv':lambda v:v.lower()=='content-type'})
except AttributeError, e:
print e
try:
encodeType = soup.findAll('meta', {'charset':lambda v:v.lower() != None})
except AttributeError, e:
print e
if encodeType == '':
encodeType = 'iso-8859-1'
我正在测试的页面在标题中包含以下内容:
我希望第一个 try 语句返回一个空字符串,但是我在两个 try 语句上都收到此错误(这就是第二个语句的原因)现在是嵌套的):
'NoneType' 对象没有属性 'lower'
第二个 try 语句有什么问题?我猜第一个也是不正确的,因为它抛出了一个错误,而不仅仅是返回空白。
或者更好是否有一种更优雅的方法来从页面中删除任何特殊字符编码?我想要实现的最终结果是我不关心任何特殊编码的字符。我想删除编码字符并保留原始文本。我可以跳过上述所有内容并告诉 BeautifulSoup 只删除任何编码的内容吗?
I am trying to get the character encoding for pages that I scrape, but in some cases it is failing. Here is what I am doing:
resp = urllib2.urlopen(request)
self.COOKIE_JAR.extract_cookies(resp, request)
content = resp.read()
encodeType= resp.headers.getparam('charset')
resp.close()
That is my first attempt. But if charset comes back as type None
, I do this:
soup = BeautifulSoup(html)
if encodeType == None:
try:
encodeType = soup.findAll('meta', {'http-equiv':lambda v:v.lower()=='content-type'})
except AttributeError, e:
print e
try:
encodeType = soup.findAll('meta', {'charset':lambda v:v.lower() != None})
except AttributeError, e:
print e
if encodeType == '':
encodeType = 'iso-8859-1'
The page I am testing has this in the header:<meta charset="ISO-8859-1">
I would expect the first try statement to return an empty string, but I get this error on both try statements (which is why the 2nd statement is nested for now):
'NoneType' object has no attribute 'lower'
What is wrong with the 2nd try statement? I am guessing the 1st one is incorrect as well, since it's throwing an error and not just coming back blank.
OR better yet is there a more elegant way to just remove any special character encoding from a page? My end result I am trying to accomplish is that I don't care about any of the specially encoded characters. I want to delete encoded characters and keep the raw text. Can I skip all of the above an tell BeautifulSoup to just strip anything that is encoded?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我决定只接受 BeautifulSoup 吐出的任何东西。然后,当我解析文档中的每个单词时,如果无法将其转换为字符串,我就会忽略它。
I decided to just go with whatever BeautifulSoup spits out. Then as I parse through each word in the document, if I can't convert it to a string, I just disregard it.
当尝试确定页面的字符编码时,我认为应该尝试的顺序是:
)
Content-Type: text/html; charset=ISO-8859-1
When attempting to determine the character encoding of a page, I believe the order that should be tried is:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
)Content-Type: text/html; charset=ISO-8859-1
)