2017-04-07 1 views
1

저는 Python을 처음 사용하고 있으며 College Football Players에서 일부 데이터를 다듬 으려는 학습 프로젝트를 진행하고 있습니다.Beautiful Soup : 데이터 값이 표제와 일치하지 않습니다.

다음
</thead> 
    <tbody> 


> <tr ><th scope="row" class="right " data-stat="year_id" ><a 
> href="/cfb/years/1957.html">1957</a></th><td class="left " 
> data-stat="school_name" csk="San Jose State.1957" ><a 
> href="/cfb/schools/san-jose-state/1957.html">San Jose 
> State</a></td><td class="left " data-stat="conf_abbr" ><a 
> href="/cfb/conferences/independent/1957.html">Ind</a></td><td 
> class="center " data-stat="class" ></td><td class="center " 
> data-stat="pos" >RB</td><td class="right " data-stat="g" >10</td><td 
> class="right " data-stat="rec" >1</td><td class="right " 
> data-stat="rec_yds" >6</td><td class="right " 
> data-stat="rec_yds_per_rec" >6.0</td><td class="right " 
> data-stat="rec_td" >0</td><td class="right " data-stat="rush_att" 
> >1</td><td class="right " data-stat="rush_yds" >3</td><td class="right " data-stat="rush_yds_per_att" >3.0</td><td class="right " 
> data-stat="rush_td" >0</td><td class="right " data-stat="scrim_att" 
> >2</td><td class="right " data-stat="scrim_yds" >9</td><td class="right " data-stat="scrim_yds_per_att" >4.5</td><td class="right 
> " data-stat="scrim_td" >0</td></tr> 

내가 내 코드와 얼마나 멀리 왔된다 :

[u'', u'header_receiving', u'header_rushing', u'header_scrimmage', u'year_id', u'school_name', u'conf_abbr', u'class', u'pos', u'g', u'rec', u'rec_yds', u'rec_yds_per_rec', u'rec_td', u'rush_att', u'rush_yds', u'rush_yds_per_att', u'rush_td', u'scrim_att', u'scrim_yds', u'scrim_yds_per_att', u'scrim_td', u'year_id', u'school_name', u'conf_abbr', u'class', u'pos', u'g', u'rec', u'rec_yds', u'rec_yds_per_rec', u'rec_td', u'rush_att', u'rush_yds', u'rush_yds_per_att', u'rush_td', u'scrim_att', u'scrim_yds', u'scrim_yds_per_att', u'scrim_td', u'year_id', u'school_name', u'conf_abbr', u'class', u'pos', u'g', u'rec', u'rec_yds', u'rec_yds_per_rec', u'rec_td', u'rush_att', u'rush_yds', u'rush_yds_per_att', u'rush_td', u'scrim_att', u'scrim_yds', u'scrim_yds_per_att', u'scrim_td'] [u'San Jose State', u'Ind', None, u'RB', u'10', u'1', u'6', u'6.0', u'0', u'1', u'3', u'3.0', u'0', u'2', u'9', u'4.5', u'0', u'San Jose State', None, None, None, None, u'1', u'6', u'6.0', u'0', u'1', u'3', u'3.0', u'0', u'2', u'9', u'4.5', u'0'] 

:

headers = [item["data-stat"] for item in soup.find_all(attrs={"data-stat" : True})] 
cellStrings = [cell.find(text = True) for cell in soup.findAll('td')] 
print headers, cellStrings 

이 다음과 같은 출력합니다 웹 사이트의 소스 코드는 다음과 같습니다 문제는 소스 코드의 앞부분에 나타나는 표제 중 일부이므로 두 목록, 데이터 및 표제가 일치하지 않습니다.

제 질문은 '데이터 - 통계'를 별도로 가져 오는 대신 연결된 값과 함께 가져올 수 있습니까? 이상적으로는, 나는 이것을 사전으로 끌어 들일 것이다.

답변

0

나는 정확하게 당신을 얻는다면 {'data-stat-value': 'value of td'}으로 구성된 사전을 원한다. 그것은 반드시 그 data-stat 태그와 연관된 텍스트를 가져옵니다

data_stats = {e['data-stat']: e.get_text().strip() 
       for e in html.find_all(attrs={'data-stat': True})} 

이 방법 : 당신은 이런 식으로 뭔가를 할 수 있습니다.

관련 문제