【问题标题】:BeautifulSoup select method with CSS selector copied from inspecting element of a page returns nothing从检查页面元素复制的带有 CSS 选择器的 BeautifulSoup 选择方法不返回任何内容
【发布时间】:2021-07-13 17:31:23
【问题描述】:
link = 'http://shop.oreilly.com/product/0636920028154.do'
req = requests.get(link)
bs = BeautifulSoup(req.text, 'html.parser')

我想获取该页面中的书籍描述,我使用了检查元素并复制了相关段落的 CSS 选择器:

bs.select('div.description:nth-child(2) > span:nth-child(2)')

不幸的是,代码返回一个空列表,我尝试了代码的多种变体,例如:

bs.select('div.description:nth-of-type(2) > span:nth-of-type(2)')
bs.select('div.description')
bs.select('div#description t-description')

它们都返回一个空列表,有什么问题?

【问题讨论】:

    标签: python css web-scraping beautifulsoup


    【解决方案1】:

    您没有正确的标签与您正在寻找的课程。描述位于<span> 标签下,您可以通过找到特定的<h2> 标签来获得。

    import requests
    from bs4 import BeautifulSoup
    
    link = 'http://shop.oreilly.com/product/0636920028154.do'
    req = requests.get(link)
    bs = BeautifulSoup(req.text, 'html.parser')
    
    desc = bs.find('h2', {'class':'t-description-heading'}).find_next('span').text
    

    或.select

    desc = bs.select('h2.t-description-heading')[0].find_next('span').text
    

    输出:

    'Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on author Mark Lutz’s popular training course, this updated fifth edition will help you quickly write efficient, high-quality code with Python. It’s an ideal way to begin, whether you’re new to programming or a professional developer versed in other languages.Complete with quizzes, exercises, and helpful illustrations,  this easy-to-follow, self-paced tutorial gets you started with both Python 2.7 and 3.3— the latest releases in the 3.X  and 2.X lines—plus all other releases in common use today. You’ll also learn some advanced language features that recently have become more common in Python code.Explore Python’s major built-in object types such as numbers, lists, and dictionariesCreate and process objects with Python statements, and learn Python’s general syntax modelUse functions to avoid code redundancy and package code for reuseOrganize statements, functions, and other tools into larger components with modulesDive into classes: Python’s object-oriented programming tool for structuring codeWrite large programs with Python’s exception-handling model and development toolsLearn advanced Python tools, including decorators, descriptors, metaclasses, and Unicode processing'
    

    【讨论】:

    • 是否也可以通过 .select 方法检索此信息?
    猜你喜欢
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 2014-05-11
    相关资源
    最近更新 更多