我的HTML页面如下所示:
data = <section class="otln" itemscope="" itemtype="http://microformats.org/wiki/hCard">
<header>
<h3 class="org">Website:</h3>
</header>
<p><a href="http://www.abilityone.gov">U.S. AbilityOne Commission </a></p> </section>,
<section class="otln" itemscope="" itemtype="http://microformats.org/wiki/hCard">
<header>
<h3 itemprop="name">Main Address:</h3>
</header>
<p class="spk street-address">1401 S. Clark Street<br/>Suite 715<br/><span class="locality">Arlington</span>, <span class="region">VA</span> <span class="postal-code">22202-3259</span></p> </section>,
<section class="otln" itemscope="" itemtype="http://microformats.org/wiki/hCard">
<header>
<h3 itemprop="name">Phone Number:</h3>
</header>
<p>1-703-603-7740</p> </section>,
<section class="otln" itemscope="" itemtype="http://microformats.org/wiki/hCard">
<header>
<h3 class="org">Government branch:</h3>
</header>
<p>Executive Department Sub-Office/Agency/Bureau</p>
</section>我想从这个网页的<p>标签中提取所有细节,比如网站的href,主要地址,电话号码和政府部门。我尝试了许多不同的变化,以获得他们,但不能完全做到这一点。
编辑的
我的守则:
soup = BeautifulSoup(data,'lxml')
website.append([l.find('a')['href'] for l in soup.find_all('section',class_='otln')])以上尝试得到的'href‘抛出TypeError: 'NoneType' object is not subscriptable我有工作解决方案,以获得主要地址,电话号码,和政府部门。如果我能得到网站的'href‘,也就是"http://www.ability.gov“,会更好
发布于 2017-07-13 18:14:34
soup = BeautifulSoup(data, 'lxml')
for h, p in zip(soup.findAll('h3'), soup.findAll('p')):
# h is the header, p is the paragraph
a = p.find('a') # is it the website ?
print('%-20s\t%s' % (h.text, a['href'] if bool(a) else p.text))https://stackoverflow.com/questions/45062616
复制相似问题