2014-04-02 4 views
2

나는이 웹 페이지에서 테이블을 움켜 잡으려고합니다. 올바른 태그를 잡는 지 확실하지 않습니다. 여기까지 내가 지금까지 가지고있는 것이있다.Python beautifulsoup 잡아 테이블

from bs4 import BeautifulSoup 
import requests 

page='http://www.airchina.com.cn/www/en/html/index/ir/traffic/' 

r=requests.get(page) 

soup=BeautifulSoup(r.text) 

test=soup.findAll('div', {'class': 'main noneBg'}) 
rows=test.findAll("td") 

main noneBg은 테이블입니까? 그 태그 위로 마우스를 가져 가면 테이블이 강조 표시됩니까?

답변

2

필요한 테이블은 다른 URL에서로드 된 iframe입니다.

from bs4 import BeautifulSoup 
import requests 

page = 'http://www.airchina.com.cn/www/jsp/airlines_operating_data/exlshow_en.jsp' 

r = requests.get(page) 

soup = BeautifulSoup(r.text) 

div = soup.find('div', class_='mainRight').find_all('div')[1] 
table = div.find('table', recursive=False) 
for row in table.find_all('tr', recursive=False): 
    for cell in row('td', recursive=False): 
     print cell.text.strip() 

인쇄 : 당신이 때문에 페이지의 중첩 된 테이블에 recursive=False를 사용할 필요가

Feb 2014 
% change vs Feb 2013 
% change vs Jan 2014 
Cumulative Feb 2014 
% cumulative change 
1.Traffic 
1.RTKs (in millions) 
1407.8 
... 

참고 여기

은 (URL이 다른 시청)를 잡아 수있는 방법입니다.

+0

'print cell.text UnicodeEncodeError : 'gbk'코덱은 '3 위치에 문자'\ xa0 '을 인코딩 할 수 없습니다 : 불법 멀티 바이트 시퀀스'마지막 줄에서이 오류가 발생합니다. – jason

+0

@jason_cant_code는'cell.text' 도움말에서'decode ('utf-8')'을 호출합니까? – alecxe

+0

미안하지만 초보자입니다. 코드는 어떻게 생겼을까요? 'cell.text.decode ('utf-8'). split()'? – jason

관련 문제