试图爬网一个网站,但没有得到<body>

2024-10-05 12:20:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我在玩,想给自己发一封电子邮件时,在论坛上出现了一个新帖子,但当我打开网址时urllib.urlopen我得到了网页,但没有网页正文。有人能告诉我为什么会这样吗?我怎样才能得到尸体?在

def loadUrl(adress): 
  adress = urllib.unquote(adress)
  print("Loading " + adress)
  socket =urllib.urlopen(adress)
  html = socket.read()
  socket.close()
  soup = BeautifulSoup(html)
  return soup


soup = loadUrl("http://de.pokerstrategy.com/forum/thread.php?threadid=498111")

Tags: 网页defhtmlsocketurllib论坛帖子urlopen
3条回答

编辑对不起,我不知道你已经发布了你想要检索的网址。我得到的答复和你一样,但不知道为什么。我在javascript中看不到任何东西,正如我在下面建议的那样。在

我测试了你的代码,它似乎运行得很好。可能您尝试检索的页面通过javascript或类似的方式生成body元素。在本例中,我相信您可以使用selenium之类的东西来模拟浏览器。在

另外,我建议使用Pyquery。在

from pyquery import PyQuery
d = PyQuery("http://de.pokerstrategy.com/forum/thread.php?threadid=498111")

print d("body").html()

我已经成功地将BeautifulSoupurllib2结合使用,例如:

from urllib2 import urlopen
...
html = urlopen(...)
soup = BeautifulSoup(html)

相关问题 更多 >

    热门问题