【问题标题】:can't collect href within a div using bs4无法使用 bs4 在 div 中收集 href
【发布时间】:2023-04-05 17:50:02
【问题描述】:

我是一个新手,试图使用 bs4抓取这个网站,方法是从指定的 div 收集 href,然后通过 href 浏览产品页面并收集数据,但我一直在收集的href。 如果有人帮助我解决这个问题,我会非常高兴:

import urllib.request
from bs4 import BeautifulSoup

urlpage = 'https://www.digikala.com/search/category-tire/' 
print(urlpage)

# scrape the webpage using beautifulsoup

# query the website and return the html to the variable 'page'
page = urllib.request.urlopen(urlpage)

# parse the html using beautiful soup and store in variable 'soup'
soup = BeautifulSoup(page, 'html.parser')

# find product items
results = soup.find_all('div', attrs={'class': 'c-product-box__title'})
print('BeautifulSoup - Number of results', len(results))

这是第一个结果,虽然当你打印结果时它会附带 36 个 div,我只是复制了第一个,我尽力不问并找到答案,但我什至没有接近,所以我很抱歉,如果它这么简单。

<div class="c-product-box__title"><a href="/product/dkp-539563/لاستیک-خودرو-میشلن-مدل-primacy-3-سایز-20555r16-دو-حلقه" target="_blank">لاستیک خودرو میشلن مدل Primacy 3 سایز 205/55R16 - دو حلقه</a></div>

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:
    # -*- coding: utf-8 -*-
    html_doc = '<div class="c-product-box__title"><a href="/product/dkp-539563/ﻼﺴﺗیک-ﺥﻭﺩﺭﻭ-ﻡیﺶﻠﻧ-ﻡﺪﻟ-primacy-3-ﺱﺍیﺯ-20555r16-ﺩﻭ-ﺢﻠﻘﻫ" target="_blank">ﻼﺴﺗیک ﺥﻭﺩﺭﻭ ﻡیﺶﻠﻧ ﻡﺪﻟ Primacy 3 ﺱﺍیﺯ 205/55R16 - ﺩﻭ ﺢﻠﻘﻫ</a></div>"'
    
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(html_doc, 'html.parser')
    
    for div in soup.find_all('div', class_='c-product-box__title'):
      print div.a['href']
    

    输出:

    $ python a.py
    /product/dkp-539563/لاستیک-خودرو-میشلن-مدل-primacy-3-سایز-20555r16-دو-حلقه
    

    https://www.crummy.com/software/BeautifulSoup/bs4/doc/#beautiful-soup-documentation

    【讨论】:

    • 这不能回答问题。
    • 非常感谢,这也有效,但我在想我应该在你的答案和 QHar 的答案之间使用哪种方法,顺便说一句,最后一行应该是 print (div.a['href'])跨度>
    • print 在 Python 2 和 3 上有所不同。
    【解决方案2】:

    您可以使用类和类型选择器结合子组合器来获取 div 的子 a 标签(通过类选择器指定 div)。在这种情况下是 36 所以不需要限制返回的孩子。

    import requests
    from bs4 import BeautifulSoup 
    
    url = 'https://www.digikala.com/search/category-tire/'
    r = requests.get(url)
    soup = BeautifulSoup(r.content,"lxml")
    links = [link['href'] for link in soup.select('.c-product-box__title > a')]
    print(len(links))
    print(links[0])
    

    【讨论】:

    • 非常感谢您的帮助,我想知道哪个答案是更好的方法,为什么?
    • 这个方法应该更快,因为它使用 css 选择器。我希望优化有利于这一点。另外,我更喜欢上面的列表理解而不是循环。
    【解决方案3】:

    对于每个生成的div,首先获取子a 元素,然后获取其href 属性的值,如下所示:

    results = soup.find_all('div', attrs={'class': 'c-product-box__title'})
    print('BeautifulSoup - Number of results', len(results))
    
    links = []
    for result in results:
        links.append(result.a['href'])
    
    print(links)
    

    这会产生一个包含 36 个链接的列表。以下是前 2 个示例:

    ['/product/dkp-539563/لاستیک-خودرو-میشلن-مدل-primacy-3-سایز-20555r16-دو-حلقه',
    '/product/dkp-959932/لاستیک-خودرو-گلدستون-مدل-2020-2000-سایز-1856514-دو-حلقه-مناسب-برای-انواع-رینگ-14',
    

    【讨论】:

    • 感谢您的回答,它有效,我有理由使用这个答案而不是其他答案吗?
    猜你喜欢
    • 2021-11-12
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 2013-09-21
    相关资源
    最近更新 更多