【发布时间】:2021-07-01 09:02:04
【问题描述】:
我有一个包含多行内容的 html 表格
<td>
Bob Marley (bmarley@whatever.com)
</td>
我正在尝试仅用电子邮件地址的第一部分查找并替换此内容,并去掉空格。
<td>
bmarley
</td>
这是当前代码,成功匹配用户名部分。我不确定如何替换标签之间的内容。
name_regex = re.compile('(?<=())[a-zA-Z]+(?=@)')
soup = bs(open(page_source), 'lxml')
for row in soup.select(table_name)[1:]:
name = re.search(name_regex,row.select_one('td:nth-of-type(2)').text.strip()).group()
print(name)
【问题讨论】:
标签: python regex beautifulsoup replace