Pregunta

I'm not sure if this is not grabbing the full table because of mechanize

This works:

from bs4 import BeautifulSoup
import requests

page = 'http://www.airchina.com.cn/www/jsp/airlines_operating_data/exlshow_en.jsp'
r = requests.get(page)

r.encoding = 'utf-8'
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.split()

but this doesn't:

import mechanize
from bs4 import BeautifulSoup
import requests

URL='http://www.airchina.com.cn/www/jsp/airlines_operating_data/exlshow_en.jsp'
control_year=['2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014']
control_month=['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']

br = mechanize.Browser()
r=br.open(URL)

br.select_form("exl")
control_m = br.form.find_control('month')
control_y = br.form.find_control('year')

br[control_m.name]=['06'] 
br[control_y.name]=['2012']
response = br.submit()
soup = BeautifulSoup(response,'html.parser')
#div = soup.find('div', class_='mainRight')


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()

The one that uses mechanize only produces the below, even though in firebug i see all of the tr and td

Jun 2012
% change vs Jun 2011
% change vs May 2012
Cumulative Jun 2012
% cumulative change
¿Fue útil?

Solución

When combining the two it works without a problem so it might be related to the html.parser you are using.

import mechanize
from bs4 import BeautifulSoup

URL = ('http://www.airchina.com.cn/www/jsp/airlines_operating_data/'
       'exlshow_en.jsp')
control_year = ['2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013',
                '2014']
control_month = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10',
                 '11', '12']

br = mechanize.Browser()
r = br.open(URL)

br.select_form("exl")
control_m = br.form.find_control('month')
control_y = br.form.find_control('year')

br[control_m.name] = ['06']
br[control_y.name] = ['2012']
response = br.submit()

soup = BeautifulSoup(response)

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.split()
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top