El *_*rce 1 python http urllib2
如何从 Python 的网页中仅下载text/html/javascript?
我正在尝试获取有关博客作者撰写的文本的一些统计信息。只需要文本,我想通过避免下载图像等来提高我的程序速度。
我能够将文本与 HTML 标记语言分开。所以我的目的主要是避免在网页中下载额外的内容(如图像、.swf 等)
到目前为止,我使用:
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()
Run Code Online (Sandbox Code Playgroud)
但我不确定我是否在做正确的事情(即仅下载文本)
Python BeautifulSoup 是解析网页的最佳选择之一
import bs4
import urllib.request
webpage=str(urllib.request.urlopen(link).read())
soup = bs4.BeautifulSoup(webpage)
print(soup.get_text())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5379 次 |
| 最近记录: |