【发布时间】:2016-11-12 18:08:54
【问题描述】:
我是网页抓取领域的绝对新手,现在我想从网页中提取可见文本。我在网上找到一段代码:
import urllib2
from bs4 import BeautifulSoup
url = "http://www.espncricinfo.com/"
web_page = urllib2.urlopen(url)
soup = BeautifulSoup(url , "lxml")
print (soup.prettify())
对于上面的代码,我得到以下结果:
/usr/local/lib/python2.7/site-packages/bs4/__init__.py:282: UserWarning: "http://www.espncricinfo.com/" looks like a URL. Beautiful Soup is not an HTTP client. You should probably use an HTTP client like requests to get the document behind the URL, and feed that document to Beautiful Soup.
' that document to Beautiful Soup.' % decoded_markup
<html>
<body>
<p>
http://www.espncricinfo.com/
</p>
</body>
</html>
无论如何,我可以获得更具体的结果以及代码发生了什么问题。对不起,我一无所知。
【问题讨论】:
-
你能读懂错误吗?我们不是在这里以更易读的格式为您反刍信息
-
还有你说的提取可见文本是什么意思,你能举个网站的例子,你可以提取什么样的文本。
-
对不起!它基本上是一个用户警告,指出我提供了一个带有 URL 的漂亮汤,我可能应该使用 HTTP 客户端。 Beautiful Soup 只会接受 URL 后面的文档。
-
现在看看你的代码,看看是什么原因造成的。
-
@RohinGopalakrishnan 我最终想从特定新闻链接(比如 Udayavaani)中提取卡纳达语段落。
标签: python web-scraping beautifulsoup urllib2