2015-12-15 9 views
5

का उपयोग करके एक विशिष्ट तालिका को स्क्रैप नहीं कर सकता है, मैं लीग 1 फुटबॉल वेबसाइट से एक टेबल को स्क्रैप करना चाहता हूं। विशेष रूप से वह तालिका जिसमें कार्ड और रेफरी पर जानकारी होती है।सुंदर सूप 4 (पायथन 3)

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

मैं निम्नलिखित कोड का उपयोग कर रहा:

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

यह कहीं और एक और टेबल रिटर्न html में। मैंने सभी कार्यों को खोजने के बाद [0], [1] आदि का उपयोग करके इसे सर्कवेट करने का प्रयास किया है लेकिन कुछ भी वापस नहीं किया है। मैंने tr और td के लिए भी खोज की है लेकिन समान परिणाम प्राप्त करें। मुझे नहीं पता कि सुंदर सूप इस तालिका को क्यों अनदेखा करता है।

तालिका मैं देख रहा हूँ नीचे

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

मैं भी एक विशिष्ट वर्ग के साथ td के लिए खोज के रूप में अच्छी तरह से जो काम करना चाहिए, लेकिन यह में मेज बाहर नहीं चुन सकते की कोशिश की है एचटीएमएल कोड में है पहले स्थान पर।

उत्तर

2

समस्या यह है कि (मुझे लगता है) आप ब्राउजर द्वारा जेनरेट किए गए एचटीएमएल कोड देख रहे हैं, और आप जो खो रहे हैं वह यह है कि टेबल जावास्क्रिप्ट का उपयोग कर पेज पर जोड़ा गया है।

आप क्रोम (या किसी अन्य ब्राउज़र) का उपयोग करके इसकी पुष्टि कर सकते हैं, और "निरीक्षण" के बजाय, "पृष्ठ देखें स्रोत" देखें, और आप देखेंगे कि सर्वर प्रतिक्रिया में ऐसी कोई तालिका नहीं है।

यह यूआरएल "http://www.ligue1.com/stats_arbitre?competition=D1" कहता है, लेकिन एक चाल है, आपको http शीर्षलेखों के माध्यम से इंगित करना होगा, अनुरोध एक एक्सएचआर है। यदि आप इस यूआरएल के साथ ब्राउज़र में आज़माते हैं, तो आपको 500 प्रतिक्रिया मिलेगी।

जाँच करने के लिए इस कर्ल उदाहरण प्रयास करें तालिका आप चाहते है।

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

अपने कोड में, ऐसा करते हैं:

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) 

... 

आशा है कि यह मदद करता है

+0

हैलो, धन्यवाद, यह बेहद सहायक था। मैंने अपने टर्मिनल में कर्ल उदाहरण दर्ज किया और मैंने HTML स्क्रिप्ट को खींच लिया, जब मैं ब्राउज़र पर निरीक्षण विकल्प का उपयोग कर रहा था। हालांकि मेरे कोड में आपके सुझाए गए संशोधनों के बाद, मैं अभी भी HTML को खींच नहीं सकता हूं। क्या मुझे पाइथन का उपयोग करके एक कर्ल करने की कोशिश करनी चाहिए या क्या कोई और समाधान है? मेरे नैतिकता के लिए माफ़ी मैं इस के लिए काफी नया हूँ। 'Http के लिए //www.ligue1.com/LFPStats/stats_arbitre प्रतियोगिता = D1': –

+0

मैं भी केवल प्रतिक्रिया 200 –

+0

यह लगता है बहुत अजीब, मैं सिर्फ फिर से परीक्षण किया हो और यह काम किया है, तो आप' http से यूआरएल बदल किया: //www.ligue1.com/stats_arbitre? प्रतियोगिता = डी 1'? –

0

सेलेनियम यह कर सकते हैं।

from selenium import webdriver 
import time 

driver = webdriver.Firefox() 
driver.get(url) 
time.sleep(5) 
htmlSource = driver.page_source 
संबंधित मुद्दे