使用BeautifulSoup取得li的文字

问题描述 投票:0回答:1

我正在尝试使用bs4抓取此HTML:

<td style="vertical-align:top;" class="vi-VR-brumblnkLst vi-VR-brumb-hasNoPrdlnks" id="vi-VR-brumb-lnkLst">
   <table width="100%" role="presentation">
      <tbody>
         <tr>
            <td style="">
               <ul role="list" aria-label="Listed in category:" itemscope="" itemtype="https://schema.org/BreadcrumbList">
                  <li itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem" class="bc-w">
                     <a itemprop="item" _sp="p2047675.l2706" href="https://www.ebay.com/b/Jewelry-Watches-/281" class="thrd"><span itemprop="name">Jewelry &amp; Watches</span></a>
                     <meta itemprop="position" content="1">
                  </li>
                  <li aria-hidden="true">&gt;</li>
                  <li itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem" class="bc-w">
                     <a itemprop="item" _sp="p2047675.l2706" href="https://www.ebay.com/b/Watches-Parts-Accessories-/14324" class="thrd"><span itemprop="name">Watches, Parts &amp; Accessories</span></a>
                     <meta itemprop="position" content="2">
                  </li>
                  <li aria-hidden="true">&gt;</li>
                  <li itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem" class="bc-w">
                     <a itemprop="item" _sp="p2047675.l2706" href="https://www.ebay.com/b/Wristwatches-/31387" class="scnd"><span itemprop="name">Wristwatches</span></a>
                     <meta itemprop="position" content="3">
                  </li>
                  <li>&gt;</li>
                  <li itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem" class="bc-w">
                     <a itemprop="item" _sp="p2047675.l2644" href="https://www.ebay.com/p/18032713872" title="See more 17j Seiko 5 Automatic Black Dial Analog Golden Color Watch Working Properly">
                     <span itemprop="name">See more 17j Seiko 5 Automatic Black Dial Analog Golden...</span>
                     </a>
                     <meta itemprop="position" content="1">
                  </li>
               </ul>
            </td>
         </tr>
      </tbody>
   </table>
</td>

具体地说,我想从中获得“手表”文字:

<li itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem" class="bc-w"><a itemprop="item" _sp="p2047675.l2706" href="https://www.ebay.com/b/Wristwatches-/31387" class="scnd"><span itemprop="name">Wristwatches</span></a><meta itemprop="position" content="3"></li>

我当前的代码是这样的:

import requests
from bs4 import BeautifulSoup

data = requests.get('https://www.ebay.com/sch/i.html?_from=R40&_trksid=m570.l1313&_nkw=mens+watch&_sacat=31387&LH_TitleDesc=0&_osacat=0&_odkw=mens+wath').text
soup = BeautifulSoup(data, 'lxml')

cat = soup.find('li', itemProp = 'itemListElement').text.strip()

print(cat)

但是它返回错误。我该如何实现?谢谢。

python html python-3.x beautifulsoup
1个回答
0
投票

没关系,我感谢您花时间阅读我的主题的任何人。

© www.soinside.com 2019 - 2024. All rights reserved.