【问题标题】:Python - Extract href value based on content valuePython - 根据内容值提取href值
【发布时间】:2018-04-28 04:34:43
【问题描述】:

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

下面的 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)

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

最终输出应如下所示:

/en/GB/men/products/omia066s188000161001

【问题讨论】:

    标签: python python-3.x beautifulsoup


    【解决方案1】:

    试一试。

    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 方法获取链接。

    【讨论】:

    • 感谢您查看此内容 - 当我运行代码时,我得到以下信息。 print link.get('href') ^ SyntaxError: invalid syntax
    • @PiersThomas 你用的是什么版本的 Python?试试这个:print(link.get('href'))
    • Python V3.6.3 是我目前的版本
    • 文件“t.py”,第 36 行,在 如果链接.encode_contents() 中的“OFF WHITE Shoes”:类型错误:需要一个类似字节的对象,而不是“str”
    • 我的错,我使用的是 Python 2.7.10 版本。 Python 3 的逻辑应该仍然相同,只是语法不同。
    【解决方案2】:

    考虑到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
    

    【讨论】:

      猜你喜欢
      • 2012-02-13
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 2013-07-14
      • 2019-02-27
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多