2015-12-15 2 views
5

Ich möchte einen Tisch von der Ligue 1 Fußball-Website kratzen. Insbesondere die Tabelle, die Informationen zu Karten und Schiedsrichtern enthält.Kann nicht eine bestimmte Tabelle mit BeautifulSoup4 (Python 3) kratzen

http://www.ligue1.com/LFPStats/stats_arbitre?competition=D1

ich den folgenden Code verwenden:

import requests 
from bs4 import BeautifulSoup 
import csv 

r=requests.get("http://www.ligue1.com/LFPStats/stats_arbitre?competition=D1") 

soup= BeautifulSoup(r.content, "html.parser") 
table=soup.find_all('table') 

Das gibt eine andere Tabelle irgendwo anders im html. Ich habe versucht, dies zu umgehen, indem ich [0], [1] usw. nach der Funktion find alle, aber nichts zurückgeben. Ich habe auch nach tr und td gesucht, bekomme aber ähnliche Ergebnisse. Ich habe keine Ahnung, warum die schöne Suppe diesen Tisch ignoriert.

Die Tabelle ich suche ist im HTML-Code unter

<table> 
<thead> 
    <tr> 
    <th class="{sorter: false} hide position">Position</th> 
    <th class="{sorter: false} joueur">Referees</th> 
    <th class="chiffre header"><span class="icon icon_carton_jaune">Yellow card</span></th> 
    <th class="chiffre header"><span class="icon icon_carton_rouge">Red card</span></th> 
    <th class="chiffre header">Matches</th> 
    </tr> 
</thead> 
    <tbody><tr> 
    <td class="position"></td> 
    <td class="joueur">Benoît BASTIEN</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/245">25</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/245">4</a></td> 
    <td class="chiffre">8</td> 
</tr> 
    <tr class="odd"> 
    <td class="position"></td> 
    <td class="joueur">Hakim BEN EL HADJ</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/259">55</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/259">4</a></td> 
    <td class="chiffre">10</td> 
</tr> 
    <tr> 
    <td class="position"></td> 
    <td class="joueur">Wilfried BIEN</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/162">44</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/162">3</a></td> 
    <td class="chiffre">9</td> 
</tr> 
    <tr class="odd"> 
    <td class="position"></td> 
    <td class="joueur">Ruddy BUQUET</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/269">33</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/269">2</a></td> 
    <td class="chiffre">7</td> 
</tr> 
    <tr> 
    <td class="position"></td> 
    <td class="joueur">Tony CHAPRON</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/102">43</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/102">1</a></td> 
    <td class="chiffre">8</td> 
</tr> 
    <tr class="odd"> 
    <td class="position"></td> 
    <td class="joueur">Amaury DELERUE</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/343">30</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/343">0</a></td> 
    <td class="chiffre">6</td> 
</tr> 
    <tr> 
    <td class="position"></td> 
    <td class="joueur">Saïd ENNJIMI</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/113">27</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/113">1</a></td> 
    <td class="chiffre">6</td> 
</tr> 
    <tr class="odd"> 
    <td class="position"></td> 
    <td class="joueur">Fredy FAUTREL</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/338">25</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/338">2</a></td> 
    <td class="chiffre">8</td> 
</tr> 
    <tr> 
    <td class="position"></td> 
    <td class="joueur">Antony GAUTIER</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/331">31</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/331">8</a></td> 
    <td class="chiffre">9</td> 
</tr> 
    <tr class="odd"> 
    <td class="position"></td> 
    <td class="joueur">Johan HAMEL</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/334">43</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/334">7</a></td> 
    <td class="chiffre">9</td> 
</tr> 
    <tr> 
    <td class="position"></td> 
    <td class="joueur">Lionel JAFFREDO</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/124">40</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/124">2</a></td> 
    <td class="chiffre">9</td> 
</tr> 
    <tr class="odd"> 
    <td class="position"></td> 
    <td class="joueur">Stéphane JOCHEM</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/294">33</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/294">4</a></td> 
    <td class="chiffre">8</td> 
</tr> 
    <tr> 
    <td class="position"></td> 
    <td class="joueur">Stéphane LANNOY</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/127">24</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/127">0</a></td> 
    <td class="chiffre">6</td> 
</tr> 
    <tr class="odd"> 
    <td class="position"></td> 
    <td class="joueur">Mikael LESAGE</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/286">38</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/286">3</a></td> 
    <td class="chiffre">9</td> 
</tr> 
    <tr> 
    <td class="position"></td> 
    <td class="joueur">Jérôme MIGUELGORRY</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/239">32</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/239">1</a></td> 
    <td class="chiffre">10</td> 
</tr> 
    <tr class="odd"> 
    <td class="position"></td> 
    <td class="joueur">Benoît MILLOT</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/287">43</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/287">0</a></td> 
    <td class="chiffre">11</td> 
</tr> 
    <tr> 
    <td class="position"></td> 
    <td class="joueur">Sébastien MOREIRA</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/148">38</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/148">5</a></td> 
    <td class="chiffre">10</td> 
</tr> 
    <tr class="odd"> 
    <td class="position"></td> 
    <td class="joueur">Nicolas RAINVILLE</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/188">40</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/188">7</a></td> 
    <td class="chiffre">10</td> 
</tr> 
    <tr> 
    <td class="position"></td> 
    <td class="joueur">Frank SCHNEIDER</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/247">33</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/247">4</a></td> 
    <td class="chiffre">10</td> 
</tr> 
    <tr class="odd"> 
    <td class="position"></td> 
    <td class="joueur">Clément TURPIN</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/333">26</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/333">3</a></td> 
    <td class="chiffre">8</td> 
</tr> 
    <tr> 
    <td class="position"></td> 
    <td class="joueur">Bartolomeu VARELA</td> 
    <td class="chiffre"><a href="/stats_arbitre_details/288">35</a></td> 
    <td class="chiffre"><a href="/stats_arbitre_details/288">3</a></td> 
    <td class="chiffre">9</td> 
</tr> 
</tbody></table> 

Ich habe auch als gut für td mit einer bestimmten Klasse versucht, die Suche, die funktionieren sollte, aber es kann nicht die Tabelle in der auszusuchen erster Platz.

Antwort

2

Das Problem ist, dass (ich nehme an) Sie beobachten den HTML-Code vom Browser generiert, und was Sie vermissen, ist, dass die Tabelle an die Seite mit Javascript angehängt wird.

Sie können dies mit Chrome (oder einem anderen Browser) bestätigen, und statt "Inspect" suchen Sie nach "Seitenquelle anzeigen", und Sie werden feststellen, dass in der Serverantwort keine solche Tabelle vorhanden ist.

Die URL, die es anruft, ist "http://www.ligue1.com/stats_arbitre?competition=D1", aber es gibt einen Trick, Sie müssen über HTTP-Header angeben, dass die Anfrage ein XHR ist. Wenn Sie mit dieser URL im Browser versuchen, erhalten Sie 500 Antworten.

Probieren Sie dieses Beispiel aus, um die Tabelle zu überprüfen, die Sie möchten.

curl --header "X-Requested-With: XMLHttpRequest" http://www.ligue1.com/stats_arbitre?competition=D1

In Ihrem Code, dies tun:

import requests 
from bs4 import BeautifulSoup 
import csv 

headers = {'X-Requested-With': 'XMLHttpRequest'} 
r = requests.get('http://www.ligue1.com/stats_arbitre?competition=D1', headers=headers) 

... 

Hoffe, dass es

+0

Hallo, danke, das war sehr hilfreich. Ich habe das curl-Beispiel in mein Terminal eingegeben und das HTML-Skript, das ich zu verwenden versuchte, wenn ich die inspect-Option im Browser verwendete, herausgezogen. Nach den vorgeschlagenen Änderungen an meinem Code kann ich den HTML-Code, nach dem ich gesucht habe, immer noch nicht abrufen. Soll ich mit Python einen Curl ausführen oder gibt es eine andere Lösung? Entschuldigung für meine Naivität bin ich ziemlich neu. –

+0

Ich bekomme auch nur Antwort 200 –

+0

es scheint ziemlich komisch, ich habe gerade wieder getestet und es hat funktioniert, hast du die URL von 'http: //www.ligue1.com/LFPStats/stats_arbitre? Competition = D1' zu "http: //www.ligue1.com/stats_arbitre? Wettbewerb = D1'? –

0

Selen kann es tun können.

from selenium import webdriver 
import time 

driver = webdriver.Firefox() 
driver.get(url) 
time.sleep(5) 
htmlSource = driver.page_source 
Verwandte Themen