我无法在列表中获取所有html的href类属性值。我不确定我做错了什么,我甚至无法访问该参考文献。
以下是我试图解析的内容:
<!-- <div class="container">
<div class="row">
<div class="col-xs-12 col-md-offset-2 col-md-8 col-md-offset-2">
<div id='location_list'><h2>Browse by location</h2><ol class='suburb_locations'><div class="row"><li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/nsw/abbotsford-nsw">abbotsford, NSW</a><br><span class="sub_title">0 active owners</span><span class="sub_title">0 active borrowers</span></li><li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/vic/abbotsford-vic">abbotsford, VIC</a><br><span class="sub_title">0 active owners</span><span class="sub_title">0 active borrowers</span>
我试图在一个列表中获取key = place和值=活跃借款人数量的字典。以及包含href值的列表。我最大的问题是我无法访问这些兄弟姐妹中的任何一个。我在下面尝试了很多方法,列出了一些我一直在尝试的代码:
from bs4 import Beautiful Soup
soup=BeautifulSoup(html,"html5lib")
print soup.find_all('br')
print soup.find_all('div h2 ol li')
print soup.find('li',{'class':"col-sm-3"})
答案 0 :(得分:2)
问题是<!--
,如果你打印汤,你会发现那里什么都没有,当你删除它时你会得到html。
In [2]: from bs4 import BeautifulSoup
In [3]: soup = BeautifulSoup(html,"lxml")
In [4]: print(soup)
In [5]: soup = BeautifulSoup(html.replace("<!--",""),"lxml")
In [6]: print(soup)
<html><body><div class="container">
<div class="row">
<div class="col-xs-12 col-md-offset-2 col-md-8 col-md-offset-2">
<div id="location_list"><h2>Browse by location</h2><ol class="suburb_locations"><div class="row"><li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/nsw/abbotsford-nsw">abbotsford, NSW</a><br/><span class="sub_title">0 active owners</span><span class="sub_title">0 active borrowers</span></li><li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/vic/abbotsford-vic">abbotsford, VIC</a><br/><span class="sub_title">0 active owners</span><span class="sub_title">0 active borrowers</span></li></div></ol></div></div></div></div></body></html>
In [6]: soup.select(".col-sm-3")
Out[6]:
[<li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/nsw/abbotsford-nsw">abbotsford, NSW</a><br/><span class="sub_title">0 active owners</span><span class="sub_title">0 active borrowers</span></li>,
<li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/vic/abbotsford-vic">abbotsford, VIC</a><br/><span class="sub_title">0 active owners</span><span class="sub_title">0 active borrowers</span></li>]
In [7]: soup.select(".col-sm-3")[0].text
Out[7]: u'abbotsford, NSW0 active owners0 active borrowers'
我不确定你从哪里获取html,但如果你想解析它,你需要清理它。
对conatiner进行了完全注释,但是您只需更换源代码中的开口<!--
,然后我们可以将其删除:
import requests
r = requests.get("http://www.carnextdoor.com.au/find-a-car/")
from bs4 import BeautifulSoup
soup = BeautifulSoup(r.content.replace("<!--",""))
print(soup.select("div #location_list"))
这给了你:
[<div id="location_list"><h2>Browse by location</h2><ol class="suburb_locations"><div class="row"><li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/nsw/abbotsford-nsw">abbotsford, NSW</a><br/><span class="sub_title">0 active owners</span><span class="sub_title">0 active borrowers</span></li><li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/vic/abbotsford-vic">abbotsford, VIC</a><br/><span class="sub_title">0 active owners</span><span class="sub_title">0 active borrowers</span></li><li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/vic/aberfeldie">aberfeldie, VIC</a><br/><span class="sub_title">0 active owners</span><span class="sub_title">1 active borrower</span></li><li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/sa/adelaide">adelaide, SA</a><br/><span class="sub_title">0 active owners</span><span class="sub_title">1 active borrower</span></li></div><div class="row"><li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/act/ainslie">ainslie, ACT</a><br/><span class="sub_title">0 active owners</span><span class="sub_title">1 active borrower</span></li><li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/vic/aireys-inlet">aireys inlet, VIC</a><br/><span class="sub_title">0 active owners</span><span class="sub_title">1 active borrower</span></li><li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/vic/airly">airly, VIC</a><br/><span class="sub_title">0 active owners</span><span class="sub_title">1 active borrower</span></li><li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/vic/airport-west">airport west, VIC</a><br/><span class="sub_title">0 active owners</span><span class="sub_title">1 active borrower</span></li></div><div class="row"><li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/vic/albert-park">albert park, VIC</a><br/><span class="sub_title">0 active owners</span><span class="sub_title">5 active borrowers</span></li><li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/sa/aldgate">aldgate, SA</a><br/><span class="sub_title">1 active owner</span><span class="sub_title">2 active borrowers</span></li><li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/nsw/alexandria">alexandria, NSW</a><br/><span class="sub_title">1 active owner</span><span class="sub_title">53 active borrowers</span></li><li class="col-sm-3"><a href="http://www.carnextdoor.com.au/car-rental/nsw/alexandria-mc">alexandria mc, NSW</a><br/><span class="sub_title">0 active owners</span><span class="sub_title">1 active borrower</span></li></div><div class="row"><li class="col-sm-3"><a href="http://www.carnextdoor.com
还有更多,基本上是评论部分感兴趣的一切。