Python - 根据内容值提取href值

时间:2018-04-27 18:03:06

标签: python python-3.x beautifulsoup

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

以下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

2 个答案:

答案 0 :(得分:2)

给这一点。

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')

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

答案 1 :(得分:0)

考虑python 3的更具体的答案是:

import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup

search_item = 'orange timberland'  #make sure the search terms are in small letters (a portion of text will suffice)
URL = 'https://www.off---white.com/en/GB/section/new-arrivals.js'

res = requests.get(URL)
soup = BeautifulSoup(res.text, 'html.parser')
for link in soup.find_all(class_="brand-name"):
    if search_item in link.text.lower():
        item_name = link.get_text(strip=True)
        item_link = urljoin(URL,link.find_parents()[2].get('href'))
        print("Name: {}\nLink: {}".format(item_name,item_link))

输出:

Name: ORANGE TIMBERLAND BOOTS
Link: https://www.off---white.com/en/GB/men/products/omia073s184780161900