【问题标题】:python bs4 find() return nonepython bs4 find() 返回无
【发布时间】:2021-04-27 22:23:50
【问题描述】:

使用 bs4 find() 函数我没有得到任何结果,即使它存在于 html 中。我正在尝试使用类 tab_content 获取所有 div。我在此链接 https://sofad.qc.ca/index.php?id_product=464&controller=product&id_lang=1 上找到了这个。所以请建议我如何以正确的方式做到这一点。 这是代码:

from bs4 import BeautifulSoup as bs
import requests

url = 'https://sofad.qc.ca/index.php?id_category=78&controller=category&id_lang=1'
r = requests.get(url)
soup = bs(r.content, 'html.parser')
tb = soup.find_all('a', class_='product_img_link')


for item in tb:
    link = item.get('href')
    r = requests.get(link)
    soup = bs(r.content, 'lxml')
    try:
        title = soup.find('h1', {'itemprop':'name'}).text
    except:
        title = ''
    try:
        price = soup.find('span', id='our_price_display').text
    except:
        price = ''
    try:
        img = soup.find('img', id='bigpic').get('src')
    except:
        img = ''
    try:
        dv = " ".join(soup.find('div', class_='rte').text.split())
    except:
        dv = ''
    for dvv in soup.find_all('div', class_='tab_content'):
        print(dvv)

【问题讨论】:

    标签: python web-scraping data-mining


    【解决方案1】:

    据我所知,语法是findAll而不是find_all

    要查找类为tab_contentdiv

    tab_content = soup.findAll('div', class_='tab_content')
    

    仅供参考,该页面的 html 树中没有带有 tab_content 类的 div。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      • 1970-01-01
      相关资源
      最近更新 更多