【发布时间】:2014-09-17 00:05:05
【问题描述】:
我需要使用 python 从网站上抓取文本。我已经安装了 BeautifulSoup 4、HTML Requests 和 NLTK,但我似乎不知道如何抓取。
我真的需要一个简单的 sn-p 代码,我可以插入任何 URL 并获取纯文本。我正在尝试从this website获取它
【问题讨论】:
-
考虑提出更多信息和代码。
标签: python html screen-scraping
我需要使用 python 从网站上抓取文本。我已经安装了 BeautifulSoup 4、HTML Requests 和 NLTK,但我似乎不知道如何抓取。
我真的需要一个简单的 sn-p 代码,我可以插入任何 URL 并获取纯文本。我正在尝试从this website获取它
【问题讨论】:
标签: python html screen-scraping
BeautifulSoup 可以轻松提取页面中的所有文本。以下是提取<body>...</body> 部分内的文本的示例。
import urllib
from bs4 import BeautifulSoup
from contextlib import closing
url = 'https://developer.valvesoftware.com/wiki/Hammer_Selection_Tool'
with closing(urllib.urlopen(url)) as h:
soup = BeautifulSoup(h.read())
print soup.body.get_text()
【讨论】: