【问题标题】:Issue with scraping data using indexing from html structure使用来自 html 结构的索引来抓取数据的问题
【发布时间】:2013-02-08 21:00:41
【问题描述】:

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

    <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 标签下的数据,这些标签没有像 50Unlimited 这样的类,对应于数据集中的 Minutes 和 texts 列。我正在使用的代码是:

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

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

问题:我希望只获得分钟和文本值,例如 50 和无限、200 和无限,并且出现在所有网页的第 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

【问题讨论】:

  • 但是我刚刚被告知 xpath 方法不能用于 Beautiful soup。

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


【解决方案1】:

我终于用下面的代码解决了我的问题:

    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()

我在这里使用数据所在的表格结构逐行访问所需的数据。我正在获取连续存在的所有元素,并将名称分配给我的数据中所需的元素。

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 2013-09-29
    • 2021-09-01
    相关资源
    最近更新 更多