【问题标题】:Parsing error with Beautiful Soup 4 and PythonBeautiful Soup 4 和 Python 的解析错误
【发布时间】:2014-04-21 19:39:18
【问题描述】:

我需要从这个网站获取房间列表:http://www.studentroom.ch/en/dynasite.cfm?dsmid=106547

我正在使用 Beautiful Soup 4 来解析页面。 这是我写到现在的代码:

from bs4 import BeautifulSoup
import urllib

pageFile = urllib.urlopen("http://studentroom.ch/dynasite.cfm?dsmid=106547")
pageHtml = pageFile.read()
pageFile.close()

soup = BeautifulSoup("".join(pageHtml))

roomsNoFilter = soup.find('div', {"id": "ImmoListe"})

rooms = roomsNoFilter.table.find_all('tr', recursive=False)

for room in rooms:
    print room
    print "----------------"

print len(rooms)

现在我试图只获取表格的行。 但我只得到 7 行而不是 78 行(或 77 行)。

起初我很难接受我只收到部分 html,但我打印了整个 html 并且我正确接收它。 页面加载后没有加载新行的 ajax 调用...

有人可以帮我找出错误吗?

【问题讨论】:

  • 你为什么用"".join(pageHtml)pageHtml不是已经是大字符串了吗?
  • 运行您提供的代码后,我收到了78
  • 我换了电脑,工作正常..在另一个仍然无法工作..还是谢谢。

标签: python html-parsing beautifulsoup


【解决方案1】:

这对我有用

soup = BeautifulSoup(pageHtml)
div = soup.select('#ImmoListe')[0]
table = div.select('table > tbody')[0]
k = 0
for room in table.find_all('tr'):
    if 'onmouseout' in str(room):
        print room
        k = k + 1
print "Total ",k

让我知道状态

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 2018-11-26
    相关资源
    最近更新 更多