【问题标题】:Python beautifulsoup not grabbing full tablePython beautifulsoup 没有抢到满桌
【发布时间】:2014-04-22 07:35:22
【问题描述】:

我不确定这是否因为mechanize而没有抢到整张桌子

这行得通:

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

但这不是:

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

使用mechanize 的那个只产生以下内容,即使在萤火虫中我看到所有trtd

Jun 2012
% change vs Jun 2011
% change vs May 2012
Cumulative Jun 2012
% cumulative change

【问题讨论】:

  • 可能会自动在表格内添加tbody 元素。尝试在 tr 之前循环遍历 table 内的所有 tbody
  • @Wolph。我试过table.find_all('tbody'),但返回[]
  • 我相信它可能与您正在使用的html.parser 有关,请参阅我对工作版本的回答

标签: python beautifulsoup mechanize


【解决方案1】:

将两者结合使用时不会出现问题,因此它可能与您正在使用的 html.parser 有关。

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

【讨论】:

  • 成功了!谢谢您的帮助。我从来没有想过。
  • 我不懂html.parser。有时它是唯一有效的方法。有时它不起作用。
  • html.parser 是 Python 发行版中的解析器,因此它始终可用。但这并不是你能得到的最好的。 lxml 更快且非常有效,但它是一个单独的依赖项,这意味着您需要自己安装它。您可以在此处找到解析器列表:crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-26
  • 2022-10-19
  • 1970-01-01
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多