Anonymous
Очистить таблицу с сайта с помощью beautifulsoup, в конце ошибка
Сообщение
Anonymous » 27 июл 2024, 18:57
Я пытаюсь получить таблицу с веб-сайта НФЛ, но постоянно получаю ошибки и не знаю, что делаю не так.
Я использую следующий код:
Код: Выделить всё
import pandas
import urllib2
#specify the url
NFLpage = "http://www.espn.com/nhl/statistics/player/_/stat/points/sort/points/year/2015/seasontype/2"
#Query the website and return the html to the variable 'page'
page = urllib2.urlopen(NFLpage)
#import the Beautiful soup functions to parse the data returned from the website
from bs4 import BeautifulSoup
#Parse the html in the 'page' variable, and store it in Beautiful Soup format
soup = BeautifulSoup(page)
print soup.prettify(page)
#Find the right table
all_tables=soup.find_all('table')
right_table=soup.find('table', class_='tablehead')
right_table
for row in right_table.findAll("tr"):
col = row.find_all('td')
column_1 = col[0].string.strip()
RK.append(column_1)
column_2 = col[1].string.strip()
PLAYER.append(column_2)
column_3 = col[2].string.strip()
TEAM.append(column_3)
column_4 = col[3].string.strip()
GP.append(column_4)
column_5 = col[4].string.strip()
G1.append(column_5)
column_6 = col[5].string.strip()
A1.append(column_6)
column_7 = col[6].string.strip()
PTS.append(column_7)
column_8 = col[7].string.strip()
Diff.append(column_8)
column_9 = col[8].string.strip()
PIM.append(column_9)
column_10 = col[9].string.strip()
PTSG.append(column_10)
column_11 = col[10].string.strip()
SOG.append(column_11)
column_12 = col[11].string.strip()
PCT.append(column_12)
column_13 = col[12].string.strip()
GWG.append(column_13)
column_14 = col[13].string.strip()
G2.append(column_14)
column_15 = col[14].string.strip()
A2.append(column_15)
column_16 = col[15].string.strip()
G3.append(column_16)
column_17 = col[15].string.strip()
A3.append(column_17)
columns = {'RK': RK, 'PLAYER':PLAYER, 'TEAM'=TEAM, 'GP': GP, 'G1': G1, 'A1': A1, 'PTS': PTS, 'Diff'=Diff, 'PIM'=PIM, 'PTSG'=PTSG, 'SOG'=SOG, 'PCT'=PCT, 'GWG'=GWG, 'G2'=G2, 'A2'=A2, 'G3'=G3,'A3'=A3}
df = pd.DataFrame(columns)
df
В настоящее время возникает ошибка в строке назначения столбца (3-я с конца). Можете ли вы помочь мне понять, что я делаю не так?
С уважением,
Андрей
Подробнее здесь:
https://stackoverflow.com/questions/417 ... in-the-end
1722095825
Anonymous
Я пытаюсь получить таблицу с веб-сайта НФЛ, но постоянно получаю ошибки и не знаю, что делаю не так. Я использую следующий код: [code]import pandas import urllib2 #specify the url NFLpage = "http://www.espn.com/nhl/statistics/player/_/stat/points/sort/points/year/2015/seasontype/2" #Query the website and return the html to the variable 'page' page = urllib2.urlopen(NFLpage) #import the Beautiful soup functions to parse the data returned from the website from bs4 import BeautifulSoup #Parse the html in the 'page' variable, and store it in Beautiful Soup format soup = BeautifulSoup(page) print soup.prettify(page) #Find the right table all_tables=soup.find_all('table') right_table=soup.find('table', class_='tablehead') right_table for row in right_table.findAll("tr"): col = row.find_all('td') column_1 = col[0].string.strip() RK.append(column_1) column_2 = col[1].string.strip() PLAYER.append(column_2) column_3 = col[2].string.strip() TEAM.append(column_3) column_4 = col[3].string.strip() GP.append(column_4) column_5 = col[4].string.strip() G1.append(column_5) column_6 = col[5].string.strip() A1.append(column_6) column_7 = col[6].string.strip() PTS.append(column_7) column_8 = col[7].string.strip() Diff.append(column_8) column_9 = col[8].string.strip() PIM.append(column_9) column_10 = col[9].string.strip() PTSG.append(column_10) column_11 = col[10].string.strip() SOG.append(column_11) column_12 = col[11].string.strip() PCT.append(column_12) column_13 = col[12].string.strip() GWG.append(column_13) column_14 = col[13].string.strip() G2.append(column_14) column_15 = col[14].string.strip() A2.append(column_15) column_16 = col[15].string.strip() G3.append(column_16) column_17 = col[15].string.strip() A3.append(column_17) columns = {'RK': RK, 'PLAYER':PLAYER, 'TEAM'=TEAM, 'GP': GP, 'G1': G1, 'A1': A1, 'PTS': PTS, 'Diff'=Diff, 'PIM'=PIM, 'PTSG'=PTSG, 'SOG'=SOG, 'PCT'=PCT, 'GWG'=GWG, 'G2'=G2, 'A2'=A2, 'G3'=G3,'A3'=A3} df = pd.DataFrame(columns) df [/code] В настоящее время возникает ошибка в строке назначения столбца (3-я с конца). Можете ли вы помочь мне понять, что я делаю не так? С уважением, Андрей Подробнее здесь: [url]https://stackoverflow.com/questions/41746171/scrape-table-from-website-using-beautifulsoup-error-in-the-end[/url]