在漂亮的汤标签中找不到超链接



我正在构建一个程序,该程序将在网站(本例中为易趣(上抓取产品,以便进行比较。我有一个链接到戴森粉丝页面的URL,并返回所有带有类"s-item"的标签。我正试图访问产品标签中的链接,但当使用常用语法(tag['href']和tag.find_all('href'((时,我得到了一个错误和None(分别(。代码:

from bs4 import BeautifulSoup, NavigableString
import requests
# create soup + find product tags
markup = requests.get(
url='https://www.ebay.com/sch/i.html? 
_nkw=dyson+tower+fan&LH_ItemCondition=1000&ipg=240&_sop=12').content
soup = BeautifulSoup(markup, 'html.parser')
products = [soup.find_all('li', attrs={'class': 's-item'})][0]
# Attempt to extract link
tag = products[30]
print(f'Tag : {tag}')
try:
print(f'Link : {tag["href"]}')
except:
print('No href attribute')
print(f'Using find_all to find href : {tag.find_all("href")}')

以下代码提供了所需的链接

from bs4 import BeautifulSoup
import requests
url = 'https://www.ebay.com/sch/i.html?_from=R40&_nkw=Dyson+fans+&_sacat=0&_pgn=1'
req = requests.get(url)
soup = BeautifulSoup(req.text,'lxml')
for link in soup.select('li.s-item'):
print(link.a.get('href'))