使用来自html结构的索引来抓取数据的问题

时间:2013-02-07 05:07:05

标签: python html python-2.7 html-parsing beautifulsoup

我正在从30-40个网页抓取以下html结构中的数据,例如https://www.o2.co.uk/shop/tariffs/sony/xperia-z-purple/

    <td class="monthlyCost">£13<span>.50</span></td>
              <td class="phoneCost">£479.99</td>
              <td><span class="lowLight">24 Months</span></td>
    <td>50</td>
    <td>Unlimited</td>
    <td class="dataAllowance">100MB</td>
    <td class="extras">

我正在编制索引以清除td标记下的数据,这些标记没有类似 50 &amp; 无限,对应于数据集中的分钟和文本列。我正在使用的代码是:

        results       = tariff_link_soup.findAll('td', {"class": None})
        minutes = results[1]
        texts = results[2]
        print minutes,texts

所有这些30-40个网页链接都出现在https://www.o2.co.uk/shop/phones/网页上,我发现这个网页上的链接访问了这些链接然后到达了所需的网页,所有这些最终设备网页都遵循相同的结构。

问题:我希望只获得分钟和文字值,如50&amp;无限制,200&amp;所有网页的第2和第3索引都是无限制的。当我打印数据时,我仍然得到一些其他值。 500MB100MBdataAllowance类和td标记下的值。我使用类作为None属性,但仍无法获取所需数据。我检查了html结构,它在各页之间是一致的。

请帮助我解决这个问题,因为我无法理解这种异常现象。

更新:我正在使用的整个Python代码:

urls  =  ['https://www.o2.co.uk/shop/phones/',
          'https://www.o2.co.uk/shop/phones/?payGo=true']

plans =  ['Pay Monthly','Pay & Go']
for url,plan in zip(urls,plans):

    if plan == 'Pay Monthly':
        device_links = parse().direct_url(url,'span', {"class": "model"})

        for device_link in device_links:
            device_link.parent['href'] = urlparse.urljoin(url, device_link.parent['href'])            
            device_link_page           = urllib2.urlopen(device_link.parent['href'])
            device_link_soup           = BeautifulSoup(device_link_page)

        dev_names = device_link_soup.find('h1')
        for devname in dev_names:

            tariff_link = device_link_soup.find('a',text = re.compile('View tariffs'))

            tariff_link['href'] = urlparse.urljoin(url, tariff_link['href'])

            tariff_link_page    = urllib2.urlopen(tariff_link['href'])
            tariff_link_soup    = BeautifulSoup(tariff_link_page)
            dev_price     = tariff_link_soup.findAll('td', {"class": "phoneCost"})
            monthly_price = tariff_link_soup.findAll('td', {"class": "monthlyCost"})
            tariff_length = tariff_link_soup.findAll('span', {"class": "lowLight"})
            data_plan     = tariff_link_soup.findAll('td', {"class": "dataAllowance"})
            results       = tariff_link_soup.xpath('//td[not(@class)]')
            print results[1].text
            print results[2].text

1 个答案:

答案 0 :(得分:0)

我最终使用以下代码来解决我的问题:

    for row in tariff_link_soup('table', {'id' : 'tariffTable'})[0].tbody('tr'):                                                                                                                                                               
        tds = row('td')                                                                                                                                                   
        #print tds[0].text,tds[1].text,tds[2].text,tds[3].text,tds[4].text,tds[5].text
        monthly_prices = unicode(tds[0].text).encode('utf8').replace("£","").replace("FREE","0").replace("Free","0").strip()
        dev_prices     = unicode(tds[1].text).encode('utf8').replace("£","").replace("FREE","0").replace("Free","0").strip()
        tariff_lengths = unicode(tds[2].text).encode('utf8').strip()
        minutes        = unicode(tds[3].text).encode('utf8').strip()
        texts          = unicode(tds[4].text).encode('utf8').strip()
        data           = unicode(tds[5].text).encode('utf8').strip()
        device_names   = unicode(dev_names).encode('utf8').strip()

我在这里逐行访问所需的数据,使用存在数据的表格结构。我将连续出现的所有元素都添加到我的数据中所需的名称。