Beautiful Soup 4和Python的解析错误

2024-09-27 00:20:36 发布

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

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

我使用BeautifulSoup4来解析页面。 这是我一直写的代码:

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调用在页面加载后加载新行。。。在

有人能帮我找出错误吗?在


Tags: importhttp页面urllibchprintsoupbeautifulsoup
1条回答
网友
1楼 · 发布于 2024-09-27 00:20:36

这对我有用

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

告诉我情况

相关问题 更多 >

    热门问题