gpt4 book ai didi

python - 分隔
 标签内的文本

转载 作者:太空宇宙 更新时间:2023-11-04 09:35:04 27 4
gpt4 key购买 nike

我想尝试一些基本的网络抓取,但遇到了一个问题,因为我习惯了简单的 td-tags,在这种情况下,我有一个网页,其中包含以下预标记和其中的所有文本,这意味着刮掉它有点棘手。

<pre style="word-wrap: break-word; white-space: pre-wrap;">
11111111
11111112
11111113
11111114
11111115
</pre>

关于如何抓取每一行有什么建议吗?

谢谢

最佳答案

如果这正是您想要解析的内容,您可以使用splitlines()轻松获取行列表的功能,或者您可以调整 split()像这样的功能。

from bs4 import BeautifulSoup

content = """
<pre style="word-wrap: break-word; white-space: pre-wrap;">
11111111
11111112
11111113
11111114
11111115
</pre>""" # This is your content

soup = BeautifulSoup(content, "html.parser")
stuff = soup.find('pre').text
lines = stuff.split("\n") # or replace this by stuff.splitlines()
# print(lines) gives ["11111111", "11111112", "11111113", "11111114", "11111115"]
for line in lines:
print(line)
# prints each row separately.

关于python - 分隔 <pre> 标签内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54074831/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com