我如何为下一个数据循环 re.search
how do i loop a re.search for the next data
我使用正则表达式
从 html table 中抓取了 2 组数据
数据:
<div class = "info">
<div class="name"><td>random</td></div>
<div class="hp"><td>123456</td></div>
<div class="email"><td>random@mail.com</td></div>
</div>
<div class = "info">
<div class="name"><td>random123</td></div>
<div class="hp"><td>654321</td></div>
<div class="email"><td>random123@mail.com</td></div>
</div>
正则表达式:
matchname = re.search('\<div class="name"><td>(.*?)</td>' , match3).group(1)
matchhp = re.search('\<div class="hp"><td>(.*?)</td>' , match3).group(1)
matchemail = re.search('\<div class="email"><td>(.*?)</td>' , match3).group(1)
所以使用正则表达式我可以取出
random
123456
random@mail.com
因此,将这组数据保存到我的数据库后,我想保存下一组如何获取下一组数据?我尝试使用 findall 然后插入我的数据库,但一切都在 1 行中。我需要数据按组设置在数据库中。
新手python有不清楚的地方请评论会尽量编辑
您不应使用正则表达式解析 HTML。简直一团糟,用BS4搞定。正确的做法:
soup = BeautifulSoup(match3, "html.parser")
names = []
allTds = soup.find_all("td")
for i,item in enumerate(allTds[::3]):
# firstname hp email
names.append((item.text, allTds[(i*3)+1].text, allTds[(i*3)+2].text))
为了回答所问的问题,我想我会包含一个你永远不应该使用的可怕丑陋的正则表达式。特别是因为它是 html,永远不要使用正则表达式来解析 html。 (请不要使用这个)
for thisMatch in re.findall(r"<td>(.+?)</td>.+?<td>(.+?)</td>.+?<td>(.+?)</td>", match3, re.DOTALL):
print(thisMatch[0], thisMatch[1], thisMatch[2])
正如@Racialz 指出的那样,您应该研究 using HTML parsers instead of regular expressions。
让我们采用 BeautifulSoup
以及 @Racialz 所做的,但构建一个更强大的解决方案。查找所有 info
元素并在其中找到所有字段,从而在输出中生成字典列表:
from pprint import pprint
from bs4 import BeautifulSoup
data = """
<div>
<div class = "info">
<div class="name"><td>random</td></div>
<div class="hp"><td>123456</td></div>
<div class="email"><td>random@mail.com</td></div>
</div>
<div class = "info">
<div class="name"><td>random123</td></div>
<div class="hp"><td>654321</td></div>
<div class="email"><td>random123@mail.com</td></div>
</div>
</div>
"""
soup = BeautifulSoup(data, "html.parser")
fields = ["name", "hp", "email"]
result = [
{field: info.find(class_=field).get_text() for field in fields}
for info in soup.find_all(class_="info")
]
pprint(result)
打印:
[{'email': 'random@mail.com', 'hp': '123456', 'name': 'random'},
{'email': 'random123@mail.com', 'hp': '654321', 'name': 'random123'}]
我使用正则表达式
从 html table 中抓取了 2 组数据数据:
<div class = "info">
<div class="name"><td>random</td></div>
<div class="hp"><td>123456</td></div>
<div class="email"><td>random@mail.com</td></div>
</div>
<div class = "info">
<div class="name"><td>random123</td></div>
<div class="hp"><td>654321</td></div>
<div class="email"><td>random123@mail.com</td></div>
</div>
正则表达式:
matchname = re.search('\<div class="name"><td>(.*?)</td>' , match3).group(1)
matchhp = re.search('\<div class="hp"><td>(.*?)</td>' , match3).group(1)
matchemail = re.search('\<div class="email"><td>(.*?)</td>' , match3).group(1)
所以使用正则表达式我可以取出
random
123456
random@mail.com
因此,将这组数据保存到我的数据库后,我想保存下一组如何获取下一组数据?我尝试使用 findall 然后插入我的数据库,但一切都在 1 行中。我需要数据按组设置在数据库中。
新手python有不清楚的地方请评论会尽量编辑
您不应使用正则表达式解析 HTML。简直一团糟,用BS4搞定。正确的做法:
soup = BeautifulSoup(match3, "html.parser")
names = []
allTds = soup.find_all("td")
for i,item in enumerate(allTds[::3]):
# firstname hp email
names.append((item.text, allTds[(i*3)+1].text, allTds[(i*3)+2].text))
为了回答所问的问题,我想我会包含一个你永远不应该使用的可怕丑陋的正则表达式。特别是因为它是 html,永远不要使用正则表达式来解析 html。 (请不要使用这个)
for thisMatch in re.findall(r"<td>(.+?)</td>.+?<td>(.+?)</td>.+?<td>(.+?)</td>", match3, re.DOTALL):
print(thisMatch[0], thisMatch[1], thisMatch[2])
正如@Racialz 指出的那样,您应该研究 using HTML parsers instead of regular expressions。
让我们采用 BeautifulSoup
以及 @Racialz 所做的,但构建一个更强大的解决方案。查找所有 info
元素并在其中找到所有字段,从而在输出中生成字典列表:
from pprint import pprint
from bs4 import BeautifulSoup
data = """
<div>
<div class = "info">
<div class="name"><td>random</td></div>
<div class="hp"><td>123456</td></div>
<div class="email"><td>random@mail.com</td></div>
</div>
<div class = "info">
<div class="name"><td>random123</td></div>
<div class="hp"><td>654321</td></div>
<div class="email"><td>random123@mail.com</td></div>
</div>
</div>
"""
soup = BeautifulSoup(data, "html.parser")
fields = ["name", "hp", "email"]
result = [
{field: info.find(class_=field).get_text() for field in fields}
for info in soup.find_all(class_="info")
]
pprint(result)
打印:
[{'email': 'random@mail.com', 'hp': '123456', 'name': 'random'},
{'email': 'random123@mail.com', 'hp': '654321', 'name': 'random123'}]