【问题标题】:Python BeautifulSoup to get content from parent/sibling relationshipPython BeautifulSoup 从父/兄弟关系中获取内容
【发布时间】:2020-02-28 22:42:31
【问题描述】:

部分 html 的结构如下。我想从中得到工作的“职称”和“时间”。我可以单独获取它们,例如:

from bs4 import BeautifulSoup


pages = '<div class="content"> \
                <a href="Org"> \
                        <h3 class="title"> \
                            Dep. Manager</h3> \
                        </a> \
                <div class="contributor"></div> \
                <p>John</p> \
                <time class="time"> \
                        <span class="timestamp">May 02 2016</span> \
                    </time> \
                </div>'


soup = BeautifulSoup(pages, "lxml")


soup.prettify()


s = soup.find_all(class_ = "title")[0]

t = soup.find_all('span', class_ = "timestamp")[0].text.strip()


pp_title = s.text.strip()

print t

print (pp_title)

它让我得到想要的东西。

Dep. Manager
May 02 2016

对于“时间”,我想要另一种方式来获取它,因为“时间”总是在“标题”下方。我试过这条线来获取“时间”,它不起作用。

print (s.parent.next_sibling.next_sibling)

从关系到“标题”获取“时间”的正确方法是什么?谢谢。

【问题讨论】:

    标签: python parsing web-scraping beautifulsoup


    【解决方案1】:

    您可以findParent指定详细信息:

    t = s.findParent("div", class_='content').find('span', class_='timestamp').text.strip()
    

    例子:

    titles = soup.find_all(class_="title")
    for title in titles:
        timestamp = title.findParent("div", class_='content').find('span', class_='timestamp').text.strip()
        print(title.text.strip(), timestamp)
    

    【讨论】:

      【解决方案2】:

      我不知道问题出在您提供的字符串还是其他地方,但是对next_sibling 的所有其他调用都会返回u' '。所以我尝试了这个:

      s.parent.next_sibling.next_sibling.next_sibling.next_sibling.next_sibling.next_sibling.findChildren()[0]
      

      我知道它很长,但它完成了工作。

      【讨论】:

        【解决方案3】:

        您可以将soup.find_all 与re 一起使用:

        import re
        from bs4 import BeautifulSoup as soup
        result = [i.get_text(strip=True) for i in soup(pages, 'html.parser').find_all(re.compile('h3|span'), {'class':re.compile('title|timestamp')})]
        

        输出:

        ['Dep. Manager', 'May 02 2016']
        

        【讨论】:

        • 伟大的思维方式,也有利于学习者的技巧和进步。希望你不介意我选择了较早的一个作为答案! :)
        • @MarkK 一点也不,很乐意提供帮助!
        【解决方案4】:

        选择共享父级,然后按类获取子级。假设父母总是两者兼有。如果需要,您可以更改选择器以确保两者都有。

        from bs4 import BeautifulSoup as bs
        
        html = '''
        <div class="content"> \
            <a href="Org"> \
                                <h3 class="title"> \
                                    Dep. Manager</h3> \
                                </a> \
            <div class="contributor"></div> \
            <p>John</p> \
            <time class="time"> \
                <span class="timestamp">May 02 2016</span> \
            </time> \
        </div>
        '''
        soup = bs(html, 'lxml')
        items = [i.text.strip() for i in soup.select('.content:has(.title) .title, .content:has(.title) .timestamp')]
        print(items)
        

        【讨论】:

        • 感谢您的分享。 BeautifulSoup解析的熟练和高级使用!希望你不介意我选择了较早的一个作为答案! :)
        猜你喜欢
        • 2022-11-14
        • 2023-04-09
        • 1970-01-01
        • 2014-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多