Real Time updates on web page (Flask, Python) - real-time-updates

I have created a table in "stat.html" file and I want the table inside should update in real time without refreshing all pages. Please, someone can help me with this. Thank you in advance! I tried ajax, but still did not succeed.
{% extends "layout.html" %}
{% block script %}
{% endblock %}
{% block title %}
Main Page
{% endblock %}
{% block main %}
<!-------------------------------->
<div id="fetch">
<div class="justify-content-sm-center text-left py-3">
<div class="row">
<div class="col-lg-8 col-md-7 col-sm-12 float-left">
<p class="h2">Real Time Production Status</p>
<p class="color-text-secondary">You can manage your production speed, analyze the breakdowns and loads.</p>
</div>
</div>
<div class="row">
<div class="col-lg-8 col-md-7 col-sm-12 float-left">
<div class="btn-group" role="group">
<a id="btnGroupDrop1" type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false" href="#">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-plus-lg" viewBox="0 0 16 16">
<path d="M8 0a1 1 0 0 1 1 1v6h6a1 1 0 1 1 0 2H9v6a1 1 0 1 1-2 0V9H1a1 1 0 0 1 0-2h6V1a1 1 0 0 1 1-1z"/>
</svg> Add
</a>
<ul class="dropdown-menu" aria-labelledby="btnGroupDrop1">
<li><button class="dropdown-item" data-toggle="modal" data-target="#add_section">Add section</button></li>
<li><button class="dropdown-item" data-toggle="modal" data-target="#add_project">Add project</button></li>
<li><button class="dropdown-item" data-toggle="modal" data-target="#add_task">Add task</button></li>
<li><button class="dropdown-item" data-toggle="modal" data-target="#add_subtask">Add subtask</button></li>
</ul>
</div>
</div>
</div>
</div>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link" id="home-tab" data-bs-toggle="tab" data-bs-target="#sections" type="button" role="tab" aria-controls="sections" aria-selected="false">Online</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link active" id="profile-tab" data-bs-toggle="tab" data-bs-target="#projects" type="button" role="tab" aria-controls="projects" aria-selected="true">Historical</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="contact-tab" data-bs-toggle="tab" data-bs-target="#tasks" type="button" role="tab" aria-controls="tasks" aria-selected="false">Tasks</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="contact-tab" data-bs-toggle="tab" data-bs-target="#subtasks" type="button" role="tab" aria-controls="subtasks" aria-selected="false">SubTasks</button>
</li>
</ul>
<!-------------------------------->
<div class="tab-pane fade show active" id="projects" role="tabpanel" aria-labelledby="profile-tab">This is projects tab
<table class="table table-striped table-hover text-left">
<thead class="thead-dark">
<tr>
<th scope="col">Chute No</th>
<th scope="col">ND Boxes</th>
<th scope="col">SD Boxes</th>
<th scope="col">Full Count</th>
<th scope="col">Full Time</th>
<th scope="col">Block Count</th>
<th scope="col">Block Time</th>
</tr>
</thead>
<tbody class="">
{% for Chute in Chutes %}
<tr>
<td data-label="Chute No">{{loop.index}}</td>
<td data-label="ND Boxes">{{ Chutes[loop.index-1][0] }}</td>
<td data-label="SD Boxes">{{ Chutes[loop.index-1][1] }}</td>
<td data-label="Full Count">{{ Chutes[loop.index-1][2] }}</td>
<td data-label="Full TIme">{{ Chutes[loop.index-1][3] }}</td>
<td data-label="Block Count">{{ Chutes[loop.index-1][4] }}</td>
<td data-label="Block Time">{{ Chutes[loop.index-1][5] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock %}
and app.py is here. Are there any ways to update the table Chutes automatically every one second without refreshing all pages?
from flask import Flask, render_template, redirect, jsonify, request
from forms import *
import plc
from threading import Thread
app=Flask(__name__)
#app.route("/",methods=["GET","POST"])
def index():
#print(RD.Chute)
return redirect("/stat")
#Real Time Statistical Data
#app.route("/stat",methods=["GET","POST"])
def stat():
RD=plc.ReadData
RD.plc_read()
return render_template('stat.html',Chutes=RD.plc_read())

Related

cart total only calculating for the first 2 products only in laravel

I have 2 types of products in my app.Products with attribute and products without attribute. I can successfully add the products to cart, but I am having an issue whereby on the part of showing the grand total,only the total of the first 2 products subtotal are calculated and shown.I haven't understood where the bug is. I have tried debugging the code but still can't figure it out. Here is the HTML code that process the grand total price.
//table that shows the cart details and where the subtotal is calculated
<table class="userdatatable table table-striped table-bordered nowrap" style="width:100%; border:2px solid black;">
<thead>
<tr>
<th>Product Name</th>
<th>Price</th>
<th>Image</th>
<th>Quantity</th>
<th>Discount</th>
<th>Total</th>
<th>Remove</th>
</tr>
</thead>
<body>
<?php $total_price=0; ?>
<?php $attributetotal_price=0; ?>
<?php $noattributetotal_price=0; ?>
#foreach($cartitems as $item)
<tr>
#if ($item->product->is_attribute==1)
<?php $attrpric=Merchadise::getdiscountedattrprice($item['product_id'],$item['size']);
?>,
#else
<?php $discountedprice=Merchadise::getdiscountedprice($item['product_id']);
?>
#endif
<td>{{ $item->product->merch_name }}</td>
#if ($item->product->is_attribute==1)
<td>{{ $attrpric['merch_price'] }}</td>
#else
<td>{{ $item->product->merch_price }}</td>
#endif
<td>
<img src="{{ asset ('images/productimages/small/'.$item->product->merch_image) }}" style="width:100px; height:100px;" alt="Product">
</td>
<td>
<button class="itemupdate qtyminus" type="button" data-cartid="{{ $item->id }}">
<i class="fa fa-minus" aria-hidden="true"></i>
</button>
<input data-id={{ $item->id }} class="quantity" min="1" name="quantity[]" value="{{ $item->quantity }}" type="number">
<button class="itemupdate qtyplus" type="button" data-cartid="{{ $item->id }}">
<i class="fa fa-plus" aria-hidden="true"></i>
</button>
</td>
#if ($item->product->is_attribute==1)
<td>sh.{{ $attrpric['discount'] * $item['quantity'] }}</td>
<td>sh.{{ $attrpric['final_price'] * $item['quantity'] }}</td>
#elseif($item->product->is_attribute==0)
<td>{{ ($item->product->merch_price-$discountedprice) * $item['quantity'] }}</td>
<td>{{ $discountedprice * $item['quantity'] }}</td>
#endif
<td>
<a class="btn btn-primary btn-xs" onclick="confirm return('Are you Sure You want to Delete?')" href="{{ route('deletecartitem', $item->id) }}"><i class="fa fa-trash"></i></a>
</td>
</tr>
{{-- show cart total --}}
#if ($item->product->is_attribute==1)
<?php $attributetotal_price=$total_price+($attrpric['final_price'] * $item['quantity'] );
?>
#elseif($item->product->is_attribute==0)
<?php $noattributetotal_price=$total_price+($discountedprice * $item['quantity'] );?>
#endif
#endforeach
</tbody>
</table>
<div class="row">
<div class="col-md-5 ml-auto">
<div class="cart-page-total">
<h2>Cart totals</h2>
<ul class="mb-20">
<li>Coupon Discount
<span class="couponAmount">
#if (Session::has('couponAmount'))
-Sh.{{ Session::get('couponAmount') }}
#else
sh.0
#endif
</span>
</li>
<li>Grand Total
<span class="grand_total">Sh.{{ $attributetotal_price+$noattributetotal_price-Session::get('couponAmount') }}</span>
</li>
</ul>
#auth
Checkout <i class="fa fa-angle-right"></i>
#else
<p>To proceed to checkout create or log in to your account...</p>
<span href="#" data-toggle="modal" data-target="#RegistrationModal" class="btn btn-success btn-block">Create/Login an Account<i class="fa fa-angle-right"></i></span>
#endauth
</div>
</div>
</div>
here is my products migration table
it was just a minor bug.i removed the $total_price
{{-- show cart total --}}
#if ($item->product->is_attribute==1)
<?php $attributetotal_price=$attributetotal_price+($attrpric['final_price'] * $item['quantity'] );
?>
#elseif($item->product->is_attribute==0)
<?php $noattributetotal_price=$noattributetotal_price+($discountedprice * $item['quantity'] );?>
#endif

Call to undefined method App\Models\Karyawan::links() - Pagination in Laravel 8

I have this code in my controller:
public function data_karyawan()
{
return view('data-karyawan', [
"title" => "Data Karyawan",
"karyawan" => Karyawan::with(['role', 'user'])->search(request(['search']))->paginate(10)
]);
}
but when I tried to give pagination on my view like this, there is an error:
{{ $karyawan->links() }}
what did I do wrong?
Blade file:
#extends('layouts.main')
#section('container')
<div class="container-fluid">
<div class="row">
<nav id="sidebarMenu" class="col-md-2 col-lg-2 d-md-block bg-light sidebar collapse">
<div class="position-sticky pt-3">
<div class="d-flex flex-column flex-shrink-0 p-3 bg-light" style="width: 200px;">
<svg class="bi me-2" width="40" height="0"></svg>
<span class="fs-10"><center><img src="/images/logo/selindo4.png" style="width:100px"></center></span>
</a>
<hr>
<ul class="nav nav-pills flex-column mb-auto">
<li class="nav-item">
<a href="/admin" class="nav-link link-dark">
<svg class="bi me-2" width="16" height="40"><use xlink:href="#home"/></svg>
Home
</a>
</li>
<li>
<a href="#" class="nav-link active" aria-current="page">
<svg class="bi me-2" width="16" height="40"><use xlink:href="#people-circle"/></svg>
Data Karyawan
</a>
</li>
<li>
<a href="/admin/data-departemen" class="nav-link link-dark">
<svg class="bi me-2" width="16" height="40"><use xlink:href="#speedometer2"/></svg>
Departemen
</a>
</li>
<li>
<a href="/admin/data-cuti" class="nav-link link-dark">
<svg class="bi me-2" width="16" height="40"><use xlink:href="#table"/></svg>
Summary Cuti
</a>
</li>
</ul>
<hr>
<div class="dropdown">
<a href="#" class="d-flex align-items-center link-dark text-decoration-none dropdown-toggle" id="dropdownUser2" data-bs-toggle="dropdown" aria-expanded="false">
<img src="/images/avatar/avatar-2.png" alt="" width="32" height="40" class="rounded-circle me-2">
<strong>Admin</strong>
</a>
<ul class="dropdown-menu text-small shadow" aria-labelledby="dropdownUser2">
<li><a class="dropdown-item" href="#">Logout</a></li>
</ul>
</div>
</div>
</div>
</nav>
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2" style="position:absolute margin: auto auto"><p></p>Data Karyawan</h1>
</div>
<div class="row">
<div class="col-md-6">
<form action="/admin/data-karyawan">
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Cari Karyawan" name = "search" value="{{ request('search') }}">
<button class="btn btn-outline-primary" type="submit">Search</button>
</div>
</form>
</div>
</div>
<center>
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<hr>
<thead>
<tr>
<th>No.</th>
<th>NIK</th>
<th>Nama</th>
<th>JK</th>
<th>Tempat Lahir</th>
<th>Tanggal Lahir</th>
<th>Alamat</th>
<th>Agama</th>
<th>Jabatan</th>
<th>Divisi</th>
<th>Role</th>
<th>E-mail</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
foreach ($karyawan as $karyawan) { ?>
<tr>
<td>{{ $i++ }}</td>
<td>{{ $karyawan["nik"] }}</td>
<td>{{ $karyawan["nama"] }}</td>
<td>{{ $karyawan["jk"] }}</td>
<td>{{ $karyawan["tempat_lahir"] }}</td>
<td>{{ $karyawan["tanggal_lahir"] }}</td>
<td>{{ $karyawan["alamat"] }}</td>
<td>{{ $karyawan["agama"] }}</td>
<td>{{ $karyawan["jabatan"] }}</td>
<td>{{ $karyawan["departemen"] }}</td>
<td>{{ $karyawan->role->nama_role }}</td>
<td>{{ $karyawan->user->email }}</td>
<td width = '180px'>
Update
Delete
</td>
</tr>
<?php } ?>
</tbody>
</table>
</center>
<table>
<tr> <td>Tambah Data Karyawan</td>
<td width='100px'> </td><td width='100px'> </td><td width='100px'> </td>
<td width='100px'> </td><td width='100px'> </td><td width='100px'> </td>
<td width='100px'> </td><td width='100px'> </td><td width='25px'> </td>
</table>
</div>
</main>
</div>
</div>
{{ $karyawan->links() }}
#endsection
thank you very much.
There is an issue here :
$karyawan as $karyawan
you override the value
In your blade file, you use the same variables for your foreach statement. Thefore overriding one.
Change foreach ($karyawan as $karyawan) to something like foreach ($karyawan as $kar) and then replace all occurances of $karyawan in the below block with $kar
<td>{{ $karyawan["nik"] }}</td>
<td>{{ $karyawan["nama"] }}</td>
<td>{{ $karyawan["jk"] }}</td>
<td>{{ $karyawan["tempat_lahir"] }}</td>
<td>{{ $karyawan["tanggal_lahir"] }}</td>
<td>{{ $karyawan["alamat"] }}</td>
<td>{{ $karyawan["agama"] }}</td>
<td>{{ $karyawan["jabatan"] }}</td>
<td>{{ $karyawan["departemen"] }}</td>
<td>{{ $karyawan->role->nama_role }}</td>
<td>{{ $karyawan->user->email }}</td>
<td width = '180px'>
Update
Delete
</td>

Trix editor doesn't show up when used inside a #if condition

In larave8 livewire I want to use the trix editor to create an bladeview on the livewire component InfoletterCrud is the following
<div class="container ml-auto mr-auto p-10 min-h-screen">
<div class="page-title ">{{ $page_title }}</div>
<div class="button-line flex flex-col">
#if ($isEditMode)
#if ($showMode)
#include('livewire.show-infoletter')
#else
#include('livewire.create-infoletter')
#endif
#else
<div>
<button wire:click="create()" class="button-cancel text-white font-bold py-2 px-4 rounded my-3">Ajouter une info-lettre</button>
</div>
#if (count($infoletters) > 0)
<table class="w-full table-fixed admin-stripped-table">
<thead>
<tr class="w-full">
<th class="w-1/12">Id</th>
<th class="w-10/12">Titre</th>
<th class="w-1/12">Actions</th>
</tr>
</thead>
<tbody>
#foreach ($infoletters as $infoletter)
<tr>
<td>{{ $infoletter->id }}</td>
<td>{{ $infoletter->title }}</td>
<td>
<div style="display:flex">
<button
wire:click="show({{ $infoletter->id }})"
class="fontawesome-icon">
<span data-toggle="tooltip" title="Voir" class="fa fa-paper-plane fa-xs "></span>
</button>
<button
wire:click="edit({{ $infoletter->id }})"
class="fontawesome-icon">
<span data-toggle="tooltip" title="Modifier" class="fa fa-edit fa-xs "></span>
</button>
<button
wire:click="$emit('openModal', 'livewire-modal', {{ json_encode(['type' => 'L\'infolettre', 'ident' => $infoletter->id]) }})"
class="fontawesome-icon">
<span data-toggle="tooltip" title="Supprimer" class=" fa fa-trash fa-xs "></span>
</button>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>,
#else
<p>Il n'y a aucune info-lettre</p>
#endif
#endif
</div>
</div>
When I place the trix component with #livewire('trix') just above the #if (isEditMode) test, it shows up. But it is not what I want because I am not in the Edit mode.
If I place it in the livewire.create-infoletter include, it doesn't show up.
Even when I place it just after the #if (isEditMode) test, it doesn't show up.
What is the trouble
You don't show your component and how you're initializing Trix, so this is just a guess. I'm assuming you're doing it with Alpinejs, and that Alpine has already made its DOM updates by the time the #if condition executes. Try calling your initialization function using $nextTick - see here: https://alpinejs.dev/magics/nextTick.

How to highlight current active menu in Laravel 5.6 app?

I am using following bootstrap menu items with My laravel application,
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<div class="container">
<div class="row">
<div class="col-sm-3 col-md-3">
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne"><span class="glyphicon glyphicon-folder-close">
</span> Content Management</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body">
<table class="table">
<tr>
<td>
<span class="glyphicon glyphicon-pencil text-primary"></span>Category
</td>
</tr>
<tr>
<td>
<span class="glyphicon glyphicon-pencil text-primary"></span>Brand
</td>
</tr>
<tr>
<td>
<span class="glyphicon glyphicon-pencil text-primary"></span>Model
</td>
</tr>
<tr>
<td>
<span class="glyphicon glyphicon-flash text-success"></span>Province
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
and using this menu bar with #include('menubar) with other blade files. now I need highlight current menu item when I visit each pages. how can I do this?
use
{{ request()->route()->getName() }}
or
{{ route()->currentRouteName() }}
then you can do something like
<a class="{{ request()->route()->getName() === "brands.index" ? "active" : "not-active" }}">
You can use this package I've written to do this: https://packagist.org/packages/arcesilas/active-state
Example with your code:
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body">
<table class="table">
<tr>
<td>
<span class="glyphicon glyphicon-pencil text-primary"></span>Category
</td>
</tr>
<tr>
<td>
<span class="glyphicon glyphicon-pencil text-primary"></span>Brand
</td>
</tr>
<tr>
<td>
<span class="glyphicon glyphicon-pencil text-primary"></span>Model
</td>
</tr>
<tr>
<td>
<span class="glyphicon glyphicon-flash text-success"></span>Province
</td>
</tr>
</table>
</div>
</div>
This package also allows you to check a url, a route with given parameters or a query.

How to access only specific item instead of all page with nokogiri [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm trying to scrape some information from a site using Nokogiri.
The site looks like this:
<div class="content">
<article class="mainarticle">
<div class="breadcrump">
<ul></ul>
</div>
<div class="boxstyle">
<h2 style="margin-top:0.1em;">Fahrschulen mit Namen "Hugis Fahrschule AG"</h2>
</div>
<section class="leftrightleft ">
<header>
<div class="borderright borderbottom pads">
<figure class="logoschule">
<a href="fahrlehrerbilder.php?irat_id=14581" onclick="showGallery('irat_gallery',14581);return false;">
<img alt="Bilder Hugis Fahrschule AG" width="110" height="110" src="http://www.fahrlehrervergleich.ch/images/lehrer/small/fe1774780ba2d3aaa3e985ee960bf8.jpg">
<figcaption class="gallerylabel minhide">1 Bild</figcaption>
</a>
</figure>
<div>
<h3>Hugis Fahrschule AG</h3>
<h4 style="padding:0;line-height:1.3em;">Thomas Hugelshofer</h4>
<span class="biger">Thomas Hugelshofer</span>
<p><span>Oerlikon</span><br><span>Schaffhauserstrasse 443</span><br><span>8050</span> <span>Zürich</span></p>
<p>Karte Streetview </p>
<p></p>
</div>
<table class="eintraegemain clearfix">
<tbody>
<tr>
<td>Homepage:</td>
<td><span>www.hugis-fahrschule.ch</span></td>
</tr>
<tr>
<td>E-Mail:</td>
<td><a class="" href="kontakt_user.php?kontakt_id=14581" target="_blank" onclick="showEmailSender(14581);return false;">sekretariat#hugis-fahrschule.ch</a></td>
</tr>
<tr>
<td>Natel:</td>
<td><span>076 684 11 11</span></td>
</tr>
</tbody>
</table>
</div>
</header>
<div class="accordion">
<div class="accordion-section">
<a class="accordion-section-title accordeonfarbe-00" href="#accordion-1-14581"><span class="arrow"></span>Preise für Auto: ab 74.-, Auto Automat: ab 74.-, Motorrad: 90...</a>
<section id="accordion-1-14581" class="secondright accordion-section-content">
<div class="borderleft pads">
<table>
<tbody>
<tr>
<td>Kategorien</td>
<td>Preise CHF</td>
</tr>
<tr>
<td>Auto:</td>
<td>ab 74.-</td>
</tr>
<tr>
<td>Auto Automat:</td>
<td>ab 74.-</td>
</tr>
<tr>
<td>Motorrad:</td>
<td>90.-</td>
</tr>
<tr>
<td>Taxi:</td>
<td>74.-</td>
</tr>
<tr>
<td>Lastwagen:</td>
<td>165 .-</td>
</tr>
<tr>
<td>Lastwagen C1:</td>
<td>165.-</td>
</tr>
<tr>
<td>Auto Anhänger:</td>
<td>112.-</td>
</tr>
<tr>
<td>LKW Anhänger:</td>
<td>195.-</td>
</tr>
<tr>
<td>Car:</td>
<td>195.-</td>
</tr>
<tr>
<td style="font-weight:normal;">Lektionsdauer:</td>
<td style="font-weight:normal;">45 Minuten</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Kurse</td>
<td>Preise CHF</td>
</tr>
<tr>
<td>Verkehrskunde:</td>
<td>150.-</td>
</tr>
<tr>
<td>Nothelferkurs:</td>
<td>120.-</td>
</tr>
<tr>
<td>Motorrad Grundkurs:</td>
<td>160 pro Teil</td>
</tr>
<tr>
<td>Theorie:</td>
<td>Nach Anmeldung</td>
</tr>
</tbody>
</table>
</div>
</section>
</div>
<div class="accordion-section">
<a class="accordion-section-title accordeonfarbe-00" href="#accordion-2-14581"><span class="arrow"></span>Weitere Informationen: Sprachen: Deutsch, Englisch...</a>
<section id="accordion-2-14581" class="thirdleft accordion-section-content">
<div class="borderright pads">
<table id="sb_14581">
<tbody>
<tr>
<td>Sprachen: </td>
<td>Deutsch, Englisch, Serbisch, Albanisch, Türkisch, Polnisch, Italienisch</td>
</tr>
<tr>
<td>Autotyp:</td>
<td>Renauld Megan</td>
</tr>
<tr>
<td>Administration:</td>
<td>CHF 110.- (pauschal)</td>
</tr>
<tr>
<td>Jahrgang:</td>
<td>2015</td>
</tr>
<tr>
<td>Ausbildungsjahr:</td>
<td>2015</td>
</tr>
</tbody>
<tbody></tbody>
</table>
</div>
</section>
</div>
<footer class="clearfix">
<section>
<div class="bordertop pads" id="sbwl_14581">
<div class="ratingoutside">
<ul class="rating">
<li class="icon icon-star-two"></li>
<li class="icon icon-star-two"></li>
<li class="icon icon-star-two"></li>
<li class="icon icon-star-two"></li>
<li class="icon icon-star-two"></li>
<li class="icon icon-star-two"></li>
<li>(12)</li>
</ul>
</div>
<div class="rightbuts"><a class="buttonsmall" href="bewertungen.php?irat=14581" onclick="showBewe(14581);return false;" title="Bewertungen anschauen">Bewertungen anschauen</a>
<a class="buttonsmall" href="lernen/index.php?bewe_irat=14581">Fahrlehrer bewerten</a>
</div>
<p class="clicks minhide clearfix"> 30-Tage-Statistik : Inserat <span class="num_imp">14069</span> Mal angezeigt, Homepage <span class="num_click">228</span> Mal angeklickt. (sponsored)</p>
</div>
</section>
</footer>
</div>
</section>
<section class="leftrightleft ">
<header>
<div class="borderright borderbottom pads">
<div>
<h3>Hugis Fahrschule AG</h3>
<h4 style="padding:0;line-height:1.3em;">Thomas Hugelshofer</h4>
<span class="biger">Thomas Hugelshofer</span>
<p><span>8106</span> <span>Adlikon b. Regensdorf</span></p>
</div>
<table class="eintraegemain clearfix">
<tbody></tbody>
</table>
</div>
</header>
<div class="accordion">
<div class="accordion-section">
<a class="accordion-section-title accordeonfarbe-00" href="#accordion-1-13695"><span class="arrow"></span>Preise für Auto: 84.-, Motorrad: 90.-, Taxi: 95.-, Lastwagen:...</a>
<section id="accordion-1-13695" class="secondright accordion-section-content">
<div class="borderleft pads">
<table>
<tbody>
<tr>
<td>Kategorien</td>
<td>Preise CHF</td>
</tr>
<tr>
<td>Auto:</td>
<td>84.-</td>
</tr>
<tr>
<td>Motorrad:</td>
<td>90.-</td>
</tr>
<tr>
<td>Taxi:</td>
<td>95.-</td>
</tr>
<tr>
<td>Lastwagen:</td>
<td>165.-</td>
</tr>
<tr>
<td>Lastwagen C1:</td>
<td>165.-</td>
</tr>
<tr>
<td>Auto Anhänger:</td>
<td>112.-</td>
</tr>
<tr>
<td>LKW Anhänger:</td>
<td>195.-</td>
</tr>
<tr>
<td>Car:</td>
<td>195.-</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Kurse</td>
<td>Preise CHF</td>
</tr>
<tr>
<td>Verkehrskunde:</td>
<td>150.-</td>
</tr>
<tr>
<td>Motorrad Grundkurs:</td>
<td>160.-</td>
</tr>
</tbody>
</table>
</div>
</section>
</div>
<div class="accordion-section">
<a class="accordion-section-title accordeonfarbe-00" href="#accordion-2-13695"><span class="arrow"></span>Weitere Informationen: Sprachen: Deutsch, Englisch...</a>
<section id="accordion-2-13695" class="thirdleft accordion-section-content">
<div class="borderright pads">
<table id="sb_13695">
<tbody>
<tr>
<td>Sprachen: </td>
<td>Deutsch, Englisch, Französisch, Italienisch, Polnisch, Türkisch</td>
</tr>
<tr>
<td>Autotyp:</td>
<td>VW, BMW, MAN, SETRA, </td>
</tr>
</tbody>
<tbody></tbody>
</table>
</div>
</section>
</div>
<footer class="clearfix">
<section>
<div class="bordertop pads" id="sbwl_13695">
<div class="ratingoutside">
<ul class="rating">
<li class="icon icon-star-two"></li>
<li class="icon icon-star-two"></li>
<li class="icon icon-star-two"></li>
<li class="icon icon-star-two"></li>
<li class="icon icon-star-two"></li>
<li class="icon icon-star-two"></li>
<li>(12)</li>
</ul>
</div>
<div class="rightbuts"><a class="buttonsmall" href="bewertungen.php?irat=13695" onclick="showBewe(13695);return false;" title="Bewertungen anschauen">Bewertungen anschauen</a>
<a class="buttonsmall" href="lernen/index.php?bewe_irat=13695">Fahrlehrer bewerten</a>
</div>
<p class="clicks minhide clearfix"> 30-Tage-Statistik : Inserat 307 Mal angezeigt. </p>
</div>
</section>
</footer>
</div>
</section>
<ul class="paginator"></ul>
<section class="texttabs clearfix">
<p>
Fahrschulen in der Schweiz: Fahrschulen und Fahrlehrer für Auto, Motorrad, Lastwagen, Taxi und Boot. Verkehrskunde (VKU), Nothelferkurs, Motorradgrundkurs. Informationen zu Strassenverkehrsamt, Lernfahrausweis, Fahrprüfung, WAB hier im Schweizer Fahrlehrervergleich.
</p>
<p>
Der Schweizer Fahrlehrervergleich gibt Auskunft über Fahrschulen und Fahrlehrer, die in folgenden Kategorien unterrichten: Auto, Motorrad, Lastwagen, Taxi und Boot. Ebenfalls werden Angaben über Verkehrtheorie, Motorradgrundkurs, Nothelferkurs und Theorietraining gemacht. Finden Sie eine Auto-, Motorrad- oder Lastwagen-Fahrschule, Informationen zu Ausbildung, Grundkurs, Theoriekurs, Schleuderkurs und Autoprüfung. Fahrschule Fahrschulen Fahrlehrer Zürich Bern Basel Luzern Aargau Schweiz. Der Schweizer Fahrlehrervergleich beinhaltet über 5000 Fahrschulen und Fahrlehrer aus der ganzen Schweiz.
</p>
</section>
</article>
<aside>
<h2>Fahrlehrer und Fahrschulen finden</h2>
<section class="filterbox regone boxstyle">
<h3>Fahrschulen </h3>
<ul>
<li><a title="Fahrschule Hugis Fahrschule AG" href="http://www.fahrlehrervergleich.ch/fahrschule-Hugis-Fahrschule-AG-Z%C3%BCrich-fahrschule-14581.htm">Hugis Fahrschule AG</a></li>
<li><a title="Fahrschule Hugis Fahrschule AG" href="http://www.fahrlehrervergleich.ch/fahrschule-Hugis-Fahrschule-AG-Adlikon+b.+Regensdorf-fahrschule-13695.htm">Hugis Fahrschule AG</a></li>
</ul>
</section>
<section class="filterbox regone boxstyle">
<h3>Sprachauswahl</h3>
<ul>
<li><a hreflang="de" href="http://www.fahrlehrervergleich.ch" title="Schweizer Fahrlehrervergleich">Schweizer Fahrlehrervergleich</a></li>
<li><a hreflang="fr" href="http://auto-ecole.comparatif.ch" title="Comparatif d'auto-écoles suisses">Comparatif d'auto-écoles suisses</a></li>
<li><a hreflang="it" href="http://autoscuola.comparazione.ch" title="Confronto delle autoscuole svizzere">Confronto delle autoscuole svizzere</a></li>
<li><a hreflang="en" href="http://driving-instructor.fahrlehrervergleich.ch" title="Swiss Driving Instructors">Swiss Driving Instructors</a></li>
</ul>
</section>
<section class="filterbox regone boxstyle">
<h3>Siehe auch...</h3>
<ul>
<li>
<a href="http://www.zahnarztvergleich.ch" title="Schweizer Zahnarztvergleich">
Schweizer Zahnarztvergleich </a>
</li>
<li>
<a href="http://www.treuhandvergleich.ch" title="Schweizer Treuhandvergleich">
Schweizer Treuhandvergleich </a>
</li>
<li>
<a href="http://www.anwaltvergleich.ch" title="Schweizer Anwaltvergleich">
Schweizer Anwaltvergleich </a>
</li>
<li>
<a href="http://www.coiffeurvergleich.ch" title="Schweizer Coiffeurvergleich">
Schweizer Coiffeurvergleich </a>
</li>
</ul>
</section>
</aside>
</div>
My code looks like this:
doc = Nokogiri::HTML(page)
doc.css(".leftrightleft").each do |item|
title = item.at_css("h3 a").text
address = item.at_css(".biger+ p , h3+ p")
address.search('br').each do |n|
n.replace(" ")
end
address = address.text
puts "-----"
puts title
puts "Address:"
puts address
puts "Course Types:"
item.css("table+ table tr+ tr").each do |type_price|
if type_price.css("td")[0] && type_price.css("td")[1] != nil
course_type = type_price.css("td")[0].text
course_type.gsub!(":", "")
price = type_price.css("td")[1].text
puts "\t #{course_type}: #{price}"
end
end
end
The problem is that when I do item.css("table+ table tr+ tr").each do |type_price| it's doing it for all items below that one and not only for that item in specific.
Can someone explain me what I'm doing wrong?
I've figured out a way to solve this.
Instead of making the loop like this item.css("table+ table tr+ tr").each do |type_price|, that makes it loop to the all page from item until the end of the page, I'm doing it like this item.at_css("table+ table tbody").css("tr+ tr").each.
With this solution I'm getting just the first table element and then I loop through the rest of the rows and it works.
I took a look and it seems that Nokogiri is jumbling the elements due to weird markup.
Switching to HTML5 is probably the best bet:
require 'nokogumbo'
doc = Nokogiri::HTML5.get(uri)
I should really say that libxml is doing the jumbling rather than Nokogiri.

Resources