【问题标题】:Issue with table structure of webpage when scraping data using beautiful soup使用漂亮的汤抓取数据时网页的表结构问题
【发布时间】:2013-02-20 22:02:27
【问题描述】:

我正在使用以下代码使用漂亮的 Soup 从网页上的表格结构中抓取数据:

# -*- coding: cp1252 -*-
import csv
import urllib2
import sys
import urllib
import time
import mechanize
from bs4 import BeautifulSoup
from itertools import islice


page = urllib2.urlopen('http://www.t-mobile.de/tarifuebersicht-telefonieren-und-surfen/0,23786,25241-_,00.html#grp=0&dev=0').read()
soup = BeautifulSoup(page)
for row in soup('table', {'class' : 'wloCol5'}).tbody('tr'):
    tds = row['td']
    print tds

这段代码给了我AttributeError: 'ResultSet' object has no attribute 'tbody' 错误。我正在为另一个运行没有任何故障的网页使用类似的代码。请告知导致此错误的此代码/网页结构可能存在什么问题。

【问题讨论】:

    标签: python html python-2.7 beautifulsoup


    【解决方案1】:

    调用soup('table', {...}) 发现不止一张表,因此它返回一个类似列表的对象。

    尝试类似:

    for table in soup('table', {...}):
        for tr in table("tr"):
            ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      • 2012-12-08
      • 2022-01-08
      • 1970-01-01
      • 2012-12-16
      • 2022-01-20
      • 1970-01-01
      相关资源
      最近更新 更多