Rak*_*rma -1 python beautifulsoup
我有以下代码,它为我提供了 Example.html 文件中的数据。但我必须逐行读取数据
html_doc = open("Example.html","r")
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.get_text())
Run Code Online (Sandbox Code Playgroud)
您可以使用 splitlines() 方法轻松地逐行读取数据。
对于您的情况,您可以使用:
html_doc = open("Example.html","r")
soup = BeautifulSoup(html_doc, 'html.parser')
output = soup.get_text()
for row in output.splitlines():
# Do whatever you want
Run Code Online (Sandbox Code Playgroud)
编辑:对于在 if 条件后读取 20 行的请求,您可以枚举splitlines ()方法并读取接下来的 20 行。然后,使用break语句退出for循环。
for idx, row in enumerate(output.splitlines()):
if row == "ADD.c":
twenty_line = idx + 20
try:
if idx < twenty_line:
print(row + "\n")
else:
break
except NameError as e:
print(e)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2747 次 |
| 最近记录: |