【问题标题】:how can I get the names in this html code by python?如何通过 python 获取此 html 代码中的名称?
【发布时间】:2022-01-19 20:05:57
【问题描述】:

I want to get both of names "Justin Cutroni" and "Krista Seiden" without the tags

这是我想通过python3获取名称的html代码:

我使用了beautifulsoup,但我不知道如何深入了解 html 代码并获取名称。

import requests
from bs4 import BeautifulSoup as bs

web_pages = ["https://maktabkhooneh.org/learn/"]

def find_lessons(web_page):
    # Load the webpage content
    r = requests.get(web_page)
    # Convert to a beautiful soup object
    soup = bs(r.content, features="html.parser")
    table = soup.select('div[class="course-card__title"]')
    data = [x.text.split(';')[-1].strip() for x in table]
    return data

find_teachers(web_pages[0])

【问题讨论】:

  • 使用开发者工具并找到 XPath。

标签: python html css beautifulsoup


【解决方案1】:

您正在查看course-card__title,当它出现时您想要的是course-card__teacher。当您使用 requests 时,查看真实的 HTML(使用 wgetcurl)通常比查看图像中的对象模型更有用。

你所拥有的几乎适用于该更改:

import requests
from bs4 import BeautifulSoup as bs

web_pages = ["https://maktabkhooneh.org/learn/"]

def find_teachers(web_page):
    # Load the webpage content
    r = requests.get(web_page)
    soup = bs(r.content, features="html.parser")
    table = soup.select('div[class="course-card__teacher"]')
    return [x.text.strip() for x in table]

print(find_teachers(web_pages[0]))

【讨论】:

  • 感谢您的帮助我使用此代码来获取课程的价格,但它不起作用!也可以帮我解决这个问题` def find_prices(web_page): r = requests.get(web_page) soup = bs(r.content, features="html.parser") table = soup.select('div[class=" course-card-extra__price"]') print([x.text.strip() for x in table]) `
  • 您需要为此提出一个单独的问题,因为它不相关。该页面上的 HTML 中没有价格。您必须从包装 <div> 中的 data-url 中单独获取它。比较复杂。
猜你喜欢
  • 2021-12-20
  • 2015-09-22
  • 2011-06-17
  • 2020-06-17
  • 1970-01-01
  • 1970-01-01
  • 2014-10-29
  • 1970-01-01
  • 2011-09-09
相关资源
最近更新 更多