在我正在抓的表中,第二行很长,我想简单地限制其中的字符,因为我只想要字符串开头的信息。我想要刮掉其他行。所以我的代码如下:
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
答案 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. ,'