Python-基于内容值提取href值

Python-基于内容值提取href值,python,python-3.x,beautifulsoup,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>OMIA066S1

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

下面的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>
如何仅过滤“content”包含要查找的项目的行,然后提取该产品的“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 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

感谢您的关注-当我运行代码时,我得到以下信息。打印链接。get('href')^Syntaxer:无效syntax@PiersThomas您使用的是什么版本的Python?试试这个:
print(link.get('href'))
pythonv3.6.3是我当前的版本文件“t.py”,第36行,在link.encode_contents()的if'OFF WHITE Shoes'中:TypeError:需要一个类似字节的对象,而不是'str'my bad,我使用的是Python版本2.7.10。Python3的逻辑应该仍然相同,只是语法不同。
Name: ORANGE TIMBERLAND BOOTS
Link: https://www.off---white.com/en/GB/men/products/omia073s184780161900