如何限制for循环中某个特定表行中的字符(Python / BeautifulSoup)

时间:2017-09-24 22:47:00

标签: python html-table beautifulsoup rows

在我正在抓的表中,第二行很长,我想简单地限制其中的字符,因为我只想要字符串开头的信息。我想要刮掉其他行。所以我的代码如下:

table = soup.find(id="table3")
    table_rows = table.findAll('tr')

    for tr in table_rows:
        td = tr.findAll('td')
        row = [i.text.strip() for i in td]
        print(row)

我怎样才能定位第二行?

输出具体如下:

["Computer price for Apple Inc. ,\n\n\nType\nForward\n\n\n\n\n\n\nBack\n\n\n\n\nDie\n\r\n...  

所以我只想抓住Computer price for Apple Inc. 也许有一种更好的方法,而不仅仅是使用字符限制作为启发式方法。是否可以指定它在,\n\n\n

之前抓取所有内容

1 个答案:

答案 0 :(得分:0)

您可以使用拆分功能分隔文本行。我使用",\n\n\n"作为分隔符:

>>> row = 'Computer price for Apple Inc. ,\n\n\nType\nForward\n\n\n\n\n\n\nBack\n\n\n\n\nDie\n\r\n'
>>> row.split(sep=",\n\n\n", maxsplit=1)[0]
'Computer price for Apple Inc. ,'