只在div内拉特定的ul

时间:2018-11-12 12:18:04

标签: python beautifulsoup

我在html文本下方-

<div class="a-fixed-left-grid-col a-col-left" id="zg-left-col" style="width:200px;margin-left:-200px;float:none;">
<ul id="zg_browseRoot">
<li class="zg_browseUp"> ‹

     <a href="https://www.amazon.com/Best-Sellers/zgbs">Any Department</a>
</li>
<ul>
<li class="zg_browseUp"> ‹

     <a href="https://www.amazon.com/Best-Sellers/zgbs/amazon-devices">Amazon Devices &amp; Accessories</a>
</li>
<ul>
<li>
<span class="zg_selected"> Amazon Devices</span>
</li>
<ul>
<li><a href="https://www.amazon.com/Best-Sellers-Home-Security-Amazon/zgbs/amazon-devices/17386948011">Home Security from Amazon</a></li>
<li><a href="https://www.amazon.com/Best-Sellers-Amazon-Echo-Alexa-Devices/zgbs/amazon-devices/9818047011">Amazon Echo &amp; Alexa Devices</a></li>
<li><a href="https://www.amazon.com/Best-Sellers-Dash-Buttons/zgbs/amazon-devices/10667898011">Dash Buttons</a></li>
<li><a href="https://www.amazon.com/Best-Sellers-Fire-TV/zgbs/amazon-devices/8521791011">Fire TV</a></li>
<li><a href="https://www.amazon.com/Best-Sellers-Fire-Tablets/zgbs/amazon-devices/6669703011">Fire Tablets</a></li>
<li><a href="https://www.amazon.com/Best-Sellers-Kindle-readers/zgbs/amazon-devices/6669702011">Kindle E-readers</a></li>
<li><a href="https://www.amazon.com/Best-Sellers-Amazon-Device-Bundles/zgbs/amazon-devices/16926003011">Device Bundles</a></li>
</ul>
</ul>
</ul>
</ul>
</div>

我想这样拉-

https://www.amazon.com/Best-Sellers-Home-Security-Amazon/zgbs/amazon-devices/17386948011
https://www.amazon.com/Best-Sellers-Amazon-Echo-Alexa-Devices/zgbs/amazon-devices/9818047011
https://www.amazon.com/Best-Sellers-Dash-Buttons/zgbs/amazon-devices/10667898011
https://www.amazon.com/Best-Sellers-Fire-TV/zgbs/amazon-devices/8521791011
https://www.amazon.com/Best-Sellers-Fire-Tablets/zgbs/amazon-devices/6669703011
https://www.amazon.com/Best-Sellers-Kindle-readers/zgbs/amazon-devices/6669702011
https://www.amazon.com/Best-Sellers-Amazon-Device-Bundles/zgbs/amazon-devices/16926003011

我尝试使用下面的代码及其工作方式,但是没有给出想要的结果。

soup.find('div', class_= 'a-fixed-left-grid-col a-col-left').find_all('ul')[3]

2 个答案:

答案 0 :(得分:1)

您需要将所有href标记内的所有anchor放入。 试试这个:

print([a['href'] for a in soup.find('div', class_= 'a-fixed-left-grid-col a-col-left').find_all('ul')[3].find_all('a')])

答案 1 :(得分:1)

使用.select()

catLinks = soup.select('#zg_browseRoot ul ul ul li a')
for link in catLinks:
    print(link.get('href'))