2013-07-23 7 views
0

이것은 BeautifulSoup를 사용하여 python2.7에서 html 테이블을 구문 분석하는 방법에 관한 문맥 별 질문입니다.BeautifulSoup를 사용하여 테이블을 구문 분석하는 방법은 무엇입니까?

나는 here html 테이블을 추출하여 tab-delim csv에 넣고 BeautifulSoup로 재생 해 보았습니다. 문맥

코드 :

proxies = { 
    "http://": "198.204.231.235:3128", 
} 
site = "http://sloanconsortium.org/onlineprogram_listing?page=11&Institution=&field_op_delevery_mode_value_many_to_one[0]=100%25%20online" 

r = requests.get(site, proxies=proxies) 
print 'r: ', r 
html_source = r.text 
print 'src: ', html_source 
soup = BeautifulSoup(html_source) 

왜이 코드는 4 행을 얻을하지 않는 이유는 무엇입니까?

soup.find('table','views-table cols-6').tr[4] 

헤더 행이 아닌 첫 번째 행의 모든 ​​요소를 ​​어떻게 인쇄합니까?

답변

2

좋아요 누군가 당신에게 하나의 라이너를 줄 수있을 수도 있지만 다음 당신은

table = soup.find('table', class_='views-table cols-6')                                                       
for row in table.find_all('tr'):                                                            
    row_text = list()                                                               
    for item in row.find_all('td'):                                                            
     text = item.text.strip()                                                            
     row_text.append(text.encode('utf8'))                                                         
    print row_text 

내가 당신의 TR [4] 당신과 같은 속성이 아니라 인덱스가 될 것으로 믿습니다 시작한다 가정 해.

관련 문제