简体   繁体   English

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

[英]How can I limit the characters in one specific table row in a for loop (Python/BeautifulSoup)

In the table I'm scraping, the 2nd row is very long and I'd like to simply limit the characters that are in it since I only want the information that is at the beginning of the string. 在我要抓取的表中,第二行很长,我只想限制其中的字符,因为我只想要字符串开头的信息。 I want to scrape the other rows as they are. 我想按原样刮其他行。 So my code is as follows: 所以我的代码如下:

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)

How can I only target the second row? 如何只定位第二行?

The output specifically looks like: 输出具体如下:

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

So I only want to grap the Computer price for Apple Inc. part, maybe there is a better way than just using character limit as a heuristic. 因此,我只想掌握Computer price for Apple Inc.Computer price for Apple Inc. ,也许有比将字符数限制作为启发法更好的方法。 Is it possible to specify it to grab everything before ,\\n\\n\\n 是否可以指定它来抓取,\\n\\n\\n之前的所有内容

You can use split function to separate text line. 您可以使用拆分功能来分隔文本行。 I have used ",\\n\\n\\n" as a separator: 我已经使用",\\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.

相关问题 我怎样才能在beautifulsoup python中删除这个 - how can I remove this one in beautifulsoup python 如何将表存储在变量中,每行作为元素和分隔符,以便在Python中使用BeautifulSoup来区分列? - How can I store a table in a variable with each row as an element and a delimiter to distinguish columns using BeautifulSoup in Python? 如何使用beautifulsoup解析表行中的两个字符串? - How can I parse two strings in a table row by using beautifulsoup? 如何使用BeautifulSoup从特定字符串解析表? - How can I parse a table from a specific string using BeautifulSoup? 如何限制 Python 中循环的迭代? - How can I limit iterations of a loop in Python? 如何有效地创建对特定字符有限制的排列? - How can i create a permutation with a limit on specific characters efficently? 在特定表格行 beautifulsoup python 上方插入元素 - insert element above specific table row beautifulsoup python 如何限制要写入的字符数和输入中的最小字符数,Python - How can I limit the number of characters to write and the minimum in an input, Python 如何将循环的一次迭代限制为固定时间(以秒为单位)? - How can I limit one iteration of a loop to a fixed time in seconds? 如何使用 BeautifulSoup 从表数据中连续删除特定的 class - How to remove specific class in a row from table data using BeautifulSoup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM