当我通过BS抓取文本时,它会将所有数据打印为一个字符串。例如,它打印
<td>1</td>
<td>2</td>
<td>3</td>
...
<td>99</td>
作为
1
2
.
.
99
我想要实现的目标是将其打印成三行:
1 2 3
. . .
. . .
97 98 99
ATM我的代码是
import requests
from bs4 import BeautifulSoup
r = requests.get("http://www.com/")
soup = BeautifulSoup(r.content)
for link in soup.find_all("td"):
print(link.string)
谢谢!
答案 0 :(得分:0)
你可以像下面的例子那样做,
In [23]: from bs4 import BeautifulSoup
In [24]: s = """<td>1</td>
...: <td>2</td>
...: <td>3</td>
...: <td>4</td>
...: <td>5</td>
...: <td>6</td>"""
In [4]: soup = BeautifulSoup(s)
In [25]: td = [i.string for i in soup.find_all("td")]
In [26]: td
Out[26]: ['1', '2', '3', '4', '5', '6']
In [28]: for i in range(0, len(td), 3):
...: print(' '.join(td[i:i+3]))
...:
1 2 3
4 5 6