-1
如何从Python中的网页下载只有 text/html/javascript?仅从Python中的网页内容下载文本
我想了解一些关于博客作者撰写的文本的统计信息。只需要文本,我想通过避免下载图像等来提高我的程序速度。
我可以将文本从HTML标记语言中分离出来。所以,我的本意主要是避免在网页中下载aditional的内容(如图片,瑞士法郎等)
到目前为止我用:
user_agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3'
headers = {'User-Agent': user_agent}
req = urllib2.Request(url, None, headers)
response = urllib2.urlopen(req, timeout=60)
content_type = response.info().getheader('Content-Type')
if 'text/html' in content_type:
return response.read()
但我不知道如果我做了(只有IE下载文本)正确的事情
我会建议看[要求](http://docs.python-requests.org/en/latest /)库f或者更容易处理HTTP请求。 – Ben