Python Scraping Issue - PullRequest
       15

Python Scraping Issue

0 голосов
/ 06 марта 2019

Я пытаюсь собрать некоторые данные с сайта команд НФЛ, и я продолжаю получать пустую строку [] в выводе python.

Я пытаюсь получить название команды и ссылку на нее.

Вот код, который я пытаюсь findAll() включить:

<td customsortid="fullName" id="standingsTile" data="[object Object]" class="rmq-30d300f6" data-radium="true" style="border-top: 1px solid rgb(238, 238, 238); box-sizing: border-box; height: auto; line-height: 40px; padding-left: 16px; padding-right: 16px; text-align: right; white-space: nowrap; vertical-align: top; box-shadow: rgb(238, 238, 238) 4px 0px 1px; left: 0px; position: absolute; width: 160px;">
    <a href="http://www.nfl.com/teams/profile?team=NE" tabindex="0" data-metrics-link-module="divisionStandings0" data-radium="true" style="cursor: pointer; text-decoration: none;">
    <div class="rsv-ae9db127" data-radium="true">
        <div class="rsv-ae9db127" data-radium="true" style="align-items: center; padding: 0px; background-color: transparent; height: 40px; -webkit-box-align: center;">
            <div data-radium="true">
                <img alt="" data-test-id="facemask-image-container" sizes="100vw" src="https://static.nfl.com/static/content/public/static/wildcat/assets/img/logos/teams/NE.svg" width="48" data-radium="true" style="border: 0px; display: block; max-width: 100%; margin-right: 8px; width: 24px;">
            </div>
            <div class="rsv-ae9db127" data-radium="true">
                <div class="rmq-9da922a7" data-radium="true" style="line-height: 1; font-size: 12px; margin-bottom: 0px; color: rgb(51, 51, 51); text-transform: none; font-family: &quot;Endzone Sans Medium&quot;, sans-serif; display: none; margin-right: 4px; text-decoration: none;">
                    New England 
                </div>
                <div data-radium="true" style="color: rgb(51, 51, 51); font-family: &quot;Endzone Sans Medium&quot;, sans-serif; font-size: 12px; line-height: 1; margin-top: 0px; text-decoration: none; text-transform: none;">
                    Patriots
                </div>
            </div>
        </div>
        <div class="rsv-ae9db127" data-radium="true" style="font-size: 10px; line-height: 1; padding-left: 4px; padding-top: 8px;">
            z
        </div>
    </div>
    </a>
</td>

Вот мой код, который постоянно дает мне пустой список []

from bs4 import BeautifulSoup as bsoup
from urllib.request import urlopen as uReq

nfl_url = ("https://www.nfl.com/standings")
webpage = uReq(nfl_url)
page_html = webpage.read()
page_parser = bsoup(page_html , "html.parser")
odd = page_parser.findAll("td", {"class": "rmq-30d300f6"})
print(odd)

Где я пытаюсь получить название команды и ссылку от https://www.nfl.com/standings

Есть идеи, что я делаю не так?

Ответы [ 2 ]

0 голосов
/ 06 марта 2019

Содержимое HTML, которое вы ищете, генерируется Javascript, и поэтому вы не счищаете его с исходного HTML.

Вам следует рассмотреть возможность использования Selenium для загрузки своей страницы и подождать, покаэлемент, который вам нужен, загружен.Примерно так:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

from bs4 import BeautifulSoup as bsoup


nfl_url = "https://www.nfl.com/standings"
browser = webdriver.Chrome('/home/your_username/Downloads/chromedriver')
browser.get(nfl_url)
delay = 10
try:
    # Using 'fullName' ID as a check that the relevant portion of the page is loaded.
    # There may be other elements that are more appropriate - up to you to figure out.
    myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'fullName')))
except TimeoutException:
    print( "Loading took too much time!")

page_html = browser.page_source
browser.close()

page_parser = bsoup(page_html , "html.parser")
odd = page_parser.findAll("td", {"class": "rmq-30d300f6"})
print(odd)

Обратите внимание, что вам нужно будет указать путь к chromedriver и скачать его , если он еще не установлен в вашей системе.

0 голосов
/ 06 марта 2019

Ваш код отлично работает с примером HTML. Однако при повторном получении URL-адреса он не имеет те же данные. Возможно, страница отображается с помощью javascript, и сервер не обслуживает фактический HTML-код, который вы указали в примере. После загрузки страницы сохраните копию для себя, чтобы сравнить:

...
page_html = webpage.read()

print("Read %d bytes" % (len(page_html)))
open("some.html","wt").write(page_html.decode('utf-8'))
...

Содержимое, сохраненное в "some.html", заметно отличается от того, что вы ожидаете.

Возможно, вы сможете проанализировать информацию, которую вы хотите, из входных данных JavaScript, возвращаемых сервером:

...
{"conference":"AMERICAN_FOOTBALL_CONFERENCE","division":"AFC_EAST","teamId":"10043200-2018-239d-5857-a43b18004fb2","fullName":"New England Patriots","nickName":"Patriots","overallWin":11,"overallLoss":5  //etc.
...
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...