【发布时间】:2014-01-21 06:52:09
【问题描述】:
我正在尝试解析一个网站,稍后我将在我的 Django 项目中使用它。为此,我使用 urllib2 和 BeautifulSoup4。但是,我无法得到我想要的。 BeautifulSoup 对象的输出很奇怪。我尝试了不同的页面,它有效(输出正常)。我以为是页面的原因。然后,当我的朋友尝试做同样的事情时,他得到了正常的输出。我无法弄清楚问题。
这是我要解析的website。
这是命令“soup.prettify()”后奇怪输出的示例:
t d B G C O L O R = " # 9 9 0 4 0 4 " w i d t h = " 3 " > i m g S R C = " 1 p . g i f " A L T B O R D E R = " 0 " h e i g h t = " 1 " w i d t h = " 3 " > / t d > \n / t r > \n t r > \n t d c o l s p a n = " 3 " B G C O L O R = " # 9 9 0 4 0 4 " w i d t h = " 6 0 0 " h e i g h t = " 3 " > i m g s r c = " 1 p . g i f " w i d t h = " 6 0 0 " \n h e i g h t = " 1 " > / t d > \n / t r > \n / t a b l e > \n / c e n t e r > / d i v > \n \n p > &n b s p ; &n b s p ; &n b s p ; &n b s p ; / p > \n / b o d y > \n / h t m l >\n </p>\n </body>\n</html>'
【问题讨论】:
-
你能贴出你使用的代码吗?
-
和@Hooked 写的差不多。这是我使用的: import urllib2 from bs4 import BeautifulSoup page = urllib2.open("kafemud.bilkent.edu.tr/monu_tr.html") soup = BeautifulSoup(page) print soup.prettify()
标签: python django beautifulsoup urllib2