[英]How can I limit the characters in one specific table row in a for loop (Python/BeautifulSoup)
在我要抓取的表中,第二行很长,我只想限制其中的字符,因为我只想要字符串开头的信息。 我想按原样刮其他行。 所以我的代码如下:
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.
的Computer price for Apple Inc.
,也许有比将字符数限制作为启发法更好的方法。 是否可以指定它来抓取,\\n\\n\\n
之前的所有内容
您可以使用拆分功能来分隔文本行。 我已经使用",\\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. ,'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.