【问题标题】:Fetching column values from html using beautifulsoup使用beautifulsoup从html中获取列值
【发布时间】:2017-07-21 14:41:23
【问题描述】:

我试图在这些列中获取单元格值,但我只在 html 表中获取这些列的标题

soup = BeautifulSoup(response, 'lxml')

table = soup.find('table', {'class', 'annotation'})
for row in table.findAll("tr"):
    cells = row.findAll("td")
    if len(cells) == 21:
        gene_id = cells[4].text
        gene_aspect = cells[6].text
        print(gene_id, gene_aspect)

我得到了这个输出

GO Identifier



Aspect

虽然,我希望这两列中存在值。

【问题讨论】:

  • 你能粘贴response的(相关部分)吗?

标签: python html web-scraping beautifulsoup lxml


【解决方案1】:

修复:

table = soup.find('table', {'class': 'annotation'})

table = soup.find('table', class_='annotation')

你的代码的问题在于

{'class', 'annotation'}

是一个集合,而 BeautifulSoup 需要一个字典。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    相关资源
    最近更新 更多