有没有一种方法可以用我的代码找到这些href,而不必找到所有与if语句匹配的href



这里是html:<ul class="turbolink_scroller" id="container"><li><div class="inner-article"><a style="height:150px;" href="/shop/jackets/s9cqv86tx/mvduilkn8"><img width="150" height="150" src="//assets.supremenewyork.com/184910/vi/XqTrMgSbEMo.jpg" alt="Xqtrmgsbemo" /><div class="sold_out_tag">sold out</div></a><div class="product-name"><a class="name-link" href="/shop/jackets/s9cqv86tx/mvduilkn8">Woven Hooded Jacket</a></div><div class="product-style"><a class="name-link" href="/shop/jackets/s9cqv86tx/mvduilkn8">Green</a></div></div></li><li><div class="inner-article"><a style="height:150px;" href="/shop/jackets/s9cqv86tx/yfc69h4xd"><img width="150" height="150" src="//assets.supremenewyork.com/184907/vi/2QzvRTY6rdY.jpg" alt="2qzvrty6rdy" /><div class="sold_out_tag">sold out</div></a><div class="product-name"><a class="name-link" href="/shop/jackets/s9cqv86tx/yfc69h4xd">Woven Hooded Jacket</a></div><div class="product-style"><a class="name-link" href="/shop/jackets/s9cqv86tx/yfc69h4xd">Black</a></div></div></li><li><div class="inner-article"><a style="height:150px;" href="/shop/jackets/ev3enjhr0/ebrve9hwg"><img width="150" height="150" src="//assets.supremenewyork.com/185498/vi/6WyRqFHLTT4.jpg" alt="6wyrqfhltt4" /></a><div class="product-name"><a class="name-link" href="/shop/jackets/ev3enjhr0/ebrve9hwg">Supreme®/Schott® Fringe Suede Coat</a></div><div class="product-style"><a class="name-link" href="/shop/jackets/ev3enjhr0/ebrve9hwg">Red</a></div></div></li><li><div class="inner-article"><a style="height:150px;" href="/shop/jackets/ev3enjhr0/phlxzfwgu"><img width="150" height="150" src="//assets.supremenewyork.com/185520/vi/9bnJYqcxXgg.jpg" alt="9bnjyqcxxgg" /></a><div class="product-name"><a class="name-link" href="/shop/jackets/ev3enjhr0/phlxzfwgu">Supreme®/Schott® Fringe Suede Coat</a></div><div class="product-style"><a class="name-link" href="/shop/jackets/ev3enjhr0/phlxzfwgu">Black</a></div></div></li><li><div class="inner-article"><a style="height:150px;" href="/shop/jackets/e6o5y7tj2/j7gw2q0fp"><img width="150" height="150" src="//assets.supremenewyork.com/185015/vi/aqugEVgfKuc.jpg" alt="Aqugevgfkuc" /></a><div class="product-name"><a class="name-link" href="/shop/jackets/e6o5y7tj2/j7gw2q0fp">Grid Taping Velour Jacket</a></div><div class="product-style"><a class="name-link" href="/shop/jackets/e6o5y7tj2/j7gw2q0fp">Blue</a></div></div></li><li><div class="inner-article"><a style="height:150px;" href="/shop/jackets/e6o5y7tj2/d2aym3g0v"><img width="150" height="150" src="//assets.supremenewyork.com/184987/vi/P9Fe5r2mdAQ.jpg" alt="P9fe5r2mdaq" /><div class="sold_out_tag">sold out</div></a><div class="product-name"><a class="name-link" href="/shop/jackets/e6o5y7tj2/d2aym3g0v">Grid Taping Velour Jacket</a></div><div class="product-style"><a class="name-link" href="/shop/jackets/e6o5y7tj2/d2aym3g0v">Off-White</a></div></div></li><li><div class="inner-article"><a style="height:150px;" href="/shop/jackets/e6o5y7tj2/y6phf5mlb"><img width="150" height="150" src="//assets.supremenewyork.com/184975/vi/ePxAOa6h7Fc.jpg" alt="Epxaoa6h7fc" /></a><div class="product-name"><a class="name-link" href="/shop/jackets/e6o5y7tj2/y6phf5mlb">Grid Taping Velour Jacket</a></div><div class="product-style"><a class="name-link" href="/shop/jackets/e6o5y7tj2/y6phf5mlb">Red</a></div></div></li><li><div class="inner-article"><a style="height:150px;" href="/shop/jackets/e6o5y7tj2/i3u9g5krx"><img width="150" height="150" src="//assets.supremenewyork.com/185001/vi/dYtK4tzVOzI.jpg" alt="Dytk4tzvozi" /></a><div class="product-name"><a class="name-link" href="/shop/jackets/e6o5y7tj2/i3u9g5krx">Grid Taping Velour Jacket</a></div><div class="product-style"><a class="name-link" href="/shop/jackets/e6o5y7tj2/i3u9g5krx">Black</a></div></div></li><li><div class="inner-article"><a style="height:150px;" href="/shop/jackets/g2emcal6f/o0ml2ikc9"><img width="150" height="150" src="//assets.supremenewyork.com/184895/vi/h7A3WqVrbAg.jpg" alt="H7a3wqvrbag" /></a><div class="product-name"><a class="name-link" href="/shop/jackets/g2emcal6f/o0ml2ikc9">Track Half Zip Pullover</a></div><div class="product-style"><a class="name-link" href="/shop/jackets/g2emcal6f/o0ml2ikc9">Tan Glen Plaid</a></div></div></li><li><div class="inner-article"><a style="height:150px;" href="/shop/jackets/g2emcal6f/i9ukqmivb"><img width="150" height="150" src="//assets.supremenewyork.com/184893/vi/bc4gd2MK45U.jpg" alt="Bc4gd2mk45u" /></a><div class="product-name"><a class="name-link" href="/shop/jackets/g2emcal6f/i9ukqmivb">Track Half Zip Pullover</a></div><div class="product-style"><a class="name-link" href="/shop/jackets/g2emcal6f/i9ukqmivb">Black</a></div></div></li><li><div class="inner-article"><a style="height:150px;" href="/shop/jackets/g2emcal6f/d7igbl05j"><img width="150" height="150" src="//assets.supremenewyork.com/184897/vi/6CKTR-_PTHA.jpg" alt="6cktr ptha" /></a><div class="product-name"><a class="name-link" href="/shop/jackets/g2emcal6f/d7igbl05j">Track Half Zip Pullover</a></div><div class="product-style"><a class="name-link" href="/shop/jackets/g2emcal6f/d7igbl05j">Red Glen Plaid</a></div></div></li><li><div class="inner-article"><a style="height:150px;" href="/shop/jackets/t320rajnf/bw5h1d8f2"><img width="150" height="150" src="//assets.supremenewyork.com/184903/vi/q4W_U311s5s.jpg" alt="Q4w u311s5s" /></a><div class="product-name"><a class="name-link" href="/shop/jackets/t320rajnf/bw5h1d8f2">Checks Embroidered Denim Jacket</a></div><div class="product-style"><a class="name-link" href="/shop/jackets/t320rajnf/bw5h1d8f2">Black</a></div></div></li><li><div class="inner-article"><a style="height:150px;" href="/shop/jackets/t320rajnf/r7q9edzxo"><img width="150" height="150" src="//assets.supremenewyork.com/184900/vi/cv8DExfCW2U.jpg" alt="Cv8dexfcw2u" /></a><div class="product-name"><a class="name-link" href="/shop/jackets/t320rajnf/r7q9edzxo">Checks Embroidered Denim Jacket</a></div><div class="product-style"><a class="name-link" href="/shop/jackets/t320rajnf/r7q9edzxo">Blue</a></div></div></li><li><div class="inner-article"><a style="height:150px;" href="/shop/jackets/q6z0e81ft/izjfmkdlo"><img width="150" height="150" src="//assets.supremenewyork.com/185094/vi/zxcLTiugvd4.jpg" alt="Zxcltiugvd4" /></a><div class="product-name"><a class="name-link" href="/shop/jackets/q6z0e81ft/izjfmkdlo">Supreme Team Puffy Jacket </a></div><div class="product-style"><a class="name-link" href="/shop/jackets/q6z0e81ft/izjfmkdlo">Purple</a></div></div></li><li><div class="inner-article"><a style="height:150px;" href="/shop/jackets/q6z0e81ft/w7wvq3nsh"><img width="150" height="150" src="//assets.supremenewyork.com/185030/vi/XETfm_tFmoQ.jpg" alt="Xetfm tfmoq" /></a><div class="product-name"><a class="name-link" href="/shop/jackets/q6z0e81ft/w7wvq3nsh">Supreme Team Puffy Jacket </a></div><div class="product-style"><a class="name-link" href="/shop/jackets/q6z0e81ft/w7wvq3nsh">Teal</a></div></div></li><li><div class="inner-article"><a style="height:150px;" href="/shop/jackets/q6z0e81ft/od5k1ra7z"><img width="150" height="150" src="//assets.supremenewyork.com/185120/vi/oD4lVboAALs.jpg" alt="Od4lvboaals" /></a><div class="product-name"><a class="name-link" href="/shop/jackets/q6z0e81ft/od5k1ra7z">Supreme Team Puffy Jacket </a></div><div class="product-style"><a class="name-link" href="/shop/jackets/q6z0e81ft/od5k1ra7z">Black</a></div></div></li><li><div class="inner-article"><a style="height:150px;" href="/shop/jackets/q6z0e81ft/evtzfp2x3"><img width="150" height="150" src="//assets.supremenewyork.com/185060/vi/RJXCXu1j6oU.jpg" alt="Rjxcxu1j6ou" /></a><div class="product-name"><a class="name-link" href="/shop/jackets/q6z0e81ft/evtzfp2x3">Supreme Team Puffy Jacket </a></div><div class="product-style"><a class="name-link" href="/shop/jackets/q6z0e81ft/evtzfp2x3">Gold</a></div></div></li></ul>

这张图片显示了我试图用这个代码得到的hrefs:

req = requests.get(mainUrl)
soup = BeautifulSoup(req.text, 'html.parser')
keywords = "Schott"
colors = "Black"
for a in soup.find_all('a', class_='name-link', href=True):
if keywords and colors in a.text:
print(a['href'])

我只想要肖特黑色物品的hrefs。如果我使用if keywords in a.text:,则留给肖特项两个不同的href,一个是黑色,另一个是绿色。当使用CCD_ 3时,它找到Schott hrefs和包含";黑色";。我怎样才能排除其他";"颜色";href也不是";关键字";hrefs?

hrefs = [a['href'] for a in soup.select('a.name-link')
if all((keywords in a.text, colors in a.text))]

您很接近,只是更改了if条件。

for a in soup.find_all('a', class_='name-link', href=True):
if (keywords in a.text) and (colors in a.text):
print(a['href'])

样本Html。

html='''<a class="name-link" href="#1">Schott </a>
<a class="name-link" href="#2">Black</a>
<a class="name-link" href="#3">Schott Black</a>'''
soup=BeautifulSoup(html,'html.parser')
keywords = "Schott"
colors = "Black"
for a in soup.find_all('a', class_='name-link', href=True):
if (keywords in a.text) and (colors in a.text):
print(a['href'])

我能够找到匹配的hrefs并输出正确的hrefs:

req = requests.get(mainUrl)
soup = BeautifulSoup(req.content, 'html.parser')
keywords = "Woven"
colors = "Green"
for kw in soup.find_all('a', 'name-link', href=True):
if keywords in kw.text:
for c in soup.find_all('a', 'name-link', href=True):
if colors in c.text:
if kw['href'] == c['href']:
print(kw['href'])

最新更新