如何在python中执行数据循环

2024-09-30 18:25:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以取出第一组<tr>,但如何取出下一组<tr>

如何使循环在最后一组<tr>处停止

x = 1
while True :
    next = re.search("\<tr>(.*?)</tr>" , match).group(0)
    print next
    x = x + 1

match在本例中包含我的已爬网表信息


Tags: re信息truesearchmatchgrouptrnext
1条回答
网友
1楼 · 发布于 2024-09-30 18:25:00

search方法将只找到第一个匹配项。必须使用^{}而不是search来查找所有匹配项,例如:

matches = re.findall("\<tr>(.*?)</tr>" , my_html)
for match in matches:
    print(match)

也就是说,像@DisplayName提到的那样,使用专门的工具(比如BeautifulSoup)解析HTML可能是更好的选择

相关问题 更多 >