我正在尝试使用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 & Watches</span></a>
<meta itemprop="position" content="1">
</li>
<li aria-hidden="true">></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 & Accessories</span></a>
<meta itemprop="position" content="2">
</li>
<li aria-hidden="true">></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>></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)
但是它返回错误。我该如何实现?谢谢。
没关系,我感谢您花时间阅读我的主题的任何人。