【问题标题】:extracting deeply nested href in python with beautiful soup用漂亮的汤在python中提取深度嵌套的href
【发布时间】:2017-01-28 09:51:24
【问题描述】:

我正在尝试提取嵌套非常深的 href。结构如下:

<div id="main">
 <ol>
   <li class>
     <div class>
       <div class>
         <a class>
         <h1 class="title entry-title">
           <a href="http://wwww.link_i_want_to_extract.com">
           <span class>
         </h1>
        </div>
       </div>
     </li>

然后还有一堆其他&lt;li class&gt; 内有hrefs。 所以基本上父子顺序是

li - div - div - h1 - a href

我尝试了以下方法:

soup.select('li div div h1')

还有

soup.find_all("h1", { "class" : "title entry-title" }) 

还有

for item in soup.find_all("h1", attrs={"class" : "title entry-title"}):
        for link in item.find_all('a',href=TRUE):

这些似乎都不起作用,我得到[] 或清空.txt 文件。

另外,更令人不安的是,在定义soup 之后我执行print(soup) 我没有看到嵌套类,我只看到顶部的那个&lt;div id=main&gt; 并且还执行print soup.l 不是检索 l 个类。我不认为Beautifulsoup 能识别 l 类和其他类。

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    这对我有用

    from bs4 import BeautifulSoup
    
    html = '''
    <div id="main">
       <ol>
          <li class>
             <div class>
                <div class>
                   <a class>
                   <h1 class="title entry-title">
                      <a href="http://www.link_i_want_to_extract.com">
                      <span class>
                   </h1>
                </div>
             </div>
          </li>
          <li class>
             <div class>
                <div class>
                   <a class>
                   <h1 class="title entry-title">
                      <a href="https://other_link_i_want_to_extract.net">
                      <span class>
                   </h1>
                </div>
             </div>
          </li>
       </ol>
    </div>
    '''
    
    soup = BeautifulSoup(html, "lxml")
    for h1 in soup.find_all('h1', class_="title entry-title"):
        print(h1.find("a")['href'])
    

    【讨论】:

      【解决方案2】:

      你打错了:href=TRUE,应该是href=True

      s = """
      <div id="main">
         <ol>
            <li class>
               <div class>
                  <div class>
                     <a class>
                     <h1 class="title entry-title">
                        <a href="http://www.link_i_want_to_extract.com">
                        <span class>
                     </h1>
                  </div>
               </div>
            </li>
            <li class>
               <div class>
                  <div class>
                     <a class>
                     <h1 class="title entry-title">
                        <a href="https://other_link_i_want_to_extract.net">
                        <span class>
                     </h1>
                  </div>
               </div>
            </li>
         </ol>
      </div>
      """
      
      from bs4 import BeautifulSoup
      soup = BeautifulSoup(s, 'html.parser')
      
      for item in soup.find_all("h1", attrs={"class" : "title entry-title"}):
          for link in item.find_all('a',href=True):
              print('bs link:', link['href'])
      

      您也可以使用pyQuery,它提供类似 js/jquery 的查询语法:

      from pyquery import PyQuery as pq
      from lxml import etree
      
      d = pq(s)
      for link in d('h1.title.entry-title > a'):
          print('pq link:', pq(link).attr('href'))
      

      返回:

      bs link: http://www.link_i_want_to_extract.com
      bs link: https://other_link_i_want_to_extract.net
      pq link: http://www.link_i_want_to_extract.com
      pq link: https://other_link_i_want_to_extract.net
      

      【讨论】:

      • 很好,还不知道 PyQuery!
      【解决方案3】:

      使用. 查找第一个后代:

      soup.find('div', id="main").h1.a['href']
      

      或使用h1 作为锚点:

      soup.find("h1", { "class" : "title entry-title" }).a['href']
      

      【讨论】:

      • 我收到 AttributeError: 'NoneType' object has no attribute 'a'
      • @ppasler 在你评论之前,先测试一下。
      【解决方案4】:

      一个简单的方法:

      soup.select('a[href]')
      

      或:

      soup.findAll('a', href=True)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-06
        • 1970-01-01
        • 1970-01-01
        • 2020-02-13
        • 1970-01-01
        相关资源
        最近更新 更多