BeautifulSoup获取li标签内的元素

时间:2017-06-30 13:14:12

标签: python-3.x

我在解析li标签内的html元素时遇到问题。 这是我的代码:

from bs4 import BeautifulSoup
import requests

sess = requests.Session()

url = 'http://example.com'
page = sess.get(url)
page = BeautifulSoup(page.text)
soap = page.select('li.item')
print(soap.find('h3').text)

这是html代码:

...
<li class="item">
    <strong class="item-type">design</strong>
    <h3 class="item-title">Item title</h3>
    <p class="item-description">
          Lorem ipsum dolor sit amet, dicam partem praesent vix ei, ne nec quem omnium cotidieque, omnes deseruisse efficiendi sit te. Mei putant postulant id. Cibo doctus eligendi at vix. Eos nisl exerci mediocrem cu, nullam pertinax petentium sea et. Vim affert feugait an.
     </p>
</li>
...

我只需粘贴其中一个,就有超过10个li标签。

输出错误:

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    print(soap.find('h3').text)
AttributeError: 'list' object has no attribute 'find'

1 个答案:

答案 0 :(得分:1)

感谢@DaveJ,这种方法有效:

facebook.com