Получение ссылки и таблицы очистки из списка - PullRequest
0 голосов
/ 24 января 2020

Я хочу получить пару столов от разных игроков. Когда он ищет кого-то вроде Серхио Родригеса, появляется несколько имен (https://basketball.realgm.com/search?q=Sergio+Rodriguez), поэтому вместо перехода на отдельную страницу он выдает «Нет международной таблицы для Серхио Родригеса». Из этих трех я хочу go на отдельной странице Серхио Родригеса, который играл в НБА, который занимает второе место в списке, и вычищать таблицы, но я не уверен, как go об этом. Как мне использовать rel, так как это единственный способ, который, кажется, будет работать. Псевдокод есть, если это помогает. Спасибо.

HTML:

<tbody>
<tr>
<td class="nowrap tablesaw-cell-persist" rel="Rodriguez Febles, Sergio"><a href="/player/Sergio-Rodriguez-Febles/Summary/50443">Sergio Rodriguez Febles</a></td>
<td class="nowrap" rel="5">SF</td>
<td class="nowrap" rel="79">6-7</td>
<td class="nowrap" rel="202">202</td>
<td class="nowrap" rel="19931018"><a href="/info/birthdays/19931018/1">Oct 18, 1993</a></td>
<td class="nowrap" rel="2015"><a href="/nba/draft/past_drafts/2015" target="_blank">2015</a></td>
<td class="nowrap" rel="N/A">-</td>
<td rel="-">-</td>
</tr>
<tr>
<td class="nowrap tablesaw-cell-persist" rel="Rodriguez, Sergio"><a href="/player/Sergio-Rodriguez/Summary/85">Sergio Rodriguez</a></td>
<td class="nowrap" rel="1">PG</td>
<td class="nowrap" rel="75">6-3</td>
<td class="nowrap" rel="176">176</td>
<td class="nowrap" rel="19860612"><a href="/info/birthdays/19860612/1">Jun 12, 1986</a></td>
<td class="nowrap" rel="2006"><a href="/nba/draft/past_drafts/2006" target="_blank">2006</a></td>
<td class="nowrap" rel="N/A">-</td>
<td rel="NYK, PHL, POR, SAC"><a href="/nba/teams/New-York-Knicks/20/Rosters/Regular/2010">NYK</a>, <a href="/nba/teams/Philadelphia-Sixers/22/Rosters/Regular/2017">PHL</a>, <a href="/nba/teams/Portland-Trail-Blazers/24/Rosters/Regular/2009">POR</a>, <a href="/nba/teams/Sacramento-Kings/25/Rosters/Regular/2010">SAC</a></td>
</tr>
<tr>
<td class="nowrap tablesaw-cell-persist" rel="Rodriguez, Sergio"><a href="/player/Sergio-Rodriguez/Summary/39601">Sergio Rodriguez</a></td>
<td class="nowrap" rel="3">SG</td>
<td class="nowrap" rel="76">6-4</td>
<td class="nowrap" rel="-">-</td>
<td class="nowrap" rel="19771012"><a href="/info/birthdays/19771012/1">Oct 12, 1977</a></td>
<td class="nowrap" rel="1999"><a href="/nba/draft/past_drafts/1999" target="_blank">1999</a></td>
<td class="nowrap" rel="N/A">-</td>
<td rel="-">-</td>
</tr>
</tbody>
import requests
from bs4 import BeautifulSoup
import pandas as pd


playernames=['Carlos Delfino', 'Sergio Rodriguez']

result = pd.DataFrame()
for name in playernames:

    fname=name.split(" ")[0]
    lname=name.split(" ")[1]
    url="https://basketball.realgm.com/search?q={}+{}".format(fname,lname)
    response = requests.get(url)

    soup = BeautifulSoup(response.content, 'html.parser')

    # check the response url
    if (response.url == "https://basketball.realgm.com/search..."):
        # parse the search results, finding players who played in NBA
        ... get urls from the table ...
        soup.table...  # etc.
        foreach url in table:
            response = requests.get(player_url)
            soup = BeautifulSoup(response.content, 'html.parser')
            # call the parse function for a player page
            ...
            parse_player(soup)
    else: # we have a player page
        # call the parse function for a player page, same as above
        ...
        parse_player(soup)

    try:
        table1 = soup.find('h2',text='International Regular Season Stats - Per Game').findNext('table')
        table2 = soup.find('h2',text='International Regular Season Stats - Advanced Stats').findNext('table')

        df1 = pd.read_html(str(table1))[0]
        df2 = pd.read_html(str(table2))[0]

        commonCols = list(set(df1.columns) & set(df2.columns))
        df = df1.merge(df2, how='left', on=commonCols)
        df['Player'] = name

    except:
        print ('No international table for %s.' %name)
        df = pd.DataFrame([name], columns=['Player'])

Ответы [ 2 ]

1 голос
/ 24 января 2020

Pandas имеет очень полезный метод для чтения html напрямую. Это особенно полезно, если вы хотите получить информацию из таблиц, которая применима к вам. По сути, pandas будет очищать веб-сайт для любых таблиц и считывать таблицы как кадры данных. Подробнее об этом здесь

Проблема здесь в том, что вам необходим доступ к ссылке на проигрывателе, а метод read_html считывает таблицу как текст и не учитывает теги. .

Тем не менее, я нашел возможное решение. Это ни в коем случае не лучший, но, надеюсь, вы можете использовать и улучшить его.

Подход:

  1. Считайте таблицу, используя read_html метод
  2. Получить необходимое имя игрока из-за стола (игрок с NBA != '-')
  3. Может быть несколько игроков с таким именем - скажем, есть 3 Sergio Rodriguez, но только 2-й игрок сыграл в НБА. - вам понадобится этот индекс, то есть index=1 (предполагается, что начальный индекс равен 0), чтобы найти ссылку позже
  4. Чтобы получить индекс, мы запрашиваем у таблицы имя игрока и получаем индексное местоположение этого игрока .
  5. Теперь мы ищем все ссылки, текст которых Sergio Rodriguez
  6. Мы выбираем только ссылку с соответствующим индексом, т. Е. Если индекс равен 1 (начиная с 0), мы выбираем 2-я ссылка с текстом == Sergio Rodriguez
import pandas as pd
import requests
from bs4 import BeautifulSoup

# read the data from the website as a list of dataframes (tables)
web_data = pd.read_html('https://basketball.realgm.com/search?q=Sergio+Rodriguez')

# the table you need is the second to last one
required_table = web_data[len(web_data)-2]

print (required_table)
>>>
                    Player Pos   HT   WT    Birth Date  Draft Year College                 NBA
0  Sergio Rodriguez Febles  SF  6-7  202  Oct 18, 1993        2015       -                   -
1         Sergio Rodriguez  PG  6-3  176  Jun 12, 1986        2006       -  NYK, PHL, POR, SAC
2         Sergio Rodriguez  SG  6-4    -  Oct 12, 1977        1999       -                   -
### get the player name who has played in NBA
required_player_name = required_table.loc[required_table['NBA']!='-']['Player'].values[0]

print (required_player_name)
>>>
Sergio Rodriguez
## check for duplicate players with this name (reset index so that we get the indices of player with the same name in order)

table_with_player = required_table.loc[(required_table['Player']==required_player_name)].reset_index(drop=True)

# get the indices of player where NBA is not '-'
index_of_player_to_get = list(table_with_player[table_with_player['NBA']!='-'].index)[0]

print (index_of_player_to_get)


### basically if indices_of_player_to_get = 2 (say) then we need the 3rd  link with player name == required_player_name
>>>
0

Теперь мы можем прочитать все ссылки и вытащить ссылку на позиция index_of_player_to_get среди всех ссылок с именем Sergio Rodriguez


url='https://basketball.realgm.com/search?q=Sergio+Rodriguez'
response = requests.get(url)

soup = BeautifulSoup(response.content, 'html.parser')

## get all links
all_links = soup.find_all('a', href=True)

link_idx = -1
for link in all_links:
    if link.text == required_player_name:
        # player name found, inc link_idx
        link_idx+=1
        if link_idx == index_of_player_to_get:
            print (link['href'])
>>>
/player/Sergio-Rodriguez/Summary/85
0 голосов
/ 24 января 2020

Итак, как вы знаете, ваш rel всегда находится в восьмом столбце таблицы, вы можете сделать что-то вроде этого:

soup = BeautifulSoup(html)

rows = [row for row in soup.find_all('tr')] # Get each row from the table

eighth_text = [col.find_all('td')[7].text for col in rows] # get text from eighth column
idx = [n for n,i in enumerate(eighth_text) if i!='-'] #Get the index of all rows that have text (are NBA players)

Затем вы можете получить доступ к этому (или этим) игроку (ам) ) с чем-то вроде:

for i in idx:
    print(rows[i].a)

или любым другим атрибутом, который вы ищете. Возможно, есть и другие способы pythoni c, но я отдаю предпочтение простоте понимания.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...