使用漂亮的汤和python从无序列表中抓取文字 [英] Scraping text from unordered lists using beautiful soup and python

查看:51
本文介绍了使用漂亮的汤和python从无序列表中抓取文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python和漂亮的汤从网页上抓取信息.我对源代码的以下部分感兴趣:

I am using python and beautiful soup to scrape information from a web page. I am interested in the following section of source code:

<ul class="breadcrumb">
<li><a href="/" title="Return to the home page">Home</a><span 
class="sprite icon-delimiter"></span></li>
<li><a href="/VehicleSearch/Search/Mini" title="View our range of Mini 
vehicles">Mini</a><span class="sprite icon-delimiter"></span></li>
<li class="active"><a href="/VehicleSearch/Search/Mini/Countryman" 
title="View our range of Mini Countryman">Countryman</a></li>
</ul>

我想提取无序列表项目符号的文本,即首页",迷你"和乡村人"(它们都是链接).

I want to extract the text of the unordered list bullets, i.e. 'Home', 'Mini' and 'Countryman' (which are also all links).

到目前为止,我最接近的尝试是这样做:

My closest try so far was by doing:

for ul in soup.findAll('ul', class_='breadcrumb'):
    print(ul.find('a').contents[0])

但这仅找到了主页"链接,而没有找到其他两个.我如何找到所有三个链接文本?

But this only found the 'Home' link and not the other two. How can I find all three link texts please?

推荐答案

尝试为链接文本添加内部循环:

Try to add inner loop for link text:

for ul in soup.findAll('ul', class_='breadcrumb'):
    for link in ul.findAll('a'):
        print(link.text)

这篇关于使用漂亮的汤和python从无序列表中抓取文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆