我正在使用BeautifulSoup4
的HTML頁面。 html
文件確實包含頂部的request headers
信息,我如何過濾掉?從原始文本Python忽略標題文本BeautifulSoup
這裏是html
文件片段
WARC/1.0
WARC-Type: response
WARC-Date: 2012-02-17T03:07:46Z
WARC-TREC-ID: clueweb12-0206wb-51-29582
WARC-Record-ID: <urn:uuid:546b127c-040e-4dee-a565-3a3f6683f898>
Content-Type: application/http; msgtype=response
Content-Length: 29032
HTTP/1.1 200 OK
Cache-Control: private
Connection: close
Date: Fri, 17 Feb 2012 03:07:48 GMT
Content-Length: 28332
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Set-Cookie: chkvalues=ClmZLoF4xnHoBwiZnWFzYcCMoYB/fMxYfeeJl/zhlypgwivOzw6qnVBRWzf8f19O; expires=Wed, 15-Aug-2012 02:07:48 GMT; path=/
Set-Cookie: previous-category-id=11; expires=Fri, 17-Feb-2012 03:27:48
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="ctl00_headTag"><title>
我想提取<html></html>
沒有別的之間的文本。當我嘗試做這樣的事情。
with codecs.open(file, 'r', 'utf-8', errors='ignore') as f:
contents = f.read()
soup = BeautifulSoup(contents, "lxml")
for script in soup.find_all(["script", "style"]): # to remove script style tags
script.extract()
try:
raw_text = soup.find('html').text.lower()
except AttributeError:
pprint('{0} file is empty'.format(file))
在raw_text
它填補了 "WARC/1.0\r\nWARC-Type: response\r\nWARC-Date: 2012-02-17T03:07:46Z....
類似的信息,意味着它添加標題變爲raw_text
。
如何從原始文本中刪除標題。