在网站中我解析器看起来像这样。顺便说一句,我是Python的新手。
<ul class="main-info-list">
<li class="span">
<div class="title">Address</div>
<div class="value">Bangkok </div>
</li>
<li>
<div class="title">Status</div>
<div class="value">Finish</div>
</li>
<li>
<div class="title">Type</div>
<div class="value">Condo</div>
</li>
<li>
<div class="title">Section</div>
<div class="value">Unknown</div>
</ul>
我怎样才能制作这样的字典。
Data = {"Address":"Bangkok","title":"Finish","Type":"Condo","Section":"Unknown"}
答案 0 :(得分:1)
使用bs4
<强>演示:强>
s = """<ul class="main-info-list">
<li class="span">
<div class="title">Address</div>
<div class="value">Bangkok </div>
</li>
<li>
<div class="title">Status</div>
<div class="value">Finish</div>
</li>
<li>
<div class="title">Type</div>
<div class="value">Condo</div>
</li>
<li>
<div class="title">Section</div>
<div class="value">Unknown</div>
</ul>"""
from bs4 import BeautifulSoup
d = {}
s = BeautifulSoup(s, "html.parser")
for i in s.find_all("li"):
d[i.find("div", {"class": "title"}).text] = i.find("div", {"class": "value"}).text
print(d)
<强>输出:强>
{u'Status': u'Finish', u'Section': u'Unknown', u'Type': u'Condo', u'Address': u'Bangkok '}