gpt4 book ai didi

Python - 根据内容值提取href值

转载 作者:行者123 更新时间:2023-12-01 01:56:53 25 4
gpt4 key购买 nike

我正在尝试扫描网页,以使用产品名称的一部分查找特定产品的链接。

下面的 HTML 是我试图从中提取信息的部分:

<article class='product' data-json-url='/en/GB/men/products/omia066s188000161001.json' id='product_24793' itemscope='' itemtype='http://schema.org/Product'>
<header>
<h3>OMIA066S188000161001</h3>
</header>
<a itemProp="url" href="/en/GB/men/products/omia066s188000161001"><span content='OFF WHITE Shoes OMIA066S188000161001' itemProp='name' style='display:none'></span>
<span content='OFF WHITE' itemProp='brand' style='display:none'></span>
<span content='OMIA066S188000161001' itemProp='model' style='display:none'></span>
<figure>
<img itemProp="image" alt="OMIA066S188000161001 image" class="top" src="https://cdn.off---white.com/images/156374/product_OMIA066S188000161001_1.jpg?1498806560" />
<figcaption>
<div class='brand-name'>
HIGH 3.0 SNEAKER
</div>
<div class='category-and-season'>
<span class='category'>Shoes</span>
</div>


<div class='price' itemProp='offers' itemscope='' itemtype='http://schema.org/Offer'>
<span content='530.0' itemProp='price'>
<strong>£ 530</strong>
</span>
<span content='GBP' itemProp='priceCurrency'></span>
</div>


<div class='size-box js-size-box'>
<!-- / .available-size -->
<!-- / = render 'availability', product: product -->
<div class='sizes'></div>
</div>
</figcaption>
</figure>
</a></article>

我的代码如下:

import requests
from bs4 import BeautifulSoup

item_to_find = 'off white shoes'

s = requests.Session()
r = s.get('https://www.off---white.com/en/GB/section/new-arrivals.js')
soup = BeautifulSoup(r.content, 'html.parser')
#find_url = soup.find("a", {"content":item_to_find})['href']
#print(find_url)

如何仅过滤“content”包含 item_to_find 的行,然后提取该产品的“href”?

最终输出应如下所示:

/en/GB/men/products/omia066s188000161001

最佳答案

尝试一下。

import requests
from bs4 import BeautifulSoup

item_to_find = 'off white shoes'

s = requests.Session()
r = s.get('https://www.off---white.com/en/GB/section/new-arrivals.js')
soup = BeautifulSoup(r.content, 'html.parser')
links = soup.find_all("a")

for link in links:
if 'OFF WHITE Shoes' in link.encode_contents():
print link.get('href')

由于“OFF WHITE Shoes”文本存在于一个范围内,我们可以使用encode_contents()来检查每个链接中的所有标记。如果我们要搜索的文本存在,我们可以使用 BeautifulSoups .get 方法获取链接。

关于Python - 根据内容值提取href值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50067941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com