当前Python脚本:
import win_unicode_console
win_unicode_console.enable()
import requests
from bs4 import BeautifulSoup
data = '''
<div class="info">
<h1>Company Title</h1>
<p class="type">Company type</p>
<p class="address"><strong>ZIP, City</strong></p>
<p class="address"><strong>Street 123</strong></p>
<p style="margin-top:10px;"> Phone: <strong>(111) 123-456-78</strong><br />
Fax: <strong>(222) 321-654-87</strong><br />
Phone: <strong>(333) 87-654-321</strong><br />
Fax: <strong>(444) 000-1111-2222</strong><br />
</p>
<p style="margin-top:10px;"> E-mail: <a href="mailto:mail@domain.com">mail@domain.com</a><br />
E-mail: <a href="mailto:mail2@domain.com">mail2@domain.com</a><br />
</p>
<p> Web: <a href="http://www.domain.com" target="_blank">www.domain.com</a><br />
</p>
<p style="margin-top:10px;"> ID: <strong>123456789</strong><br />
VAT: <strong>987654321</strong> </p>
<p class="del" style="margin-top:10px;">Some info:</p>
<ul>
<li><a href="#category">» Category</a></li>
</ul>
</div>
'''
html = BeautifulSoup(data, "html.parser")
p = html.find_all('p', attrs={'class': None})
for pp in p:
print(pp.contents)
返回以下内容:
^{pr2}$问题: 我不知道如何为电话、传真和电子邮件、id、vat提取文本并从中创建数组,例如:
phones = [123-456-78, 87-654-321]
faxes = [321-654-87, 000-1111-2222]
emails = [mail@domain.com, mail2@domain.com]
id = [123456789]
vat = [987654321]
可以在拆分后使用defaultdict对数据进行分组:
拆分文本将为您提供数据列表:
^{pr2}$所以我们使用每两个元素作为键/值对。附加重复键。在
为了便于编辑捕捉传真和电话号码中的空格,只需使用拆分线将其拆分为多行,并在空白处拆分一次: 从集合导入defaultdict
输出:
相关问题 更多 >
编程相关推荐