【问题标题】:parsing html table using beautiful soup使用漂亮的汤解析 html 表
【发布时间】:2014-02-27 15:37:39
【问题描述】:

我编写了这段代码来打印一张表格,如http://www.medindia.net/drug-price/list.asp 所示

import mechanize
import urllib2
from bs4 import BeautifulSoup

med="paracetamol"
br=mechanize.Browser()
br.set_handle_robots(False)
res=br.open("http://www.medindia.net/drug-price/")
br.select_form("frmdruginfo_search")
br.form['druginfosearch']=med
br.submit()
url=br.response().geturl()
print url
web_page = urllib2.urlopen(url)
soup = BeautifulSoup(web_page)
tabl=soup.find_all('table')
rows=tabl.find_all('tr')

for tr in rows:
        cols=tr.find_all('td')
        for td in cols:
              text = ''.join(td.find(text=True))
              print text+"|",

但是当我执行同样的操作时,我得到了这个错误

 rows=tabl.find_all('tr')
    AttributeError: 'list' object has no attribute 'find_all'

谁能帮我解决这个问题?谢谢!

【问题讨论】:

    标签: python html parsing html-parsing beautifulsoup


    【解决方案1】:

    soup.find_all('table') 返回匹配表的列表,您只需要一个 - 使用 find()

    tabl = soup.find('table', {'class': 'content-table'})
    rows = tabl.find_all('tr')
    

    另请注意,我明确表示我需要一个具有特定类的表。

    此外,您无需对页面进行单独的 urllib2 调用 - 只需使用 br.response().read() 获取实际的 html 供 BS 解析。

    仅供参考,如果您想在控制台上获得格式更好的表格结果,请考虑使用texttable

    import mechanize
    from bs4 import BeautifulSoup
    import texttable
    
    
    med = raw_input("Enter the drugname: ")
    br = mechanize.Browser()
    br.set_handle_robots(False)
    res = br.open("http://www.medindia.net/drug-price/")
    br.select_form("frmdruginfo_search")
    br.form['druginfosearch'] = med
    br.submit()
    
    soup = BeautifulSoup(br.response().read())
    
    tabl = soup.find('table', {'class': 'content-table'})
    table = texttable.Texttable()
    for tr in tabl.find_all('tr'):
        table.add_row([td.text.strip() for td in tr.find_all('td')])
    
    print table.draw()
    

    打印:

    +--------------+--------------+--------------+--------------+--------------+
    | SNo          | Prescribing  | Total No of  | Single       | Combination  |
    |              | Information  | Brands       |     Generic  |     of       |
    |              |              | (Single+Comb |              | Generic(s)   |
    |              |              | ination)     |              |              |
    +--------------+--------------+--------------+--------------+--------------+
    | 1            | Abacavir     | 6            | View Price   | -            |
    +--------------+--------------+--------------+--------------+--------------+
    | 2            | Abciximab    | 1            | View Price   | -            |
    +--------------+--------------+--------------+--------------+--------------+
    | 3            | Acamprosate  | 3            | View Price   | -            |
    +--------------+--------------+--------------+--------------+--------------+
    | 4            | Acarbose     | 41           | View Price   | -            |
    +--------------+--------------+--------------+--------------+--------------+
    ...
    

    【讨论】:

    • 出于某种原因,在我的最终结果中测试完全会导致texttableArraySizeError。出于某种原因,标题还包括我第一行的1Abacavir。唔。好奇。
    • @BK201 嗯,您正在寻找什么药物进行测试? (这样我就可以重现你的问题)
    • 完全是上面的 OP 代码(扑热息痛),但有您的变体。它抛出:texttable.ArraySizeError: array should contain 5 elements。很奇怪。
    • @BK201 这是我正在使用的code。请检查。输入扑热息痛,效果很好..
    • 这里是mine。我想显示错误的屏幕截图。代码根本没有什么不同。检查您的代码...
    猜你喜欢
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 2019-11-10
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多