2017-12-29 5 views
1

위키 피 디아의 고귀한 계시록 목록에서 테이블을 추출하려고했습니다.이 테이블에는 아무런 가치가 없습니다. 그 값을 처리하는 방법을 알지 못합니다. 셀을 반복하면서 어떻게 할 수 있습니까? 표에 none 값을 포함 시키십시오. 위키 백과 페이지에 링크입니다 : https://en.wikipedia.org/wiki/List_of_Nobel_laureates파이썬을 사용하는 웹 스크래핑 테이블

import requests 
from bs4 import BeautifulSoup 
import pandas as pd 
r=requests.get('https://en.wikipedia.org/wiki/List_of_Nobel_laureates') 
soup=BeautifulSoup(r.text, 'html.parser') 
table = soup.find('table', class_='wikitable') 

rows = table.find_all('tr') 
del rows[0] 


for row in rows: 
    cells=row.find_all('td') 
    records=[] 
    print(cells) 


    year = cells[0].text 
    print("contents",cells[1].contents[1].text) 
    physics_winner = cells[1].contents[1].text 
    physics_url = cells[1].find('a')['href'] 
+0

예상되는 출력은 얼마입니까? –

+0

내 결과물은 모든 귀족 계급의 수상자와 그들의 규율, 각자의 URL과 그들이받은 상금을받은 csv 파일이어야합니다. – Aamir

+0

'wikitablescrape'을 시도 했습니까? 파이썬 패키지 –

답변

0

당신은 시도하고 그냥 당신이 테이블에 없음에 대한 슬로우됩니다 어떤 예외 알고있는 경우를 제외하고 사용할 수 있습니다. 내가 원하는 출력의 확실하지 않다하지만 난 세포를 통해 반복에 대한

import requests 
    from bs4 import BeautifulSoup 
    import pandas as pd 

    r = requests.get('https://en.wikipedia.org/wiki/List_of_Nobel_laureates') 
    soup = BeautifulSoup(r.text, 'html.parser') 
    table = soup.find('table', class_='wikitable') 

    rows = table.find_all('tr') 
    del rows[0] 


    for row in rows[:20]: 
     cells = row.find_all('td') 
     records = [] 

     try: 
      year = cells[0].text 
     except IndexError: 
      year = 'n/a' 

     try: 
      print("contents", cells[1].contents[1].text) 
     except IndexError: 
      print("contents", 'n/a') 

     try: 
      physics_winner = cells[1].contents[1].text 
     except IndexError: 
      physics_winner = 'n/a' 

     try: 
      physics_url = cells[1].find('a')['href'] 
     except TypeError: 
      physics_url = 'n/a' 
+0

덕분에 당신 응답하지만 난 여전히 오류가 점점 오전에 대해 : 년 = 세포는 [0] IndexError는 .text : 를 추가 범위 – Aamir

+0

에서리스트 인덱스를'보십시오 년 = 세포가 [0] IndexError 제외 을는 .text : 를 year = 'n/a' ' – doughboy

+0

나는 그것을 시도하고 그것은 효과가있다. URL에서 색인 오류가 발생합니다. TypeError와 IndexError 모두에 예외를 추가 할 수있는 방법이 있습니까? – Aamir

0

을/당신은 팬더가 그렇게 시도에 더 나은 대안을 위해 replace function 들여다 가져 볼 excepts 않습니다

import requests 
    from bs4 import BeautifulSoup 
    import pandas as pd 

    r = requests.get('https://en.wikipedia.org/wiki/List_of_Nobel_laureates') 
    soup = BeautifulSoup(r.text, 'html.parser') 
    table = soup.find('table', class_='wikitable') 

    rows = table.find_all('tr') 
    del rows[0] 


    for row in rows[:15]: 
     cells = row.find_all('td') 
     for cell in cells: 
      print(cell.text) 
      # print(cells) 
      # records = [] 


      try: 
       year = cell.text 
      except IndexError: 
       year = 'n/a' 

      try: 
       print("contents", cell.contents[1].text) 
      except IndexError: 
       print("contents", 'n/a') 

      try: 
       physics_winner = cell.contents[1].text 
      except IndexError: 
       physics_winner = 'n/a' 

      try: 
       physics_url = cell.find('a')['href'] 
      except TypeError: 
       physics_url = 'n/a'